Skip to content

Commit

Permalink
Create and update tag on every push
Browse files Browse the repository at this point in the history
  • Loading branch information
saisab29 committed Dec 12, 2024
1 parent ad43260 commit 46cb0a1
Showing 1 changed file with 49 additions and 11 deletions.
60 changes: 49 additions & 11 deletions .github/workflows/publish-ghcr.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#
name: Create and publish a Docker image to ghcr

on:
Expand All @@ -22,39 +21,78 @@ jobs:
packages: write
attestations: write
id-token: write
#

steps:
- name: Checkout repository
uses: actions/checkout@v4
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here.
with:
fetch-depth: 0 # Fetch full history for versioning

# Generate a version number based on commits
- name: Generate version
id: version
run: |
# Get the latest tag or use 0.0.0 if no tags exist
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "0.0.0")
# Count commits since the latest tag
COMMIT_COUNT=$(git rev-list --count ${LATEST_TAG}..HEAD)
# Split the latest tag into major, minor, patch
IFS='.' read -r MAJOR MINOR PATCH <<< "${LATEST_TAG#v}"
# Increment patch version
PATCH=$((PATCH + 1))
# Construct new version
NEW_VERSION="v${MAJOR}.${MINOR}.${PATCH}-${COMMIT_COUNT}"
echo "version=${NEW_VERSION}" >> $GITHUB_OUTPUT
echo "Generated version: ${NEW_VERSION}"
# Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages.
- name: Log in to the Container registry
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
# This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels.

# Extract metadata for Docker image
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
# This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages.
# It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository.
# It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step.
tags: |
type=raw,value=${{ steps.version.outputs.version }}
type=raw,value=latest
# Build and push Docker image
- name: Build and push Docker image
id: push
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# Optional: Add build arguments if needed
# build-args: |
# VERSION=${{ steps.version.outputs.version }}

# This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see "[AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds)."
# Generate artifact attestation
- 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

# Optional: Create a tag in the repository
- name: Create tag
run: |
git config user.name github-actions
git config user.email [email protected]
git tag ${{ steps.version.outputs.version }}
git push origin ${{ steps.version.outputs.version }}

0 comments on commit 46cb0a1

Please sign in to comment.