build_docker.yaml (4194B)
1 # 2 name: Create and publish a Docker image 3 4 # Configures this workflow to run every time a change is pushed to main 5 on: 6 push: 7 branches: ['main'] 8 9 # Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. 10 env: 11 REGISTRY: ghcr.io 12 IMAGE_NAME: mtrnord/ping-monitoring 13 14 # There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. 15 jobs: 16 build: 17 runs-on: ubuntu-latest 18 permissions: 19 contents: read 20 packages: write 21 strategy: 22 fail-fast: false 23 matrix: 24 platform: 25 - linux/amd64 26 - linux/arm/v7 27 - linux/arm64 28 steps: 29 - name: Prepare 30 run: | 31 platform=${{ matrix.platform }} 32 echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV 33 34 - name: Checkout repository 35 uses: actions/checkout@v4 36 37 # Fetch the latest Git tag and current branch 38 - name: Get Git version and branch 39 id: git_info 40 run: | 41 echo ::set-output name=VERSION::$(git describe --tags --abbrev=0) 42 echo ::set-output name=BRANCH::$(git rev-parse --abbrev-ref HEAD) 43 44 - name: Extract metadata (tags, labels) for Docker 45 id: meta 46 uses: docker/metadata-action@v5 47 with: 48 images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} 49 50 # Install QEMU to enable cross-platform builds 51 - name: Setup QEMU 52 uses: docker/setup-qemu-action@v3 53 54 # Set up Docker Buildx 55 - name: Set up Docker Buildx 56 uses: docker/setup-buildx-action@v3 57 58 - name: Log in to the Container registry 59 uses: docker/login-action@v3 60 with: 61 registry: ${{ env.REGISTRY }} 62 username: ${{ github.actor }} 63 password: ${{ secrets.GITHUB_TOKEN }} 64 - name: Build and push Docker image 65 uses: docker/build-push-action@v5 66 id: build 67 with: 68 context: . 69 platforms: ${{ matrix.platform }} 70 outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true 71 labels: ${{ steps.meta.outputs.labels }} 72 cache-from: type=gha,scope=${{ matrix.platform }} 73 cache-to: type=gha,mode=max,scope=${{ matrix.platform }} 74 build-args: | 75 VERSION=${{ steps.git_info.outputs.VERSION }} 76 BRANCH=${{ steps.git_info.outputs.BRANCH }} 77 - name: Export digest 78 run: | 79 mkdir -p /tmp/digests 80 digest="${{ steps.build.outputs.digest }}" 81 touch "/tmp/digests/${digest#sha256:}" 82 - name: Upload digest 83 uses: actions/upload-artifact@v4 84 with: 85 name: digests-${{ env.PLATFORM_PAIR }} 86 path: /tmp/digests/* 87 if-no-files-found: error 88 retention-days: 1 89 merge: 90 runs-on: ubuntu-latest 91 needs: 92 - build 93 permissions: 94 contents: read 95 packages: write 96 steps: 97 - name: Download digests 98 uses: actions/download-artifact@v4 99 with: 100 path: /tmp/digests 101 pattern: digests-* 102 merge-multiple: true 103 - name: Set up Docker Buildx 104 uses: docker/setup-buildx-action@v3 105 - name: Docker meta 106 id: meta 107 uses: docker/metadata-action@v5 108 with: 109 images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} 110 - name: Login to the Container registry 111 uses: docker/login-action@v3 112 with: 113 registry: ${{ env.REGISTRY }} 114 username: ${{ github.actor }} 115 password: ${{ secrets.GITHUB_TOKEN }} 116 - name: Create manifest list and push 117 working-directory: /tmp/digests 118 run: | 119 docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ 120 $(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *) 121 - name: Inspect image 122 run: | 123 docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }}