Flutter Github Actions多平台打包

470 阅读1分钟

使用github actions打包多个平台,并根据pubspec.yaml获取版本号创建Release版本,并将各个平台的产物压缩上传.

因为没有苹果的开发者账号,下面只配置了五个平台:

name: Flutter 自动化打包

on:
  push:
    branches:
      - master

# 并发策略
concurrency:
  group: ${{ github.workflow }}-${{ github.event.number || github.ref }}
  # 如果有新的运行实例,取消正在进行的实例
  cancel-in-progress: true

jobs:
  build:
    name: Build
    # 运行环境,基于矩阵策略
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        type: [ web, macos, windows, linux, android ]
        include:
          - type: web
            os: macos-latest
            path: build
            output: web

          - type: macos
            os: macos-latest
            path: build/macos/Build/Products/Release
            output: ${{ github.event.repository.name }}.app

          - type: windows
            os: windows-latest
            path: build\windows\x64\runner
            output: Release

          - type: linux
            os: ubuntu-latest
            path: build/linux/x64/release
            output: bundle

          - type: android
            os: macos-latest
            path: build/app/outputs
            output: flutter-apk

    steps:
      - name: 代码检出
        uses: actions/checkout@v4

      - name: 安卓环境
        uses: actions/setup-java@v4
        if: ${{ matrix.type == 'android' }}
        with:
          distribution: "zulu"
          java-version: "17"

      - name: Linux环境
        if: ${{ matrix.type == 'linux' }}
        run: |
          sudo apt-get update -y
          sudo apt-get install -y ninja-build libgtk-3-dev

      - name: FlutterAction
        uses: subosito/flutter-action@v2
        with:
          channel: "stable"

      - name: 下载依赖
        run: flutter pub get

      - name: 生成代码
        run: dart run build_runner build

      - name: 获取版本号
        if: ${{ matrix.type != 'windows' }}
        run: |
          echo "version=$(awk -F': ' '/version:/ {gsub("\\+", "-", $2); print $2}' pubspec.yaml)" >> "$GITHUB_ENV"

      - name: 获取版本号(windows)
        if: ${{ matrix.type == 'windows' }}
        shell: pwsh
        run: |
          $version = (Get-Content pubspec.yaml -Raw) -match 'version:\s*([^\s]+)' | Out-Null
          $version = $Matches[1] -replace '\+', '-'
          echo "version=$version" | Out-File -Append -Encoding utf8 -FilePath $Env:GITHUB_ENV

      - name: 打印版本号
        run: |
          echo "version: ${{ env.version }}"

      - name: 打包WEB
        if: ${{ matrix.type == 'web' }}
        run: flutter build web --release --base-href "/${{ github.event.repository.name }}/"

      - name: 将web部署到Github Page
        uses: JamesIves/github-pages-deploy-action@v4
        if: ${{ matrix.type == 'web' }}
        with:
          token: ${{ secrets.TOKEN }}
          branch: gh-pages
          folder: build/web
          clean: true

      - name: 打包Macos
        if: ${{ matrix.type == 'macos' }}
        run: flutter build macos

      - name: 打包Windows
        if: ${{ matrix.type == 'windows' }}
        shell: pwsh
        run: flutter build windows

      - name: 打包Linux
        if: ${{ matrix.type == 'linux' }}
        run: flutter build linux

      - name: 打包安卓
        if: ${{ matrix.type == 'android' }}
        run: flutter build apk
        env:
          KEYSTORE_KEY_ALIAS: ${{ secrets.KEYSTORE_KEY_ALIAS }}
          KEYSTORE_KEY_PASSWORD: ${{ secrets.KEYSTORE_KEY_PASSWORD }}
          KEYSTORE_FILE: ${{ secrets.KEYSTORE_FILE }}
          KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}


      - name: 压缩产物
        if: ${{ matrix.type != 'windows' && matrix.type != 'android' }}
        run: |
          tar -czvf ${{ github.event.repository.name }}-${{ matrix.type }}.tar.gz -C ${{ matrix.path }} ${{ matrix.output }}

      - name: 压缩产物(windows)
        if: ${{ matrix.type == 'windows' }}
        shell: pwsh
        run: |
          Compress-Archive -Path '${{ matrix.path }}\${{ matrix.output }}' -DestinationPath '${{ github.event.repository.name }}-${{ matrix.type }}.zip'

      - name: Release
        uses: ncipollo/release-action@v1
        with:
          tag: "v${{ env.version }}"
          draft: false
          artifacts: "*.tar.gz,*.zip,build/app/outputs/flutter-apk/*.apk"
          allowUpdates: true
          token: ${{ secrets.TOKEN }}