Skip to content

feat: add custom tag name follow commit number #5

feat: add custom tag name follow commit number

feat: add custom tag name follow commit number #5

name: CI - Build and Push Docker Image
on:
workflow_dispatch:
push:
branches:
- main
tags:
- "v*"
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to the Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Repository to lowercase and set IMAGE_NAME
env:
REPOSITORY: ${{ github.repository }}
run: |
echo "IMAGE_NAME=ghcr.io/${REPOSITORY,,}" >> "$GITHUB_ENV"
- name: Gather tags name
id: tags
env:
IMAGE_NAME: ${{ env.IMAGE_NAME }}
SHA_SHORT: ${{ github.sha }}
REF: ${{ github.ref }}
RUN_NUMBER: ${{ github.run_number }}
run: |
CUSTOM_VERSION_TAG="1.0.${RUN_NUMBER}"
TAG_LIST="$IMAGE_NAME:$CUSTOM_VERSION_TAG"
if [[ "$REF" == "refs/heads/main" ]]; then
TAG_LIST="${TAG_LIST},$IMAGE_NAME:latest"
fi
TAG_LIST="${TAG_LIST},$IMAGE_NAME:$(echo $SHA_SHORT | cut -c1-7)"
case "$REF" in
refs/tags/v*)
GIT_TAG_VERSION="${REF#refs/tags/}"
TAG_LIST="${TAG_LIST},$IMAGE_NAME:${GIT_TAG_VERSION}"
;;
esac
echo "Generated tags: $TAG_LIST"
echo "tags=$TAG_LIST" >> "$GITHUB_OUTPUT"
- name: Build and Push Docker Image
uses: docker/build-push-action@v3
with:
context: .
file: Dockerfile
push: true
tags: ${{ steps.tags.outputs.tags }}
cache-from: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache
cache-to: type=registry,ref=${{ env.IMAGE_NAME }}:buildcache,mode=max