Build #18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Build | |
| on: | |
| workflow_dispatch: | |
| jobs: | |
| prepare-matrix: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # 1. 拉取代码 | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| # 2. 生成 matrix(从项目根目录 matrix.json 读取) | |
| - name: Prepare build matrix | |
| id: generate | |
| run: | | |
| sudo apt-get update && sudo apt-get install -y jq | |
| if [ ! -f matrix.json ]; then | |
| echo "matrix.json not found in repository root." | |
| MATRIX='{"include": []}' | |
| else | |
| MATRIX=$(jq -c ' | |
| ( | |
| if type == "array" then . | |
| elif has("matrix") then .matrix | |
| else [.] | |
| end | |
| ) | |
| | sort_by(.ref, .sdk) | |
| | group_by(.ref, .sdk) | |
| | map({ | |
| ref: .[0].ref, | |
| sdk: .[0].sdk, | |
| tags: (map(.tags // []) | add | unique) | |
| }) | |
| | {include: .} | |
| ' matrix.json) | |
| fi | |
| echo "matrix=$MATRIX" >> "$GITHUB_OUTPUT" | |
| outputs: | |
| matrix: ${{ steps.generate.outputs.matrix }} | |
| build-and-publish: | |
| needs: prepare-matrix | |
| runs-on: ubuntu-latest | |
| strategy: | |
| # 3. 使用动态 matrix(每个 ref+sdk 组合最多构建一次) | |
| matrix: ${{ fromJson(needs.prepare-matrix.outputs.matrix) }} | |
| fail-fast: false | |
| steps: | |
| # 4. 拉取代码 | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| # 5. 登录 GitHub Package Registry | |
| - name: Log in to GitHub Package Registry | |
| uses: docker/login-action@v2 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| # 6. 基于 ref 构建一次,并推送该 ref 下的所有 tag | |
| - name: Build and push images for ref | |
| run: | | |
| sudo apt-get update && sudo apt-get install -y jq | |
| mkdir -p tmp | |
| cd tmp | |
| # 替换 Dockerfile 中的 --FLUTTER_REF-- 为 matrix.ref | |
| cp ../Dockerfile Dockerfile | |
| sed -i "s/--FLUTTER_REF--/${{ matrix.ref }}/g" Dockerfile | |
| image_base="ghcr.io/${{ github.repository }}/image" | |
| tags_json='${{ toJson(matrix.tags) }}' | |
| first_tag=$(echo "$tags_json" | jq -r '.[0]') | |
| if [ "$first_tag" = "null" ] || [ -z "$first_tag" ]; then | |
| echo "No tags found for ref=${{ matrix.ref }}, skip." | |
| exit 0 | |
| fi | |
| # 若主 tag 已存在则复用,避免重复构建镜像 | |
| if docker manifest inspect "${image_base}:${first_tag}" >/dev/null 2>&1; then | |
| echo "Image ${image_base}:${first_tag} already exists, skip build." | |
| docker pull "${image_base}:${first_tag}" | |
| else | |
| docker build -t "${image_base}:${first_tag}" . | |
| fi | |
| # 为同一 ref 的其他 tag 创建别名 | |
| for tag in $(echo "$tags_json" | jq -r '.[]'); do | |
| if [ "$tag" != "$first_tag" ]; then | |
| docker tag "${image_base}:${first_tag}" "${image_base}:${tag}" | |
| fi | |
| done | |
| # 推送所有 tag | |
| for tag in $(echo "$tags_json" | jq -r '.[]'); do | |
| docker push "${image_base}:${tag}" | |
| done | |