每当项目打标签就会自动构建APK并上传到GitHub Release。目前body_path对应的文件不会取符合当前tag标签的那段文字,而且全部内容都会被上传到Release的body中。

编写配置文件.github/workflows/release.yml,其中CUSTOM_GITHUB_TOKEN需要在GitHub仓库的Settings -> Secrets中添加,需要有相关权限。

该Token在Settings -> Developer settings -> Personal access tokens -> Generate new token中生成,需要勾选repo权限。

仓库可以使用该Token根据标签创建Release。

pAdA4HA.png

name: Generated APK AAB (Upload - Create Artifact To Github Action)

env:
  # The name of the main module repository
  main_project_module: app

  # The name of the Play Store
  playstore_name: Frogobox ID

  # app name
  app_name: com.www.xxxx

on:
  push:
    tags:
      - 'v*'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      # Set Current Date As Env Variable
      - name: Set current date as env variable
        run: echo "date_today=$(date +'%Y-%m-%d')" >> $GITHUB_ENV

      # Set Repository Name As Env Variable
      - name: Set repository name as env variable
        run: echo "repository_name=$(echo '${{ github.repository }}' | awk -F '/' '{print $2}')" >> $GITHUB_ENV

      - name: Set Up JDK
        uses: actions/setup-java@v3
        with:
          distribution: 'zulu' # See 'Supported distributions' for available options
          java-version: '17'
          cache: 'gradle'

      - name: Change wrapper permissions
        run: chmod +x ./gradlew

      # Run Tests Build
      # - name: Run gradle tests
      #   run: ./gradlew test

      # Run Build Project
      # - name: Build gradle project
      #   run: ./gradlew build

      # Create APK Debug
#      - name: Build apk debug project (APK) - ${{ env.main_project_module }} module
#        run: ./gradlew assembleDebug

      # Create APK Release
      - name: Build apk release project (APK) - ${{ env.main_project_module }} module
        run: ./gradlew assembleRelease

      # Create Bundle AAB Release
      # Noted for main module build [main_project_module]:bundleRelease
#      - name: Build app bundle release (AAB) - ${{ env.main_project_module }} module
#        run: ./gradlew ${{ env.main_project_module }}:bundleRelease

      # Upload Artifact Build
      # Noted For Output [main_project_module]/build/outputs/apk/debug/
#      - name: Upload APK Debug - ${{ env.repository_name }}
#        uses: actions/upload-artifact@v3
#        with:
#          name: ${{ env.date_today }} - ${{ env.playstore_name }} - ${{ env.repository_name }} - APK(s) debug generated
#          path: ${{ env.main_project_module }}/build/outputs/apk/debug/

      # Noted For Output [main_project_module]/build/outputs/apk/release/
#      - name: Upload APK Release - ${{ env.repository_name }}
#        uses: actions/upload-artifact@v3
#        with:
#          name: ${{ env.date_today }} - ${{ env.playstore_name }} - ${{ env.repository_name }} - APK(s) release generated
#          path: ${{ env.main_project_module }}/build/outputs/apk/release/

      # Noted For Output [main_project_module]/build/outputs/bundle/release/
#      - name: Upload AAB (App Bundle) Release - ${{ env.repository_name }}
#        uses: actions/upload-artifact@v3
#        with:
#          name: ${{ env.date_today }} - ${{ env.playstore_name }} - ${{ env.repository_name }} - App bundle(s) AAB release generated
#          path: ${{ env.main_project_module }}/build/outputs/bundle/release/

      - uses: olegtarasov/get-tag@v2.1.3
        id: tagName

      # package
      - name: Pack
        run: |
          cd ${{ env.main_project_module }}/build/outputs/apk/release/
          tar zcvf ${{ env.app_name }}-${{ steps.tagName.outputs.tag }}.tar.gz `ls`
          sha256sum ${{ env.app_name }}-${{ steps.tagName.outputs.tag }}.tar.gz > ${{ env.app_name }}-${{ steps.tagName.outputs.tag }}.sha256.txt
          cd ..
      - name: ChangeLog
        run: |
          chmod +x get_changelog.sh
          ./get_changelog.sh ${{ steps.tagName.outputs.tag }}

      - name: Release
        uses: softprops/action-gh-release@v2
        with:
          files: |
            ${{ env.main_project_module }}/build/outputs/apk/release/${{ env.app_name }}-${{ steps.tagName.outputs.tag }}.tar.gz
            ${{ env.main_project_module }}/build/outputs/apk/release/${{ env.app_name }}-${{ steps.tagName.outputs.tag }}.sha256.txt
          body_path: changes_for_${{ steps.tagName.outputs.tag }}.txt
          token: ${{ secrets.CUSTOM_GITHUB_TOKEN }}
        env:
          GITHUB_REPOSITORY: your_name/your_repo

根据标签切分ChangeLog的脚本

#!/bin/bash

# 获取当前git标签
# CI使用current_tag=${{github.ref_name}}
current_tag=$1
if [ -z "$current_tag" ]; then
  current_tag=$(git describe --tags `git rev-list --tags --max-count=1`)
fi


# 检查changelog文件是否存在
changelog_file="CHANGELOG.md"
if [ ! -f "$changelog_file" ]; then
  echo "Changelog file not found!"
  exit 1
fi

# 提取当前标签对应的变更内容
awk -v tag="## $current_tag" '
  $0 ~ tag {flag=1; next}
  /^## / && flag {flag=0}
  flag' "$changelog_file" > changes_for_$current_tag.txt

echo "Changes for tag $current_tag have been written to changes_for_$current_tag.txt"

该脚本根据当前标签提取CHANGELOG.md中的内容,生成changes_for_${tag}.txt文件。例如,当前标签为v1.0.0,则生成changes_for_v1.0.0.txt文件。CHANGELOG.md文件格式如下:

## v1.0.0

- 新增功能1
- 新增功能2

## v0.1.0

- 修复bug1
- 修复bug2

参考

  • https://github.com/softprops/action-gh-release
  • https://github.com/amirisback/automated-build-android-app-with-github-action/