记录一下,免得忘了
实现根据tag自动打包并上传至release

workflow

name: Android CI

# 触发器
on:
  push:
    tags:
      - v*
  pull_request:
    tags:
      - v*

jobs:
  build:

    runs-on: ubuntu-latest
    # 设置jdk环境为1.8
    steps:
    - uses: actions/checkout@v2
    - name: set up JDK 1.8
      uses: actions/setup-java@v1
      with:
        java-version: 1.8
    # 获取打包秘钥
    - name: Checkout Android Keystore
      uses: actions/checkout@v2
      with:
        repository: 存储android打包用的key的仓库(格式:用户名/仓库名)
        token: ${{ secrets.TOKEN }} # 连接仓库的token,需要单独配置
        path: keystore # 仓库的根目录名
    # 打包release
    - name: Build with Gradle
      run: bash ./gradlew assembleRelease
    # 创建release
    - name: Create Release
      id: create_release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        #GitHub 会自动创建 GITHUB_TOKEN 密码以在工作流程中使用。 
        #您可以使用 GITHUB_TOKEN 在工作流程运行中进行身份验证。
        #当您启用 GitHub Actions 时,GitHub 在您的仓库中安装 GitHub 应用程序。 
        #GITHUB_TOKEN 密码是一种 GitHub 应用程序 安装访问令牌。 
        #您可以使用安装访问令牌代表仓库中安装的 GitHub 应用程序 进行身份验证。 
        #令牌的权限仅限于包含您的工作流程的仓库。 更多信息请参阅“GITHUB_TOKEN 的权限”。
        #在每个作业开始之前, GitHub 将为作业提取安装访问令牌。 令牌在作业完成后过期。
      with:
        tag_name: ${{ github.ref }}
        release_name: Release ${{ github.ref }}
        draft: false
        prerelease: false
    # 获取apk版本号
    - name: Get Version Name
      uses: actions/github-script@v3
      id: get-version
      with:
        script: |
          const str=process.env.GITHUB_REF;
          return str.substring(str.indexOf("v"));
        result-encoding: string
    # 上传至release的资源
    - name: Upload Release Asset
      id: upload-release-asset 
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        upload_url: ${{ steps.create_release.outputs.upload_url }} # 上传网址,无需改动
        asset_path: app/build/outputs/apk/release/app-release.apk # 上传路径
        asset_name: LightTimetable-${{steps.get-version.outputs.result}}.apk # 资源名
        asset_content_type: application/vnd.android.package-archiv #资源类型
    # 存档打包的文件
    - name: Archive production artifacts
      uses: actions/upload-artifact@v2
      with:
        name: build
        path: app/build/outputs #将打包之后的文件全部上传(里面会有混淆的map文件)

创建保存打包秘钥的私有仓库

仓库里保存以下两个文件

android github 发布 github action android_android github 发布


key.jks是秘钥文件

keystore.properties

storePassword=创建秘钥时的key store密码
keyPassword=创建秘钥时的key密码
keyAlias=秘钥别名
storeFile=../keystore/key.jks

将两个文件上传至新仓库

android github 发布 github action android_android_02

配置release打包签名

在Android项目中获取秘钥

git submodule add 秘钥仓库地址 keystore

在项目根目录下新建version.properties文件,用于保存版本号

version.properties
versionName=1.0.0
versionCode=28
配置gradle
// 读取前面配置的keystore.properties
def keyProps = new Properties()
def keyPropsFile = rootProject.file('keystore/keystore.properties')
if (keyPropsFile.exists()) {
    keyProps.load(new FileInputStream(keyPropsFile))
}

// 读取version.properties
def versionProps = new Properties()
def versionPropsFile = rootProject.file('version.properties')
if (versionPropsFile.exists()) {
    versionProps.load(new FileInputStream(versionPropsFile))
}

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"
    defaultConfig {
        versionCode versionProps['versionCode'].toInteger()
        versionName versionProps['versionName']
    }
    // 签名
    signingConfigs {
        release {
            keyAlias keyProps['keyAlias']
            keyPassword keyProps['keyPassword']
            storeFile keyProps['storeFile'] ? file(keyProps['storeFile']) : null
            storePassword keyProps['storePassword']
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release //配置签名文件
        }

    }
}

配置好之后可以用打包命令./gradlew assembleRelease测试是否配置正确

配置连接私有秘钥仓库的token


android github 发布 github action android_版本号_03

android github 发布 github action android_android_04


android github 发布 github action android_github_05


android github 发布 github action android_git_06

将生成的秘钥保存在仓库的Secrets中,名称为TOKEN

android github 发布 github action android_android_07

gradle task自动升级版本号

会自动升级版本号,打tag,然后推送至github然后,github会开始自动构建

task upgradeVersion {
    group 'help'
    description '构建新版本'
    doLast {
        println("---自动升级版本号---\n")
        def versionProps = new Properties()
        def versionPropsFile = rootProject.file('version.properties')
        if (versionPropsFile.exists()) {
            versionProps.load(new FileInputStream(versionPropsFile))
        }
        String oldVersionCode = versionProps['versionCode']
        String oldVersionName = versionProps['versionName']
        if (oldVersionCode == null || oldVersionName == null ||
                oldVersionCode.isEmpty() || oldVersionName.isEmpty()) {
            println("error:版本号不能为空")
            return;
        }
        versionProps['versionCode'] = String.valueOf(versionProps['versionCode'].toInteger() + 1)
        String str = versionProps['versionName'].toString()
        versionProps['versionName'] = str.substring(0, str.lastIndexOf('.') + 1) +
                (str.substring(str.lastIndexOf('.') + 1).toInteger() + 1)
        String tip =
                "版本号从$oldVersionName($oldVersionCode)升级到${versionProps['versionName']}(${versionProps['versionCode']})"
        println(tip)

        def writer = new FileWriter(versionPropsFile)
        versionProps.store(writer, null)
        writer.flush()
        writer.close()
        def tag = "v${versionProps['versionName']}"
        cmdExecute("git pull")
        cmdExecute("git add version.properties")
        cmdExecute("git commit -m \"版本号升级为:$tag\"")
        cmdExecute("git push origin")
        cmdExecute("git tag $tag")
        cmdExecute("git push origin $tag")
    }
}

void cmdExecute(String cmd) {
    println "\n执行$cmd"
    println cmd.execute().text
}

运行task

android github 发布 github action android_git_08