Skip to content

Update github workflows. #7

Update github workflows.

Update github workflows. #7

Workflow file for this run

name: Build and Release Android APK

Check failure on line 1 in .github/workflows/release.yml

View workflow run for this annotation

GitHub Actions / .github/workflows/release.yml

Invalid workflow file

(Line: 95, Col: 11): Unrecognized named-value: 'secrets'. Located at position 1 within expression: secrets.KEYSTORE_FILE != ''
# 触发条件:当推送以v开头的tag时
on:
push:
tags:
- 'v*.*.*' # 匹配 v1.0.0, v2.1.3 等格式
# 也可以手动触发
workflow_dispatch:
jobs:
build-and-release:
runs-on: ubuntu-latest
# softprops/action-gh-release 创建 Release 需要 contents 写权限
permissions:
contents: write
steps:
# 1. 检出代码
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # 获取完整历史,用于生成changelog
# 2. 设置JDK环境
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
# 3. 设置Android SDK
- name: Setup Android SDK
uses: android-actions/setup-android@v3
# 4. 缓存Gradle依赖
- name: Cache Gradle packages
uses: actions/cache@v4
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
# 5. 赋予gradlew执行权限
- name: Grant execute permission for gradlew
run: chmod +x gradlew
# 6. 获取版本信息
- name: Get version info
id: version
run: |
VERSION=${GITHUB_REF#refs/tags/}
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
echo "VERSION_CODE=$(git rev-list --count HEAD)" >> $GITHUB_OUTPUT
# 7. 构建Debug APK
- name: Build Debug APK
run: ./gradlew assembleDebug
# 8. 构建Release APK
- name: Build Release APK
run: ./gradlew assembleRelease
env:
VERSION_NAME: ${{ steps.version.outputs.VERSION }}
VERSION_CODE: ${{ steps.version.outputs.VERSION_CODE }}
# 9. 定位 Gradle 产出的 APK
# Why: app/build.gradle 通过 outputFileName 将 APK 重命名为
# BtLogger-{variant}-v{versionName}-{versionCode}({applicationId}).apk,
# 因此不能再使用 app-debug.apk / app-release.apk 这些默认名字。
- name: Locate APK artifacts
id: locate
run: |
set -euo pipefail
DEBUG_APK=$(find app/build/outputs/apk/debug -maxdepth 1 -name '*.apk' | head -n 1)
RELEASE_APK=$(find app/build/outputs/apk/release -maxdepth 1 -name '*.apk' | head -n 1)
if [ -z "${DEBUG_APK}" ] || [ -z "${RELEASE_APK}" ]; then
echo "::error::未找到构建产物: DEBUG_APK='${DEBUG_APK}' RELEASE_APK='${RELEASE_APK}'"
ls -R app/build/outputs/apk || true
exit 1
fi
echo "DEBUG_APK=${DEBUG_APK}" >> "$GITHUB_OUTPUT"
echo "RELEASE_APK=${RELEASE_APK}" >> "$GITHUB_OUTPUT"
echo "Debug APK: ${DEBUG_APK}"
echo "Release APK: ${RELEASE_APK}"
# 10. 签名 Release APK(仅在配置了签名 Secrets 时执行)
# 前提:Release APK 必须是 unsigned 状态。若 build.gradle 已配置 signingConfig,跳过此步骤。
- name: Sign Release APK
if: ${{ secrets.KEYSTORE_FILE != '' }}
run: |
set -euo pipefail
echo "${{ secrets.KEYSTORE_FILE }}" | base64 -d > keystore.jks
jarsigner -verbose -sigalg SHA256withRSA -digestalg SHA-256 \
-keystore keystore.jks \
-storepass "${{ secrets.KEYSTORE_PASSWORD }}" \
-keypass "${{ secrets.KEY_PASSWORD }}" \
"${{ steps.locate.outputs.RELEASE_APK }}" \
"${{ secrets.KEY_ALIAS }}"
# 11. 生成 Changelog
- name: Generate Changelog
id: changelog
run: |
set -euo pipefail
PREVIOUS_TAG=$(git describe --tags --abbrev=0 HEAD~1 2>/dev/null || echo "")
if [ -n "$PREVIOUS_TAG" ]; then
CHANGELOG=$(git log ${PREVIOUS_TAG}..HEAD --pretty=format:"- %s" --no-merges)
else
CHANGELOG=$(git log --pretty=format:"- %s" --no-merges -10)
fi
{
echo "CHANGELOG<<EOF"
echo "$CHANGELOG"
echo "EOF"
} >> "$GITHUB_OUTPUT"
# 12. 创建 Release 并上传所有 APK 资产
# Why: actions/create-release@v1 与 actions/upload-release-asset@v1 已归档,
# 改用仍在维护的 softprops/action-gh-release,通过 glob 一次性上传所有 APK。
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.version.outputs.VERSION }}
name: Release ${{ steps.version.outputs.VERSION }}
draft: false
prerelease: false
body: |
## 🚀 Release ${{ steps.version.outputs.VERSION }}
### 📱 Download
- **Release APK**: For production use
- **Debug APK**: For testing and debugging
### 📋 Changes
${{ steps.changelog.outputs.CHANGELOG }}
### 📊 Build Info
- Version Code: ${{ steps.version.outputs.VERSION_CODE }}
- Build Date: ${{ github.event.head_commit.timestamp }}
- Commit: ${{ github.sha }}
files: |
app/build/outputs/apk/release/*.apk
app/build/outputs/apk/debug/*.apk
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# 13. 上传构建产物到 Artifacts(备份)
- name: Upload APK Artifacts
uses: actions/upload-artifact@v4
with:
name: apk-files-${{ steps.version.outputs.VERSION }}
path: |
app/build/outputs/apk/release/*.apk
app/build/outputs/apk/debug/*.apk
retention-days: 30