Skip to content

github actions

github actions #11

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 Buildx
uses: docker/setup-buildx-action@v3
- 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
echo "::set-output name=version::$NEW_VERSION"
id: bump_version
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
labels: ${{ steps.meta.outputs.labels }}
tags: |
type=raw,value=${{ steps.bump_version.outputs.version }},enable=${{ github.event_name != 'pull_request' }}
type=raw,value=${{ steps.bump_version.outputs.version }}-{{sha}},enable=${{ github.event_name != 'pull_request' }}
type=raw,value=latest,enable=${{ github.event_name != 'pull_request' }}