Add github action to publish docker images #9
Workflow file for this run
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 & Publish Docker Images | |
| on: | |
| push: | |
| branches: | |
| - master | |
| pull_request: | |
| workflow_dispatch: | |
| inputs: | |
| dry_run: | |
| description: 'Build images only, do not push' | |
| required: false | |
| default: 'true' | |
| jobs: | |
| discover-dockerfiles: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| dockerfiles: ${{ steps.extract.outputs.dockerfiles }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Generate matrix from manifest.yaml | |
| id: extract | |
| run: | | |
| files=() | |
| roots=$(yq e -r 'keys | .[]' manifest.yaml) | |
| for root in $roots; do | |
| while read -r version; do | |
| dockerfile="./${root}/${version}/Dockerfile" | |
| # Read image_name including tag | |
| image_name=$(yq e -r ".${root}.versions.\"${version}\".image_name" manifest.yaml) | |
| if [ -n "$image_name" ] && [ "$image_name" != "null" ]; then | |
| files+=("$dockerfile|$image_name") | |
| fi | |
| done < <(yq e -r ".${root}.versions | keys | .[]" manifest.yaml) | |
| done | |
| # Convert array to compact JSON for GitHub Actions matrix | |
| json=$(printf '%s\n' "${files[@]}" | jq -R -c . | jq -c -s .) | |
| echo "dockerfiles=$json" >> $GITHUB_OUTPUT | |
| build-and-push: | |
| needs: discover-dockerfiles | |
| runs-on: ubuntu-latest | |
| strategy: | |
| matrix: | |
| entry: ${{ fromJson(needs.discover-dockerfiles.outputs.dockerfiles) }} | |
| env: | |
| DRY_RUN: ${{ github.event.inputs.dry_run }} | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Parse Dockerfile and image | |
| id: metadata | |
| run: | | |
| dockerfile="${{ matrix.entry }}" | |
| dockerfile="${dockerfile%%|*}" | |
| image="${{ matrix.entry }}" | |
| image="${image##*|}" # Already includes tag from manifest | |
| dir=$(dirname "$dockerfile") | |
| short_sha="${GITHUB_SHA:0:7}" | |
| echo "directory=$dir" >> $GITHUB_OUTPUT | |
| echo "image=$image" >> $GITHUB_OUTPUT | |
| echo "short_sha=$short_sha" >> $GITHUB_OUTPUT | |
| - name: Build and push image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: ${{ steps.metadata.outputs.directory }} | |
| file: ${{ steps.metadata.outputs.directory }}/Dockerfile | |
| push: ${{ env.DRY_RUN != 'true' }} | |
| tags: | | |
| ${{ steps.metadata.outputs.image }} | |
| ${{ steps.metadata.outputs.image }}-sha:${{ steps.metadata.outputs.short_sha }} |