build multiplatform images for testing #60
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 / Test / Push | |
| on: | |
| push: | |
| branches: | |
| - '**' | |
| workflow_call: | |
| workflow_dispatch: | |
| env: | |
| BUILD_SUFFIX: -build-${{ github.run_id }}_${{ github.run_attempt }} | |
| DOCKER_METADATA_SET_OUTPUT_ENV: 'true' | |
| jobs: | |
| build: | |
| runs-on: ${{ matrix.runner }} | |
| outputs: | |
| build-image-arm: ${{ steps.gen-output.outputs.image-arm64 }} | |
| build-image-x64: ${{ steps.gen-output.outputs.image-x64 }} | |
| strategy: | |
| fail-fast: false | |
| matrix: | |
| include: | |
| - platform: linux/amd64 | |
| runner: ubuntu-24.04 | |
| - platform: linux/arm64 | |
| runner: ubuntu-24.04-arm | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - id: build-meta | |
| name: Docker meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ghcr.io/${{ github.repository }} | |
| tags: type=sha,suffix=${{ env.BUILD_SUFFIX }} | |
| # Build cache is shared among all builds of the same architecture | |
| - id: cache-meta | |
| name: Docker meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ghcr.io/${{ github.repository }} | |
| tags: type=raw,value=buildcache-${{ runner.arch }} | |
| - id: get-registry | |
| name: Get the sanitized registry name | |
| run: | | |
| echo "registry=$(echo '${{ steps.build-meta.outputs.tags }}' | cut -f1 -d:)" | tee -a "$GITHUB_OUTPUT" | |
| - id: set_build_url | |
| name: Set BUILD_URL | |
| run: | | |
| echo "build_url=${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" | tee -a "$GITHUB_OUTPUT" | |
| - id: build | |
| name: Build/push the arch-specific image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| platforms: ${{ matrix.platform }} | |
| build-args: | | |
| BUILD_TIMESTAMP=${{ github.event.repository.updated_at }} | |
| BUILD_URL=${{ steps.set_build_url.outputs.build_url }} | |
| GIT_REF_NAME=${{ github.ref_name }} | |
| GIT_SHA=${{ github.sha }} | |
| GIT_REPOSITORY_URL=${{ github.repositoryUrl }} | |
| cache-from: type=registry,ref=${{ steps.cache-meta.outputs.tags }} | |
| cache-to: type=registry,ref=${{ steps.cache-meta.outputs.tags }},mode=max | |
| labels: ${{ steps.build-meta.outputs.labels }} | |
| provenance: mode=max | |
| sbom: true | |
| tags: ${{ steps.get-registry.outputs.registry }} | |
| outputs: type=image,push-by-digest=true,push=true | |
| - id: gen-output | |
| name: Write arch-specific image digest to outputs | |
| run: | | |
| echo "image-${RUNNER_ARCH,,}=${{ steps.get-registry.outputs.registry }}@${{ steps.build.outputs.digest }}" | tee -a "$GITHUB_OUTPUT" | |
| merge: | |
| runs-on: ubuntu-24.04 | |
| needs: | |
| - build | |
| env: | |
| DOCKER_APP_IMAGE_ARM64: ${{ needs.build.outputs.build-image-arm }} | |
| DOCKER_APP_IMAGE_X64: ${{ needs.build.outputs.build-image-x64 }} | |
| outputs: | |
| build-image: ${{ steps.meta.outputs.tags }} | |
| build-image-arm: ${{ needs.build.outputs.build-image-arm }} | |
| build-image-x64: ${{ needs.build.outputs.build-image-x64 }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Docker meta | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ghcr.io/${{ github.repository }} | |
| tags: | | |
| type=sha,suffix=-build-${{ github.run_id }}_${{ github.run_attempt }} | |
| - name: Push the multi-platform image | |
| run: | | |
| docker buildx imagetools create \ | |
| --tag "$DOCKER_METADATA_OUTPUT_TAGS" \ | |
| "$DOCKER_APP_IMAGE_ARM64" "$DOCKER_APP_IMAGE_X64" | |
| test: | |
| runs-on: ubuntu-24.04 | |
| needs: | |
| - merge | |
| env: | |
| COMPOSE_FILE: docker-compose.yml:docker-compose.ci.yml | |
| DOCKER_APP_IMAGE: ${{ needs.merge.outputs.build-image }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Compose | |
| uses: docker/setup-compose-action@v1 | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Set ARTIFACTS_DIR | |
| run: echo "ARTIFACTS_DIR=${RUNNER_TEMP}/artifacts" >> $GITHUB_ENV | |
| - name: Create the artifacts directory | |
| run: mkdir -p "$ARTIFACTS_DIR" | |
| - name: Record start time | |
| run: TEST_START=`date +%s` >> $GITHUB_ENV | |
| - name: Run the tests | |
| run: bin/test | |
| - name: Retain the compose file | |
| if: ${{ always() }} | |
| run: docker compose config | tee "${ARTIFACTS_DIR}/docker-compose.yml" | |
| - name: Retain compose logs | |
| if: ${{ always() }} | |
| run: docker compose logs | tee "${ARTIFACTS_DIR}/docker-services.log" | |
| - name: Retain Docker events | |
| if: ${{ always() }} | |
| run: docker events --json --since $TEST_START --until `date +%s` | tee "${ARTIFACTS_DIR}/docker-events.json" | |
| - name: Upload test report | |
| if: ${{ always() }} | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: Test Report (${{ github.sha }}-${{ github.run_id }}-${{ github.run_attempt }}) | |
| path: ${{ env.ARTIFACTS_DIR }} | |
| if-no-files-found: error | |
| push: | |
| runs-on: ubuntu-24.04 | |
| needs: | |
| - merge | |
| - test | |
| env: | |
| DOCKER_APP_IMAGE: ${{ needs.merge.outputs.build-image }} | |
| DOCKER_APP_IMAGE_ARM64: ${{ needs.merge.outputs.build-image-arm }} | |
| DOCKER_APP_IMAGE_X64: ${{ needs.merge.outputs.build-image-x64 }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to GitHub Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Produce permanent image tags | |
| id: branch-meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ghcr.io/${{ github.repository }} | |
| tags: | | |
| type=sha | |
| type=ref,event=branch | |
| type=raw,value=latest,enable={{is_default_branch}} | |
| - name: Retag and push the image | |
| run: | | |
| docker buildx imagetools create \ | |
| $(jq -cr '.tags | map("--tag " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") $DOCKER_APP_IMAGE_ARM64 $DOCKER_APP_IMAGE_X64 |