使用 GitHub Actions 做 Flutter 项目的 CI/CD

3,195 阅读1分钟

GitHub 发布了能够直接集成在 GitHub 仓库的 CI/CD 工具, GitHub Actions。并且针对 Public 项目免费提供。

我尝试了在 Flutter 项目上使用,相比 travis-ci,目前看来除了方便(不用跳出 github 站外), 还没有其他优点发现。

使用的已有的 Action: github.com/subosito/fl…

运行截图

Github Actions

配置文件

发生pull_request 时触发 test

.github/workflows/check.yml

name: F4LabCI
on: pull_request

jobs:
  check:
    name: Test on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-java@v1
        with:
          java-version: "12.x"
      - uses: subosito/flutter-action@v1
        with:
          # same with pubspec.yaml
          flutter-version: "1.9.1+hotfix.2"
      - run: flutter pub get
      - run: flutter test --no-pub test/

新建 release-v* tag 时触发打包, 并上传到 release

.github/workflows/release.yml

name: F4LabCIRelease
on:
  push:
    tags:
      - "release-v*"

jobs:
  release-to-gitHub:
    name: release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-java@v1
        with:
          java-version: "12.x"
      - uses: subosito/flutter-action@v1
        with:
          # same with pubspec.yaml
          flutter-version: "1.9.1+hotfix.2"
      - run: flutter pub get
      - run: flutter analyze --no-pub --no-current-package lib/ test/
      - run: flutter test --no-pub test/
      - run: flutter build apk
      - uses: softprops/action-gh-release@v1
        with:
          files: build/app/outputs/apk/release/app-release.apk
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

总结

  • 整体构建思想和 GitLab-CI, Travis-CI 类似。只是关键字可能不一样
  • 开源的方便和强大。可以方便的复用别人提供的 actions
  • 一些功能还是没有 Travis-CI、GitLab-CI 方便
  • 本次实验的仓库: github.com/stefanJi/Fl…