Skip to content

feat: Add labels for glance dashboard #9

feat: Add labels for glance dashboard

feat: Add labels for glance dashboard #9

name: Auto Version and Release
on:
push:
branches: [ "main" ]
jobs:
version-and-release:
runs-on: ubuntu-latest
permissions:
contents: write
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Get current version
id: current_version
run: |
VERSION=$(python3 -c "from version import get_version; print(get_version())")
echo "current_version=$VERSION" >> $GITHUB_OUTPUT
echo "Current version: $VERSION"
- name: Increment version
id: new_version
run: |
# Get the current version and increment patch number
CURRENT_VERSION="${{ steps.current_version.outputs.current_version }}"
IFS='.' read -ra VERSION_PARTS <<< "$CURRENT_VERSION"
MAJOR=${VERSION_PARTS[0]}
MINOR=${VERSION_PARTS[1]}
PATCH=${VERSION_PARTS[2]}
NEW_PATCH=$((PATCH + 1))
NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "New version: $NEW_VERSION"
- name: Update version in code
run: |
NEW_VERSION="${{ steps.new_version.outputs.new_version }}"
sed -i "s/__version__ = \".*\"/__version__ = \"$NEW_VERSION\"/" version.py
echo "Updated version.py with version $NEW_VERSION"
- name: Generate changelog
id: changelog
run: |
# Get the latest tag
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$LATEST_TAG" ]; then
echo "No previous tag found, getting all commits"
COMMITS=$(git log --oneline --pretty=format:"- %s" HEAD)
else
echo "Getting commits since $LATEST_TAG"
COMMITS=$(git log --oneline --pretty=format:"- %s" $LATEST_TAG..HEAD)
fi
# Function to enhance commit messages with GitHub links
enhance_commit_message() {
local line="$1"
local repo_url="https://github.com/${{ github.repository }}"
# Replace "Merge pull request #123" with links to PRs
line=$(echo "$line" | sed -E 's/Merge pull request #([0-9]+)/Merge pull request [#\1]('${repo_url//\//\\\/}'\/pull\/\1)/g')
# Replace "Fix #123" or "Fixes #123" with links to issues
line=$(echo "$line" | sed -E 's/(Fix|Fixes) #([0-9]+)/\1 [#\2]('${repo_url//\//\\\/}'\/issues\/\2)/g')
# Replace standalone "#123" with links to issues (using word boundaries)
line=$(echo "$line" | sed -E 's/([[:space:]]|^)#([0-9]+)([[:space:]]|[[:punct:]]|$)/\1[#\2]('${repo_url//\//\\\/}'\/issues\/\2)\3/g')
echo "$line"
}
# Create changelog with enhanced commit messages
{
echo "## Changes in v${{ steps.new_version.outputs.new_version }}"
echo ""
if [ -n "$COMMITS" ]; then
# Process each commit message to add GitHub links
echo "$COMMITS" | while IFS= read -r commit_line; do
if [ -n "$commit_line" ]; then
enhance_commit_message "$commit_line"
fi
done
else
echo "- Minor updates and improvements"
fi
} > CHANGELOG.md
# Set output for release notes
{
echo 'changelog<<EOF'
cat CHANGELOG.md
echo EOF
} >> $GITHUB_OUTPUT
- name: Commit version update
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add version.py
git commit -m "Bump version to v${{ steps.new_version.outputs.new_version }}" || echo "No changes to commit"
git pull --rebase origin main
git push
- name: Create Git Tag
run: |
git tag "v${{ steps.new_version.outputs.new_version }}"
git push origin "v${{ steps.new_version.outputs.new_version }}"
- name: Create GitHub Release
uses: ncipollo/release-action@v1
with:
tag: v${{ steps.new_version.outputs.new_version }}
name: Release v${{ steps.new_version.outputs.new_version }}
body: ${{ steps.changelog.outputs.changelog }}
draft: false
prerelease: false
token: ${{ secrets.GITHUB_TOKEN }}
- name: Log in to the GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}
tags: |
type=raw,value=latest
type=raw,value=v${{ steps.new_version.outputs.new_version }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}