Skip to content

Update build-release-sign-image-toGHCR.yml #18

Update build-release-sign-image-toGHCR.yml

Update build-release-sign-image-toGHCR.yml #18

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: Build and release and sign a Docker image to GHCR.IO
on:
push:
tags:
- 'v*'
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-push-and-sign-image:
##runs-on: ubuntu-latest
runs-on: buildjet-2vcpu-ubuntu-2004
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign and test presence in path
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@main ##f3c664df7af409cb4873aa5068053ba9d61a57b6 = v2.6.0
- name: Check install!
run: cosign version
- name: Install Syft
if: github.event_name != 'pull_request'
uses: anchore/sbom-action/download-syft@v0
# Workaround: https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v2
# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm64
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Generate SBOM
run: syft "${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build-and-push.outputs.digest }}" -o spdx-json=sbom-spdx.json
- name: Add attestation
env:
COSIGN_PRIVATE_KEY: ${{secrets.COSIGN_PRIVATE_KEY}}
run: cosign attest --predicate sbom-spdx.json --type spdx ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build-and-push.outputs.digest }} --verbose
- name: Sign image with a key
run: cosign sign -y --key env://COSIGN_PRIVATE_KEY ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build-and-push.outputs.digest }}
env:
DIGEST: ${{ steps.build-and-push.outputs.digest }}
TAGS: ${{ fromJSON(steps.meta.outputs.json).tags[0] }}
COSIGN_PRIVATE_KEY: ${{secrets.COSIGN_PRIVATE_KEY}}
COSIGN_PASSWORD: ${{secrets.COSIGN_PASSWORD}}
##cosign sign --key env://COSIGN_PRIVATE_KEY -a "author=srnfr" ${TAGS}
##cosign sign --key env://COSIGN_PRIVATE_KEY -a "author=srnfr" ${DIGEST}
# - name: Verify signature
# run: cosign verify --key env://COSIGN_PUBLIC_KEY ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}@${{ steps.build-and-push.outputs.digest }}
# env:
# COSIGN_PUBLIC_KEY: ${{secrets.COSIGN_PUBLIC_KEY}}