|
| 1 | +name: Container Build and Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_call: |
| 5 | + inputs: |
| 6 | + version: |
| 7 | + description: "Semantic version of the image" |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + |
| 11 | + container-file: |
| 12 | + description: "Path to the Dockerfile" |
| 13 | + type: string |
| 14 | + default: "Dockerfile" |
| 15 | + |
| 16 | + container-name: |
| 17 | + description: "Name of the container" |
| 18 | + type: string |
| 19 | + default: "${{ github.repository }}" |
| 20 | + |
| 21 | + sbom: |
| 22 | + description: "Generate and upload SBOM" |
| 23 | + type: string |
| 24 | + default: "true" |
| 25 | + |
| 26 | + signing: |
| 27 | + description: "Sign the image" |
| 28 | + type: string |
| 29 | + default: "false" |
| 30 | + |
| 31 | +env: |
| 32 | + REGISTRY: ghcr.io |
| 33 | + |
| 34 | +jobs: |
| 35 | + publish-image: |
| 36 | + runs-on: ubuntu-latest |
| 37 | + |
| 38 | + permissions: |
| 39 | + # to upload SBOM |
| 40 | + id-token: write |
| 41 | + contents: write |
| 42 | + # to upload Docker image |
| 43 | + packages: write |
| 44 | + |
| 45 | + steps: |
| 46 | + - name: Checkout repository |
| 47 | + uses: actions/checkout@v4 |
| 48 | + |
| 49 | + - name: Set up Docker Buildx |
| 50 | + uses: docker/setup-buildx-action@c47758b77c9736f4b2ef4073d4d51994fabfe349 # v3.7.1 |
| 51 | + |
| 52 | + - name: Log in to the Container registry |
| 53 | + uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0 |
| 54 | + with: |
| 55 | + registry: ${{ env.REGISTRY }} |
| 56 | + username: ${{ github.actor }} |
| 57 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 58 | + |
| 59 | + - name: Set Container Metadata |
| 60 | + uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 |
| 61 | + with: |
| 62 | + images: ${{ env.REGISTRY }}/${{ inputs.container-name }} |
| 63 | + tags: | |
| 64 | + # latest / main |
| 65 | + type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }} |
| 66 | + # SemVer |
| 67 | + type=semver,pattern={{version}},value=${{ inputs.version }} |
| 68 | + type=semver,pattern=v{{version}},value=${{ inputs.version }} |
| 69 | + type=semver,pattern=v{{major}},value=${{ inputs.version }} |
| 70 | + type=semver,pattern=v{{major}}.{{minor}},value=${{ inputs.version }} |
| 71 | + |
| 72 | + - name: Build & Publish Container ${{ inputs.container-name }} |
| 73 | + uses: docker/build-push-action@4f58ea79222b3b9dc2c8bbdd6debcef730109a75 # v6.9.0 |
| 74 | + if: ${{ steps.set-version.outputs.release == 'true' }} |
| 75 | + id: build |
| 76 | + with: |
| 77 | + file: "${{ inputs.container-file }}" |
| 78 | + context: . |
| 79 | + push: true |
| 80 | + tags: ${{ steps.meta.outputs.tags }} |
| 81 | + labels: ${{ steps.meta.outputs.labels }} |
| 82 | + # SBOM Settings |
| 83 | + sbom: true |
| 84 | + |
| 85 | + # Upload Software Bill of Materials (SBOM) to GitHub |
| 86 | + - name: Upload SBOM |
| 87 | + uses: advanced-security/spdx-dependency-submission-action@5530bab9ee4bbe66420ce8280624036c77f89746 # v0.1.1 |
| 88 | + if: ${{ inputs.sbom == 'true' }} |
| 89 | + with: |
| 90 | + filePath: '.' |
| 91 | + filePattern: '*.spdx.json' |
| 92 | + |
| 93 | + sign-image: |
| 94 | + runs-on: ubuntu-latest |
| 95 | + needs: publish-image |
| 96 | + # Sign the image only if it is being published |
| 97 | + if: ${{ inputs.signing == 'true' && inputs.publish == 'true' }} |
| 98 | + |
| 99 | + permissions: |
| 100 | + # read the image from GitHub Container Registry |
| 101 | + packages: read |
| 102 | + |
| 103 | + steps: |
| 104 | + - name: Checkout repository |
| 105 | + uses: actions/checkout@v4 |
| 106 | + |
| 107 | + - uses: sigstore/cosign-installer@dc72c7d5c4d10cd6bcb8cf6e3fd625a9e5e537da # v3.7.0 |
| 108 | + with: |
| 109 | + cosign-release: 'v2.4.1' |
| 110 | + |
| 111 | + - name: Log in to the Container registry |
| 112 | + uses: docker/login-action@9780b0c442fbb1117ed29e0efdff1e18412f7567 # v3.3.0 |
| 113 | + with: |
| 114 | + registry: ${{ env.REGISTRY }} |
| 115 | + username: ${{ github.actor }} |
| 116 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 117 | + |
| 118 | + - name: Sign the published container |
| 119 | + # This step uses the identity token to provision an ephemeral certificate against |
| 120 | + # the sigstore community Fulcio instance. |
| 121 | + run: | |
| 122 | + cosign sign --yes \ |
| 123 | + ${{ env.IMAGE_NAME }}@${{ needs.build-publish-image.outputs.digest }} |
| 124 | + |
0 commit comments