Create and publish a Docker image to ghcr #9
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: 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 and commit distance | |
id: get_version_info | |
run: | | |
# Get the latest tag | |
LATEST_TAG=$(git describe --tags --abbrev=0) | |
# Remove 'v' prefix from latest tag | |
BASE_VERSION=${LATEST_TAG#v} | |
# Get the number of commits since the last tag | |
COMMIT_DISTANCE=$(git rev-list --count ${LATEST_TAG}..HEAD) | |
# Construct new version with build number | |
NEW_VERSION="v${BASE_VERSION}.${COMMIT_DISTANCE}" | |
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.get_version_info.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 |