|
| 1 | +name: Build & Publish Docker Images |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - master |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + dry_run: |
| 10 | + description: 'Build images only, do not push' |
| 11 | + required: false |
| 12 | + default: 'true' |
| 13 | + |
| 14 | +jobs: |
| 15 | + discover-dockerfiles: |
| 16 | + runs-on: ubuntu-latest |
| 17 | + outputs: |
| 18 | + dockerfiles: ${{ steps.extract.outputs.dockerfiles }} |
| 19 | + steps: |
| 20 | + - name: Checkout repository |
| 21 | + uses: actions/checkout@v4 |
| 22 | + |
| 23 | + - name: Generate matrix from manifest.yaml |
| 24 | + id: extract |
| 25 | + run: | |
| 26 | + files=() |
| 27 | + roots=$(yq e -r 'keys | .[]' manifest.yaml) |
| 28 | + for root in $roots; do |
| 29 | + while read -r version; do |
| 30 | + dockerfile="./${root}/${version}/Dockerfile" |
| 31 | + # Read image_name including tag |
| 32 | + image_name=$(yq e -r ".${root}.versions.\"${version}\".image_name" manifest.yaml) |
| 33 | + if [ -n "$image_name" ] && [ "$image_name" != "null" ]; then |
| 34 | + files+=("$dockerfile|$image_name") |
| 35 | + fi |
| 36 | + done < <(yq e -r ".${root}.versions | keys | .[]" manifest.yaml) |
| 37 | + done |
| 38 | +
|
| 39 | + # Convert array to compact JSON for GitHub Actions matrix |
| 40 | + json=$(printf '%s\n' "${files[@]}" | jq -R -c . | jq -c -s .) |
| 41 | + echo "dockerfiles=$json" >> $GITHUB_OUTPUT |
| 42 | +
|
| 43 | + build-and-push: |
| 44 | + needs: discover-dockerfiles |
| 45 | + runs-on: ubuntu-latest |
| 46 | + strategy: |
| 47 | + matrix: |
| 48 | + entry: ${{ fromJson(needs.discover-dockerfiles.outputs.dockerfiles) }} |
| 49 | + |
| 50 | + env: |
| 51 | + DRY_RUN: ${{ github.event.inputs.dry_run }} |
| 52 | + |
| 53 | + steps: |
| 54 | + - name: Checkout repository |
| 55 | + uses: actions/checkout@v4 |
| 56 | + |
| 57 | + - name: Set up Docker Buildx |
| 58 | + uses: docker/setup-buildx-action@v3 |
| 59 | + |
| 60 | + - name: Log in to Docker Hub |
| 61 | + if: ${{ env.DRY_RUN != 'true' }} |
| 62 | + uses: docker/login-action@v3 |
| 63 | + with: |
| 64 | + username: ${{ secrets.DOCKERHUB_USERNAME }} |
| 65 | + password: ${{ secrets.DOCKERHUB_TOKEN }} |
| 66 | + |
| 67 | + - name: Parse Dockerfile and image |
| 68 | + id: metadata |
| 69 | + run: | |
| 70 | + dockerfile="${{ matrix.entry }}" |
| 71 | + dockerfile="${dockerfile%%|*}" |
| 72 | + image="${{ matrix.entry }}" |
| 73 | + image="${image##*|}" # Already includes tag from manifest |
| 74 | + dir=$(dirname "$dockerfile") |
| 75 | + short_sha="${GITHUB_SHA:0:7}" |
| 76 | +
|
| 77 | + echo "directory=$dir" >> $GITHUB_OUTPUT |
| 78 | + echo "image=$image" >> $GITHUB_OUTPUT |
| 79 | + echo "short_sha=$short_sha" >> $GITHUB_OUTPUT |
| 80 | +
|
| 81 | + - name: Build and push image |
| 82 | + uses: docker/build-push-action@v6 |
| 83 | + with: |
| 84 | + context: ${{ steps.metadata.outputs.directory }} |
| 85 | + file: ${{ steps.metadata.outputs.directory }}/Dockerfile |
| 86 | + push: ${{ env.DRY_RUN != 'true' }} |
| 87 | + tags: | |
| 88 | + ${{ steps.metadata.outputs.image }} |
0 commit comments