|
| 1 | +name: Build Docker Container |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: ['**'] |
| 6 | + tags: ["*.*.*"] |
| 7 | + pull_request: |
| 8 | + branches: ["main"] |
| 9 | + |
| 10 | +env: |
| 11 | + REGISTRY: ghcr.io |
| 12 | + IMAGE_NAME: 'pythoncoderas/<name>' |
| 13 | + |
| 14 | +concurrency: |
| 15 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 16 | + cancel-in-progress: true |
| 17 | + |
| 18 | +jobs: |
| 19 | + get-image-name: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + steps: |
| 22 | + - run: echo "null" |
| 23 | + outputs: |
| 24 | + image: ${{ env.IMAGE_NAME }} |
| 25 | + build: |
| 26 | + runs-on: ubuntu-latest |
| 27 | + needs: [get-image-name] |
| 28 | + if: ${{ needs.get-image-name.outputs.image != 'pythoncoderas/<name>' }} |
| 29 | + permissions: |
| 30 | + contents: read |
| 31 | + packages: write |
| 32 | + strategy: |
| 33 | + fail-fast: false |
| 34 | + matrix: |
| 35 | + platform: |
| 36 | + - linux/amd64 |
| 37 | + - linux/arm64 |
| 38 | + |
| 39 | + steps: |
| 40 | + - name: Checkout repository |
| 41 | + uses: actions/checkout@v3 |
| 42 | + |
| 43 | + - name: Set up Docker Buildx |
| 44 | + |
| 45 | + |
| 46 | + - name: Log into registry ${{ env.REGISTRY }} |
| 47 | + if: github.event_name != 'pull_request' |
| 48 | + |
| 49 | + with: |
| 50 | + registry: ${{ env.REGISTRY }} |
| 51 | + username: ${{ github.actor }} |
| 52 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 53 | + |
| 54 | + - name: Extract Docker metadata |
| 55 | + id: meta |
| 56 | + |
| 57 | + with: |
| 58 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 59 | + |
| 60 | + - name: Build Docker image |
| 61 | + id: build |
| 62 | + |
| 63 | + with: |
| 64 | + context: . |
| 65 | + labels: ${{ steps.meta.outputs.labels }} |
| 66 | + cache-from: type=gha |
| 67 | + cache-to: type=gha,mode=max |
| 68 | + platforms: ${{ matrix.platform }} |
| 69 | + provenance: true |
| 70 | + sbom: true |
| 71 | + outputs: type=image,name=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=${{ github.event_name != 'pull_request' }} |
| 72 | + |
| 73 | + - name: Export digest |
| 74 | + if: ${{ github.event_name != 'pull_request' }} |
| 75 | + run: | |
| 76 | + mkdir -p /tmp/digests |
| 77 | + digest="${{ steps.build.outputs.digest }}" |
| 78 | + touch "/tmp/digests/${digest#sha256:}" |
| 79 | + - name: Upload digest |
| 80 | + if: ${{ github.event_name != 'pull_request' }} |
| 81 | + uses: actions/upload-artifact@v3 |
| 82 | + with: |
| 83 | + name: digests |
| 84 | + path: /tmp/digests/* |
| 85 | + if-no-files-found: error |
| 86 | + retention-days: 1 |
| 87 | + |
| 88 | + merge: |
| 89 | + runs-on: ubuntu-latest |
| 90 | + if: ${{ github.event_name != 'pull_request' }} |
| 91 | + needs: |
| 92 | + - build |
| 93 | + permissions: |
| 94 | + packages: write |
| 95 | + steps: |
| 96 | + - name: Download digests |
| 97 | + uses: actions/download-artifact@v3 |
| 98 | + with: |
| 99 | + name: digests |
| 100 | + path: /tmp/digests |
| 101 | + - name: Set up Docker Buildx |
| 102 | + uses: docker/setup-buildx-action@v3 |
| 103 | + - name: Docker meta |
| 104 | + id: meta |
| 105 | + uses: docker/metadata-action@v5 |
| 106 | + with: |
| 107 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 108 | + - name: Log into registry ${{ env.REGISTRY }} |
| 109 | + if: github.event_name != 'pull_request' |
| 110 | + |
| 111 | + with: |
| 112 | + registry: ${{ env.REGISTRY }} |
| 113 | + username: ${{ github.actor }} |
| 114 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 115 | + - name: Create manifest list and push |
| 116 | + working-directory: /tmp/digests |
| 117 | + run: | |
| 118 | + docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \ |
| 119 | + $(printf '${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@sha256:%s ' *) |
| 120 | + - name: Inspect image |
| 121 | + run: | |
| 122 | + docker buildx imagetools inspect ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.version }} |
0 commit comments