Build, Push & Sign Docker Image #6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build, Push & Sign Docker Image | |
on: | |
workflow_dispatch: | |
env: | |
IMAGE: ${{secrets.DOCKER_USER}}/ubuntu-patched:$(date +%s) | |
jobs: | |
image-lifecycle: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Setup Docker | |
uses: docker/setup-buildx-action@v3 | |
- name: Build the Docker image | |
run: docker build . --file Dockerfile --tag sample-image:1.0.0 | |
- name: Login to Docker | |
run: echo ${{secrets.DOCKER_TOKEN}} | docker login -u ${{secrets.DOCKER_USER}} --password-stdin | |
- name: Tag image | |
run: docker tag sample-image:1.0.0 ${{env.IMAGE}} | |
- name: Push to Docker | |
run: docker push ${{env.IMAGE}} | |
- name: Retrieve image digest | |
run: export IMAGE_DIGEST=$(docker inspect --format='{{index .RepoDigests 0}}' ${{env.IMAGE}}) | |
- name: Setup Cosign | |
run: | | |
curl -O -L "https://github.com/sigstore/cosign/releases/latest/download/cosign-linux-amd64" | |
sudo mv cosign-linux-amd64 /usr/local/bin/cosign | |
sudo chmod +x /usr/local/bin/cosign | |
- name: Sign image using Cosign | |
run: | | |
echo ${{secrets.COSIGN_KEY}} > cosign.key | |
cosign sign --key cosign.key $IMAGE_DIGEST | |
- name: Verify image signature | |
run: | | |
apt install jq -y | |
cosign verify --key cosign.pub ${{env.IMAGE}} | jq . | |