最近在用flutter web做网页开发,通过Github Actions、GAE(Google App Engine)实现CI/CD。记录下为了以后复盘。
主要实现功能
-
触发条件:创建PR(
opened
,synchronize
,reopened
,ready_for_review
)等相关动作触发CI操作,在关闭PR(closed
)的时候触发CD - 环境控制:通过merge到不同分支实现了dev、stg、prod环境的控制
- 具体功能:在Github Actions上实现format、analyze、test、build操作,把build后的文件上传到GAE上进行托管
- 通知:邮件通知
备注:这里不记录Load Balance及DNS相关操作及GCP上的相关配置,可以参阅官方文档完成。
注意:一些敏感的信息需要放在github settings里面设置,在yaml文件里直接引用即可。
build_deploy.yaml文件:
name: build_and_deploy
on:
pull_request:
types: [opened, synchronize, reopened, ready_for_review, closed]
workflow_dispatch:
jobs:
job_check:
name: Test
runs-on: ubuntu-latest
if: ${{ github.event.action != 'closed' }}
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
flutter-version: '3.7.5' # 指定 Flutter 版本
- name: Clean and Install pub
run: |
flutter clean
flutter pub get
- name: Analyze project
run: flutter analyze
- name: Check for the existence of unformatted files
run: dart format --set-exit-if-changed lib/
- name: Metrics project
run: flutter pub run dart_code_metrics:metrics analyze lib/
- name: Test project
run: flutter test
job_build_deploy:
name: Build And Deploy
runs-on: ubuntu-latest
env:
env_variables: debug
BUCKET_NAME: test_flutter_bucket
needs: job_check
if: ${{ github.event.action == 'closed' }}
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Setup Flutter
uses: subosito/flutter-action@v2
with:
flutter-version: '3.7.5' # 指定 Flutter 版本
- name: Auth
uses: google-github-actions/auth@v1
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v1
# Set environment variables
- name: Set environment variables (debug)
if: github.event.pull_request.base.ref == 'develop'
run: echo "env_variables=debug" >> $GITHUB_ENV
- name: Set environment variables (stg)
if: github.event.pull_request.base.ref == 'release'
run: echo "env_variables=stg" >> $GITHUB_ENV
- name: Set environment variables (release)
if: github.event.pull_request.base.ref == 'main'
run: echo "env_variables=release" >> $GITHUB_ENV
- name: Build Web
run: |
flutter build web --dart-define=MODE="${{ env.env_variables }}" --base-href="/" --web-renderer html
- name: Deploy
run: |
gcloud app deploy --project xxxxproject-idxxxxxx
job_notify:
name: Notify
runs-on: ubuntu-latest
needs: [job_check, job_build_deploy]
if: ${{ github.event.action == 'closed' }}
steps:
- name: Configure SMTP server
uses: dawidd6/action-send-mail@v3
with:
server_address: smtp.gmail.com
server_port: 465
username: ${{ secrets.EMAIL_USERNAME }}
password: ${{ secrets.EMAIL_PASSWORD }}
from: ${{ secrets.EMAIL_USERNAME }}
to: xxxxxxx@163.com
subject: 'Build completed successfully'
body: 'Your build TestFlutterWebGithubActions project has completed successfully.'
app.yaml
runtime: python27
threadsafe: true
handlers:
- url: /
static_files: build/web/index.html
upload: build/web/index.html
- url: /(.*)
static_files: build/web/\1
upload: build/web/(.*)
网友评论