Skip to content

Manual Docker Build and Push #1

Manual Docker Build and Push

Manual Docker Build and Push #1

name: Manual Docker Build and Push
on:
workflow_dispatch: # Allows manual triggering
# Add permissions for pushing packages and OIDC token
permissions:
contents: read
packages: write # Needed to push container images
id-token: write # Needed for signing/attestations
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
# Install the cosign tool
# https://github.com/sigstore/cosign-installer
- name: Install cosign
uses: sigstore/cosign-installer@v3.5.0
with:
cosign-release: 'v2.2.4'
# Setup Docker buildx
# https://github.com/docker/build-push-action/issues/461
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v3
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5
with:
images: sethblack/python-seo-analyzer # Your Docker Hub image
tags: |
# Add timestamp tag YYYYMMDDHHMM (using HH for 24hr, mm for minutes)
type=timestamp,format=yyyyMMddHHmm
# Explicitly add the 'latest' tag for all manual runs
type=raw,value=latest,enable=true
# Build and push Docker image with attestation
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push # Add id to reference outputs
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }} # Use tags from metadata
labels: ${{ steps.meta.outputs.labels }} # Use labels from metadata
# Attestations for provenance and SBOM
# Correct format: type=<type>,<key>=<value>
attests: |
type=provenance,builder-id=${{ github.workflow }}/${{ github.job_id }}
type=sbom,scan-mode=local,scan-args=--exclude=./tests
# Sign the resulting Docker image digest.
# https://github.com/sigstore/cosign
- name: Sign the published Docker image
env:
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-repository-for-the-build
COSIGN_EXPERIMENTAL: "true"
# This step uses the identity token to provision an ephemeral certificate
# against the sigstore community Fulcio instance.
run: echo "${{ steps.meta.outputs.tags }}" | xargs -I {} cosign sign --yes {}@${{ steps.build-and-push.outputs.digest }}