Skip to content

Build, Push & Sign Docker Image #6

Build, Push & Sign Docker Image

Build, Push & Sign Docker Image #6

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 .