Skip to content

Docker Image CI

Docker Image CI #14

Workflow file for this run

name: Docker Image CI
on:
workflow_dispatch:
push:
branches: [main]
paths:
- 'src/**'
pull_request:
branches: [main]
paths:
- 'src/**'
jobs:
build:
runs-on: ubuntu-latest
env:
IMAGE_NAME: 'sampleapi'
TAG: ${{ github.sha }}
steps:
- name: Checkout
uses: actions/checkout@v4
# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@3
with:
cosign-release: 'v2.1.1'
# Set up BuildKit Docker container builder to be able to build
# multi-platform images and export cache
# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@3
# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ vars.DOCKER_LOGIN_SERVER }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ vars.DOCKER_LOGIN_SERVER }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@5
with:
images: ${{ vars.DOCKER_LOGIN_SERVER }}/${{ env.IMAGE_NAME }}
- name: Build Docker Image
uses: docker/build-push-action@v5
with:
context: src/SampleApi
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: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: 'docker.io/library/${{ env.IMAGE_NAME }}:${{ env.TAG }}'
format: 'github'
github-pat: ${{ github.token }}
env:
TRIVY_USERNAME: Username
TRIVY_PASSWORD: Password
# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
# repository is public to avoid leaking data. If you would like to publish
# transparency data even for private images, pass --force to cosign below.
# https://github.com/sigstore/cosign
- name: Sign the published Docker image
if: ${{ github.event_name != 'pull_request' }}
env:
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
TAGS: ${{ steps.meta.outputs.tags }}
DIGEST: ${{ steps.build-and-push.outputs.digest }}
# This step uses the identity token to provision an ephemeral certificate
# against the sigstore community Fulcio instance.
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}