github actions #14
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: 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 using environment file | |
echo "version=$NEW_VERSION" >> $GITHUB_ENV | |
id: bump_version | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
push: true | |
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.version }},${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:sha-${{ github.sha }},${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest |