|
| 1 | +on: |
| 2 | + workflow_call: |
| 3 | + inputs: |
| 4 | + image: |
| 5 | + required: true |
| 6 | + type: string |
| 7 | + context: |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + dockerfile: |
| 11 | + required: false |
| 12 | + type: string |
| 13 | + default: Dockerfile |
| 14 | + secrets: |
| 15 | + registry-password: |
| 16 | + required: true |
| 17 | + |
| 18 | +permissions: |
| 19 | + contents: read |
| 20 | + packages: write |
| 21 | + id-token: write |
| 22 | + |
| 23 | +jobs: |
| 24 | + build: |
| 25 | + runs-on: ubuntu-latest |
| 26 | + permissions: |
| 27 | + contents: read |
| 28 | + packages: write |
| 29 | + id-token: write |
| 30 | + outputs: |
| 31 | + digest: ${{ steps.build-and-push.outputs.digest }} |
| 32 | + tags: ${{ steps.meta.outputs.tags }} |
| 33 | + steps: |
| 34 | + - name: Checkout |
| 35 | + uses: actions/checkout@v4 |
| 36 | + |
| 37 | + - name: Get image tag (from Dockerfile ARG VERSION) |
| 38 | + id: get_image_tag |
| 39 | + run: | |
| 40 | + if [[ "${{ github.event_name }}" == "push" && "${{ github.ref }}" == "refs/heads/main" ]]; then |
| 41 | + IMAGE_TAG=$(grep -m1 '^ARG VERSION=' "${{ inputs.context }}/${{ inputs.dockerfile }}" \ |
| 42 | + | cut -d'=' -f2 | tr -d '"' | tr -d "'" | tr -d '[:space:]') |
| 43 | + else |
| 44 | + IMAGE_TAG=$(echo ${{ github.sha }} | cut -c1-7) |
| 45 | + fi |
| 46 | + echo "Using image tag: $IMAGE_TAG" |
| 47 | + echo "IMAGE_TAG=${IMAGE_TAG}" >> $GITHUB_ENV |
| 48 | +
|
| 49 | + - name: Install cosign |
| 50 | + if: github.event_name != 'pull_request' |
| 51 | + uses: sigstore/cosign-installer@faadad0cce49287aee09b3a48701e75088a2c6ad |
| 52 | + with: |
| 53 | + cosign-release: "v2.5.3" |
| 54 | + |
| 55 | + - name: Setup Buildx |
| 56 | + uses: docker/setup-buildx-action@v2 |
| 57 | + |
| 58 | + - name: Login to registry |
| 59 | + if: github.event_name != 'pull_request' |
| 60 | + uses: docker/login-action@v2 |
| 61 | + with: |
| 62 | + registry: ghcr.io |
| 63 | + username: ${{ github.actor }} |
| 64 | + password: ${{ secrets.registry-password }} |
| 65 | + |
| 66 | + - name: Extract Docker metadata |
| 67 | + id: meta |
| 68 | + uses: docker/metadata-action@v4 |
| 69 | + with: |
| 70 | + images: ${{ inputs.image }} |
| 71 | + tags: | |
| 72 | + type=raw,value=${{ env.IMAGE_TAG }} |
| 73 | + type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }} |
| 74 | +
|
| 75 | + - name: Build and push |
| 76 | + id: build-and-push |
| 77 | + uses: docker/build-push-action@v4 |
| 78 | + with: |
| 79 | + context: ${{ inputs.context }} |
| 80 | + file: ${{ inputs.context }}/${{ inputs.dockerfile }} |
| 81 | + push: ${{ github.event_name != 'pull_request' }} |
| 82 | + tags: ${{ steps.meta.outputs.tags }} |
| 83 | + labels: ${{ steps.meta.outputs.labels }} |
| 84 | + cache-from: type=gha |
| 85 | + cache-to: type=gha,mode=max |
| 86 | + |
| 87 | + - name: Sign published image (keyless / certificate-based) |
| 88 | + if: ${{ github.event_name != 'pull_request' }} |
| 89 | + env: |
| 90 | + COSIGN_EXPERIMENTAL: 1 |
| 91 | + TAGS: ${{ steps.meta.outputs.tags }} |
| 92 | + DIGEST: ${{ steps.build-and-push.outputs.digest }} |
| 93 | + run: | |
| 94 | + echo "${TAGS}" | xargs -n1 -I {} cosign sign --yes {}@${DIGEST} |
| 95 | +
|
| 96 | + - name: Verify signatures |
| 97 | + if: ${{ github.event_name != 'pull_request' }} |
| 98 | + env: |
| 99 | + COSIGN_EXPERIMENTAL: 1 |
| 100 | + TAGS: ${{ steps.meta.outputs.tags }} |
| 101 | + DIGEST: ${{ steps.build-and-push.outputs.digest }} |
| 102 | + run: | |
| 103 | + echo "${TAGS}" | while read -r tag; do |
| 104 | + cosign verify \ |
| 105 | + --certificate-identity="https://github.com/${{ github.repository }}/.github/workflows/release-image.yml@${{ github.ref }}" \ |
| 106 | + --certificate-oidc-issuer="https://token.actions.githubusercontent.com" \ |
| 107 | + "${tag}@${DIGEST}" |
| 108 | + done |
| 109 | +
|
| 110 | + # - name: Sign the published Docker image |
| 111 | + # if: ${{ github.event_name != 'pull_request' }} |
| 112 | + # env: |
| 113 | + # TAGS: ${{ steps.meta.outputs.tags }} |
| 114 | + # DIGEST: ${{ steps.build-and-push.outputs.digest }} |
| 115 | + # run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST} |
| 116 | + # - name: Verify ghcr image signatures |
| 117 | + # if: ${{ github.event_name != 'pull_request' }} |
| 118 | + # shell: bash |
| 119 | + # env: |
| 120 | + # COSIGN_EXPERIMENTAL: 1 |
| 121 | + # TAGS: ${{ steps.meta.outputs.tags }} |
| 122 | + # DIGEST: ${{ steps.build-and-push.outputs.digest }} |
| 123 | + # run: | |
| 124 | + # echo "${TAGS}" | xargs -I {} cosign verify \ |
| 125 | + # --certificate-identity=https://github.com/${{ github.repository }}/.github/workflows/release-dogecoind.yml@${{ github.ref }} \ |
| 126 | + # --certificate-oidc-issuer=https://token.actions.githubusercontent.com \ |
| 127 | + # "{}@${DIGEST}" |
| 128 | + generate-provenance: |
| 129 | + needs: [build] |
| 130 | + if: ${{ github.event_name != 'pull_request' }} |
| 131 | + permissions: |
| 132 | + actions: read |
| 133 | + id-token: write |
| 134 | + packages: write |
| 135 | + uses: slsa-framework/slsa-github-generator/.github/workflows/generator_container_slsa3.yml@v2.1.0 |
| 136 | + with: |
| 137 | + image: ${{ inputs.image }} |
| 138 | + digest: ${{ needs.build.outputs.digest }} |
| 139 | + registry-username: ${{ github.actor }} |
| 140 | + secrets: |
| 141 | + registry-password: ${{ secrets.registry-password }} |
0 commit comments