Skip to content

github actions

github actions #17

name: Docker Build and Push
on:
push:
branches: [ "master" ]
workflow_dispatch:
inputs:
version_bump:
description: 'Version bump type (major, minor, patch)'
required: true
default: 'patch'
type: choice
options:
- major
- minor
- patch
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-push:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Docker
uses: docker-practice/actions-setup-docker@master
- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get latest tag and bump version
run: |
# Get the latest Git tag
LATEST_TAG=$(git describe --tags --abbrev=0)
# Extract the version numbers from the latest tag
MAJOR=$(echo $LATEST_TAG | cut -d'.' -f1 | cut -d'v' -f2)
MINOR=$(echo $LATEST_TAG | cut -d'.' -f2)
PATCH=$(echo $LATEST_TAG | cut -d'.' -f3)
# Determine the version bump type
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
# Use the manual input for version bump
VERSION_BUMP="${{ github.event.inputs.version_bump }}"
else
# Default to patch bump for push events
VERSION_BUMP="patch"
fi
# Increment the version based on the version bump type
if [ "$VERSION_BUMP" == "major" ]; then
NEW_VERSION="v$((MAJOR+1)).0.0"
elif [ "$VERSION_BUMP" == "minor" ]; then
NEW_VERSION="v$MAJOR.$((MINOR+1)).0"
else
NEW_VERSION="v$MAJOR.$MINOR.$((PATCH+1))"
fi
# Create a new tag with the bumped version
git tag $NEW_VERSION
git push origin $NEW_VERSION
# Set the new version as an output variable using environment file
echo "version=$NEW_VERSION" >> $GITHUB_ENV
id: bump_version
- name: Build Docker image
run: |
docker build -t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.version }} \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:sha-${GITHUB_SHA::8} \
-t ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest .
- name: Push Docker image
run: |
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.version }}
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:sha-${GITHUB_SHA::8}
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest