Skip to content

Create and publish a Docker image to ghcr #7

Create and publish a Docker image to ghcr

Create and publish a Docker image to ghcr #7

Workflow file for this run

name: Create and publish a Docker image to ghcr
on:
workflow_run:
workflows: ["Run Tests"]
types:
- completed
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get latest tag
id: get_latest_tag
run: |
# Get the latest tag, sorted by version
LATEST_TAG=$(git describe --tags --abbrev=0)
echo "latest_tag=${LATEST_TAG}" >> $GITHUB_OUTPUT
- name: Generate version
id: version
run: |
# Get the latest tag
LATEST_TAG=${{ steps.get_latest_tag.outputs.latest_tag }}
# Remove 'v' prefix and split version
VERSION_PARTS=($(echo "${LATEST_TAG#v}" | tr '.' ' '))
# Extract parts or default to 0
MAJOR=${VERSION_PARTS[0]:-0}
MINOR=${VERSION_PARTS[1]:-0}
PATCH=${VERSION_PARTS[2]:-0}
# Increment patch version
PATCH=$((PATCH + 1))
# Construct new version
NEW_VERSION="v${MAJOR}.${MINOR}.${PATCH}"
echo "version=${NEW_VERSION}" >> $GITHUB_OUTPUT
echo "Generated version: ${NEW_VERSION}"
- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=${{ steps.version.outputs.version }}
- name: Build and push Docker image
id: push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true