Skip to content

Create flutter-ci-cd.yml #1

Create flutter-ci-cd.yml

Create flutter-ci-cd.yml #1

Workflow file for this run

name: Build and Release Flutter APK
on:
push:
branches:
- main
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: write # 允许 GITHUB_TOKEN 发布 release
steps:
- name: Checkout source
uses: actions/checkout@v4
- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
flutter-version: '3.13.0'
- name: Disable analytics
run: flutter config --no-analytics
# 解码 keystore
- name: Decode keystore from secret
run: |
echo "${{ secrets.KEYSTORE_BASE64 }}" | base64 -d > flutter_printer_qpos/example/android/app/app.keystore
# 创建 key.properties 文件
- name: Create key.properties
run: |
cat > flutter_printer_qpos/example/android/key.properties <<EOF
storePassword=${{ secrets.KEYSTORE_PASSWORD }}
keyPassword=${{ secrets.KEY_PASSWORD }}
keyAlias=${{ secrets.KEY_ALIAS }}
storeFile=app.keystore
EOF
# 安装依赖
- name: Install dependencies
run: flutter pub get
working-directory: flutter_printer_qpos/example
# 分析插件本体
- name: Analyze (non-fatal)
run: flutter analyze || true
working-directory: flutter_printer_qpos
# 构建 APK(已签名)
- name: Build signed APK
run: flutter build apk --release
working-directory: flutter_printer_qpos/example
# 自动生成 TAG(递增规则 0.0.1 -> 0.0.2 ... -> 0.1.0)
- name: Determine next tag
id: tag
run: |
# 获取远程 tag 列表
git fetch --tags
latest_tag=$(git tag | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1)
if [[ -z "$latest_tag" ]]; then
next_tag="1.0.0"
else
IFS='.' read -r major minor patch <<<"${latest_tag}"
if [[ $patch -lt 9 ]]; then
patch=$((patch+1))
else
patch=0
minor=$((minor+1))
fi
next_tag="${major}.${minor}.${patch}"
fi
echo "Next tag: $next_tag"
echo "tag=$next_tag" >> $GITHUB_OUTPUT
# 创建 Git Tag(必要)
- name: Create Git tag
run: |
git config user.name "github-actions"
git config user.email "github-actions@github.com"
git tag ${{ steps.tag.outputs.tag }}
git push origin ${{ steps.tag.outputs.tag }}
# 创建 GitHub Release 并上传 APK
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ steps.tag.outputs.tag }}
name: Release ${{ steps.tag.outputs.tag }}
files: flutter_printer_qpos/example/build/app/outputs/flutter-apk/app-release.apk
env:
GITHUB_TOKEN: ${{ github.token }}