name: Docker # thanks to https://github.com/sredevopsorg/multi-arch-docker-github-workflow on: push: branches: ["main"] tags: ["v*.*.*"] pull_request: branches: ["main"] workflow_dispatch: env: IMAGE_NAME: ${{ github.repository }} DOCKERHUB_USERNAME: c4illin concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: # The build job builds the Docker image for each platform specified in the matrix. build: strategy: fail-fast: false matrix: platform: - linux/amd64 - linux/arm64 permissions: contents: write packages: write attestations: write id-token: write runs-on: ${{ matrix.platform == 'linux/amd64' && 'ubuntu-24.04' || matrix.platform == 'linux/arm64' && 'ubuntu-24.04-arm' }} name: Build Docker image for ${{ matrix.platform }} steps: - name: Prepare environment for current platform # This step sets up the environment for the current platform being built. # It replaces the '/' character in the platform name with '-' and sets it as an environment variable. # This is useful for naming artifacts and other resources that cannot contain '/'. # The environment variable PLATFORMS_PAIR will be used later in the workflow. id: prepare run: | platform=${{ matrix.platform }} echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV - name: Checkout repository uses: actions/checkout@v6 - name: downcase REPO run: | echo "REPO=${GITHUB_REPOSITORY@L}" >> "${GITHUB_ENV}" - name: Docker meta default id: meta uses: docker/metadata-action@v5 with: images: ghcr.io/${{ env.REPO }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 with: platforms: ${{ matrix.platform }} - name: Login to GitHub Container Registry # here we only login to ghcr.io since the this only pushes internal images uses: docker/login-action@v4 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build and push by digest id: build uses: docker/build-push-action@v6 env: DOCKER_BUILDKIT: 1 with: context: . platforms: ${{ matrix.platform }} labels: ${{ steps.meta.outputs.labels }} annotations: ${{ steps.meta.outputs.annotations }} outputs: type=image,name=ghcr.io/${{ env.REPO }},push-by-digest=true,name-canonical=true,oci-mediatypes=true push: ${{ github.event_name != 'pull_request' || (github.event.pull_request.head.repo.full_name == github.repository && contains(fromJSON('["COLLABORATOR","MEMBER","OWNER"]'), github.event.pull_request.author_association)) }} cache-from: type=gha,scope=${{ matrix.platform }} cache-to: type=gha,mode=max,scope=${{ matrix.platform }} - name: Export digest run: | mkdir -p /tmp/digests digest="${{ steps.build.outputs.digest }}" touch "/tmp/digests/${digest#sha256:}" - name: Upload digest uses: actions/upload-artifact@v7 with: name: digests-${{ env.PLATFORM_PAIR }} path: /tmp/digests/* if-no-files-found: error retention-days: 1 merge: if: ${{ github.event_name != 'pull_request' || (github.event.pull_request.head.repo.full_name == github.repository && contains(fromJSON('["COLLABORATOR","MEMBER","OWNER"]'), github.event.pull_request.author_association)) }} name: Merge Docker manifests runs-on: ubuntu-latest permissions: contents: write packages: write attestations: write id-token: write needs: - build steps: - name: Download digests uses: actions/download-artifact@v8 with: path: /tmp/digests pattern: digests-* merge-multiple: true - name: downcase REPO run: | echo "REPO=${GITHUB_REPOSITORY@L}" >> "${GITHUB_ENV}" - name: Extract Docker metadata id: meta uses: docker/metadata-action@v5 with: images: | ghcr.io/${{ env.REPO }} ${{ env.IMAGE_NAME }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Login to GitHub Container Registry uses: docker/login-action@v4 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Login to Docker Hub uses: docker/login-action@v4 with: username: ${{ env.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Get execution timestamp with RFC3339 format id: timestamp run: | echo "timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> $GITHUB_OUTPUT - name: Create manifest list and push working-directory: /tmp/digests run: | docker buildx imagetools create \ $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ --annotation='index:org.opencontainers.image.description=${{ github.event.repository.description }}' \ --annotation='index:org.opencontainers.image.created=${{ steps.timestamp.outputs.timestamp }}' \ --annotation='index:org.opencontainers.image.url=${{ github.event.repository.url }}' \ --annotation='index:org.opencontainers.image.source=${{ github.event.repository.url }}' \ $(printf 'ghcr.io/${{ env.REPO }}@sha256:%s ' *) - name: Inspect image run: | docker buildx imagetools inspect 'ghcr.io/${{ env.REPO }}:${{ steps.meta.outputs.version }}'