|
| 1 | +name: "Build & Push Docker images" |
| 2 | +description: "Build per-platform images from templates.yml with docker bake and push-by-digest artifacts." |
| 3 | + |
| 4 | +inputs: |
| 5 | + family: |
| 6 | + description: "Image family (repo folder name)." |
| 7 | + required: true |
| 8 | + distro: |
| 9 | + description: "Image distro (used in Dockerfile name and tags)." |
| 10 | + required: true |
| 11 | + platform: |
| 12 | + description: "Optional os/arch[/variant]; defaults to the native daemon platform." |
| 13 | + required: false |
| 14 | + default: "" |
| 15 | + docker-username: |
| 16 | + description: "dockerhub username" |
| 17 | + default: "" |
| 18 | + required: false |
| 19 | + docker-password: |
| 20 | + description: "dockerhub password" |
| 21 | + required: false |
| 22 | + ghcr-username: |
| 23 | + description: "ghcr username" |
| 24 | + default: "" |
| 25 | + required: false |
| 26 | + ghcr-password: |
| 27 | + description: "ghcr password" |
| 28 | + default: "" |
| 29 | + required: false |
| 30 | + push: |
| 31 | + description: "Push digests to repo" |
| 32 | + default: "true" |
| 33 | + required: false |
| 34 | + |
| 35 | +outputs: |
| 36 | + platform: |
| 37 | + description: "Canonical build platform (os/arch[/variant])." |
| 38 | + value: ${{ steps.detect.outputs.platform }} |
| 39 | + group: |
| 40 | + description: "Bake group (family-distro-platform)." |
| 41 | + value: ${{ steps.gen.outputs.group }} |
| 42 | + release: |
| 43 | + description: "Release identifier (family-distro)." |
| 44 | + value: ${{ steps.gen.outputs.release }} |
| 45 | + stage-targets: |
| 46 | + description: "JSON array of Bake targets built in this run." |
| 47 | + value: ${{ steps.gen.outputs.stage_targets }} |
| 48 | + metadata-path: |
| 49 | + description: "Path to the stored bake metadata JSON." |
| 50 | + value: ${{ steps.persist.outputs.metadata_path }} |
| 51 | + |
| 52 | +runs: |
| 53 | + using: "composite" |
| 54 | + steps: |
| 55 | + - name: Set up Docker Buildx |
| 56 | + uses: docker/setup-buildx-action@v3 |
| 57 | + |
| 58 | + - name: Login to Docker Hub |
| 59 | + if: ${{ inputs.docker-password }} |
| 60 | + uses: docker/login-action@v3 |
| 61 | + with: |
| 62 | + username: ${{ inputs.docker-username }} |
| 63 | + password: ${{ inputs.docker-password }} |
| 64 | + |
| 65 | + - name: Log in to GHCR |
| 66 | + if: ${{ inputs.ghcr-password }} |
| 67 | + uses: docker/login-action@v3 |
| 68 | + with: |
| 69 | + registry: ghcr.io |
| 70 | + username: ${{ inputs.ghcr-username }} |
| 71 | + password: ${{ inputs.ghcr-password }} |
| 72 | + |
| 73 | + - name: Set up Python |
| 74 | + uses: actions/setup-python@v6 |
| 75 | + with: |
| 76 | + python-version: "3.x" |
| 77 | + cache: "pip" |
| 78 | + |
| 79 | + - name: Install dependencies |
| 80 | + shell: bash |
| 81 | + run: pip install -r "${{ github.action_path }}/requirements.txt" |
| 82 | + |
| 83 | + - name: Detect build platform |
| 84 | + id: detect |
| 85 | + shell: bash |
| 86 | + run: | |
| 87 | + set -euo pipefail |
| 88 | + plat="${{ inputs.platform }}" |
| 89 | + if [[ -z "$plat" ]]; then |
| 90 | + plat="$(docker version -f '{{.Server.Os}}/{{.Server.Arch}}')" |
| 91 | + echo "Detected platform: $plat" |
| 92 | + fi |
| 93 | + echo "platform=$plat" >> "$GITHUB_OUTPUT" |
| 94 | + key="${plat//\//-}" |
| 95 | + echo "platform_key=$key" >> "$GITHUB_OUTPUT" |
| 96 | +
|
| 97 | + - name: Compute bake variables |
| 98 | + id: gen |
| 99 | + shell: bash |
| 100 | + run: | |
| 101 | + set -euo pipefail |
| 102 | + cmd=(python .github/actions/docker-bake/get_variables.py |
| 103 | + --family "${{ inputs.family }}" |
| 104 | + --distro "${{ inputs.distro }}" |
| 105 | + --platform "${{ steps.detect.outputs.platform }}" |
| 106 | + ${{ inputs.ghcr-password && format('--ghcr-username "{0}"', inputs.ghcr-username) || '' }} |
| 107 | + ${{ inputs.docker-password && format('--docker-username "{0}"', inputs.docker-username) || '' }} |
| 108 | + --digest |
| 109 | + ) |
| 110 | + output=$("${cmd[@]}") |
| 111 | + echo "$output" |
| 112 | +
|
| 113 | + - name: Bake |
| 114 | + id: bake |
| 115 | + uses: docker/bake-action@v6 |
| 116 | + with: |
| 117 | + files: docker-bake.hcl |
| 118 | + targets: ${{ steps.gen.outputs.group }} |
| 119 | + push: ${{ inputs.push }} |
| 120 | + set: | |
| 121 | + ${{ steps.gen.outputs.release }}-*.platform=${{ steps.detect.outputs.platform }} |
| 122 | + *.cache-to=type=gha,mode=max,scope=${{ steps.gen.outputs.group }} |
| 123 | + *.cache-from=type=gha,scope=${{ steps.gen.outputs.group }} |
| 124 | + ${{ steps.gen.outputs.set_lines }} |
| 125 | +
|
| 126 | + - name: Persist Bake metadata for merge |
| 127 | + id: persist |
| 128 | + shell: bash |
| 129 | + run: | |
| 130 | + set -euo pipefail |
| 131 | + meta_dir="${{ github.workspace }}/.tmp" |
| 132 | + mkdir -p "$meta_dir" |
| 133 | + meta_file="bake-metadata-${{ inputs.family }}-${{ inputs.distro }}-${{ steps.detect.outputs.platform_key }}.json" |
| 134 | + metadata='${{ steps.bake.outputs.metadata }}' |
| 135 | + printf '%s' "$metadata" > "$meta_dir/$meta_file" |
| 136 | + echo "metadata_path=$meta_dir/$meta_file" >> "$GITHUB_OUTPUT" |
| 137 | + echo "Saved $meta_file ($(wc -c < "$meta_dir/$meta_file") bytes)" |
| 138 | +
|
| 139 | + - uses: actions/upload-artifact@v4 |
| 140 | + with: |
| 141 | + name: bake-metadata-${{ steps.gen.outputs.group }} |
| 142 | + path: ${{ steps.persist.outputs.metadata_path }} |
| 143 | + |
| 144 | + - name: Summary |
| 145 | + if: always() |
| 146 | + shell: bash |
| 147 | + run: | |
| 148 | + { |
| 149 | + echo "### Bake summary" |
| 150 | + echo "- Family: \`${{ inputs.family }}\`" |
| 151 | + echo "- Distro: \`${{ inputs.distro }}\`" |
| 152 | + echo "- Platform: \`${{ steps.detect.outputs.platform }}\`" |
| 153 | + echo "- Group: \`${{ steps.gen.outputs.group }}\`" |
| 154 | + echo "- Targets: \`${{ steps.gen.outputs.stage_targets }}\`" |
| 155 | + echo "- Artifact: \`bake-metadata-${{ steps.gen.outputs.group }}\`" |
| 156 | + } >> "$GITHUB_STEP_SUMMARY" |
0 commit comments