iT邦幫忙

第 12 屆 iT 邦幫忙鐵人賽

DAY 23
0
Mobile Development

ReactNative 懶人開發之路,薪水小偷練成日記(X系列 第 23

[Day:22] GitHub Actions 懶人部署-Android 打包入門

首先,建立你的專案存儲庫(repository)

再來,找到我們所需的 Actions (Marketplace)

reactnativecommunity/react-native-android
範例: https://github.com/react-native-community/ci-sample
這個 Docker image 提供的 container 可以幫助我們完成 React Naive Android 的環境建置
也有支援其他 CI 工具

  • circleci
  • gitlab
  • travis-ci
  • azure devops
  • github actions

Generate Build Number

actions/checkout@v2

Generate Build Number

einaregilsson/build-number@v2

create Release Tag

actions/create-release@v1

Upload Artifact

actions/upload-artifact@v1

Upload Asset to Release

actions/upload-release-asset@v1

接者,建立你專案的Actions(workflows)

在GitHub上的存儲庫中,找到在.github/workflows名為的目錄中創建一個新文件 Build-Android.yml。
有關更多信息,請參見“創建新文件”。

將以下YAML內容複製到 Build-Android.yml 文件中。
注意:如果您的默認分支不是main,請更新的值 DEFAULT_BRANCH 以匹配存儲庫的默認分支名稱。

name: Github Actions

# on: [push, pull_request]
on:
  push:
    branches: [  master  ]
    tags:
      - 'v*'
  pull_request:
    branches: [  master  ]
jobs:
  build:
    runs-on: ubuntu-latest
    container: reactnativecommunity/react-native-android
    steps:
    - name: "#️⃣ Generate Build Number"
      id: buildnumber
      uses: einaregilsson/build-number@v2
      with:
        token: ${{ secrets.ACTION_TEST }}
    - name: Print new build number
      run: |
        echo "Build number is $BUILD_NUMBER"
        echo "Build number is $buildnumber" ${{ steps.buildnumber.outputs.build_number }}
        echo  $BUILD_NUMBER
    - uses: actions/checkout@v2
    - name: Envinfo
      run: npx envinfo
    - name: Build application
      run: |
        yarn install && cd android && chmod +x gradlew && ./gradlew build bundleRelease 
        du -shc `find . -regex '.+\.\(apk\|aab\)$'`
#      run: yarn install && cd android && chmod +x gradlew && ./gradlew assembleRelease
            
    #Prepare Release
      
    - name: Prepare Release
      id: prepare_release
      run: |
        APK_PATH=`find . -name '*.apk' | grep release`
        echo ::set-output name=apk_path::$APK_PATH
        APK_NAME=`basename $APK_PATH`
        echo ::set-output name=apk_name::$APK_NAME
        BUNDLE_PATH=`find . -name '*.aab' | grep release | grep -v intermediates`
        echo ::set-output name=bundle_path::$BUNDLE_PATH
        BUNDLE_NAME=`basename $BUNDLE_PATH`
        echo ::set-output name=bundle_name::$BUNDLE_NAME
#        選用
#        TAG_NAME=`echo $GITHUB_REF | cut -d / -f3`
#        echo ::set-output name=tag_name::$TAG_NAME
#        PACKAGE_NAME=`./tools/aapt2 dump packagename $APK_PATH`
#        echo ::set-output name=package_name::$PACKAGE_NAME
    #Uploads
    - name: Upload Artifact
      if: steps.prepare_release.outputs.apk_path
      uses: actions/upload-artifact@v1
      with:
        name: app-release.apk
        path: android/app/build/outputs/apk/release/     
    #Uploads
    - name: Upload Artifact
      if: steps.prepare_release.outputs.bundle_path
      uses: actions/upload-artifact@v1
      with:
        name: app-release.aab
        path: android/app/build/outputs/bundle/release/  
        
    - name: Create GitHub release
      id: create_release
#       if: steps.version_check.outputs.changed == 'true'
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: V-${{ steps.buildnumber.outputs.build_number }}
        release_name: V-${{ steps.buildnumber.outputs.build_number }}
        body: |
          ![release!](https://i.giphy.com/media/lPulaaB9lDMnGZiivs/source.gif)
          # Changelog
          Release type: ${{ steps.buildnumber.outputs.build_number }}
        prerelease: true


    - name: Upload APK Asset
      if: steps.create_release.outputs.upload_url && steps.prepare_release.outputs.apk_path && steps.prepare_release.outputs.apk_name
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
      with:
        upload_url: ${{steps.create_release.outputs.upload_url}}
        asset_path: ${{steps.prepare_release.outputs.apk_path}}
        asset_name: ${{steps.prepare_release.outputs.apk_name}}
        asset_content_type: application/vnd.android.package-archive
#       選用 BBA
    - name: Upload Bundle Asset
      if: steps.create_release.outputs.upload_url && steps.prepare_release.outputs.bundle_path && steps.prepare_release.outputs.bundle_name
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
      with:
        upload_url: ${{steps.create_release.outputs.upload_url}}
        asset_path: ${{steps.prepare_release.outputs.bundle_path}}
        asset_name: ${{steps.prepare_release.outputs.bundle_name}}
        asset_content_type: application/octet-stream
        

測試他

你會看到,就是成功了
在 Download artifacts 有兩個可以下載的檔案

 app-release.aab26.3 MB
 app-release.apk

https://ithelp.ithome.com.tw/upload/images/20201006/20104220ce6Zsdf12e.png

在 Pre-release 中可以也找到完整的 Assets

app-release.aab 
app-release.apk 
Source code(zip)
Source code(tar.gz)

https://ithelp.ithome.com.tw/upload/images/20201006/20104220AAJyRpqTDk.png

明天,我將說明我如何用 GitHub Actions 修改 Build Number 並上架到 Play Console測試


上一篇
[Day:21] GitHub Actions 懶人部署-看懂 Actions
下一篇
[Day:23] GitHub Actions 懶人部署-Android 發布上架
系列文
ReactNative 懶人開發之路,薪水小偷練成日記(X31

尚未有邦友留言

立即登入留言