Automate versioning with GitHub actions #8
Workflow file for this run
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: 🏷️ Auto Versioning | |
on: | |
pull_request: | |
types: | |
- labeled # Runs when labels are added to the PR | |
jobs: | |
versioning: | |
name: Auto Versioning | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 # Fetch full history to enable merging | |
- name: Set up Git | |
run: | | |
git config --global user.name "github-actions[bot]" | |
git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
- name: Get PR Labels | |
id: labels | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const labels = context.payload.pull_request.labels.map(label => label.name); | |
console.log("PR Labels:", labels); | |
return labels; | |
- name: Check if Version Label Exists | |
id: check_version_label | |
run: | | |
if echo "${{ steps.labels.outputs.result }}" | grep -Eq "version:(major|minor|patch)"; then | |
echo "run_versioning=true" >> $GITHUB_ENV | |
else | |
echo "::notice::No versioning label found. Skipping workflow." | |
exit 0 | |
fi | |
- name: Merge `dev` into PR Branch | |
if: env.run_versioning == 'true' | |
run: | | |
git fetch origin dev | |
git checkout ${{ github.head_ref }} | |
if git merge --no-edit origin/dev; then | |
echo "Merge successful." | |
else | |
echo "::error::Merge conflict detected! Resolve conflicts manually." | |
exit 1 | |
fi | |
continue-on-error: false | |
- name: Get Current Version from `dev` | |
if: env.run_versioning == 'true' | |
run: | | |
git checkout origin/dev -- CMakeLists.txt | |
VERSION=$(sed -nE 's/project\(atta VERSION ([0-9]+\.[0-9]+\.[0-9]+).*/\1/p' CMakeLists.txt) | |
if [[ -z "$VERSION" ]]; then | |
echo "::error::Failed to extract version from CMakeLists.txt" | |
exit 1 | |
fi | |
echo "::notice::Current Version (from dev): $VERSION" | |
echo "VERSION=$VERSION" >> $GITHUB_ENV | |
- name: Determine Version Increment | |
if: env.run_versioning == 'true' | |
run: | | |
echo "::notice::PR Labels Found: ${{ steps.labels.outputs.result }}" | |
if echo "${{ steps.labels.outputs.result }}" | grep -q "version:major"; then | |
echo "increment=major" >> $GITHUB_ENV | |
echo "::notice::Version Increment: major" | |
elif echo "${{ steps.labels.outputs.result }}" | grep -q "version:minor"; then | |
echo "increment=minor" >> $GITHUB_ENV | |
echo "::notice::Version Increment: minor" | |
else | |
echo "increment=patch" >> $GITHUB_ENV | |
echo "::notice::Version Increment: patch" | |
fi | |
- name: Increment Version | |
if: env.run_versioning == 'true' | |
run: | | |
IFS='.' read -r major minor patch <<< "$VERSION" | |
case "$increment" in | |
major) major=$((major + 1)); minor=0; patch=0 ;; | |
minor) minor=$((minor + 1)); patch=0 ;; | |
patch) patch=$((patch + 1)) ;; | |
esac | |
NEW_VERSION="$major.$minor.$patch" | |
echo "::notice::New Version: $NEW_VERSION" | |
sed -i -E "s/(project\(atta VERSION) [0-9]+\.[0-9]+\.[0-9]+/\1 $NEW_VERSION/" CMakeLists.txt | |
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV | |
- name: Commit Updated Version (If Changed) | |
if: env.run_versioning == 'true' | |
run: | | |
git add CMakeLists.txt | |
if git diff --staged --quiet; then | |
echo "No changes to commit." | |
else | |
git commit -m "Chore: Bump version to $NEW_VERSION" | |
git push origin ${{ github.head_ref }} | |
fi | |
- name: Delete Old Tag (If Exists) | |
if: env.run_versioning == 'true' | |
run: | | |
if git rev-parse "v$NEW_VERSION" >/dev/null 2>&1; then | |
git push --delete origin "v$NEW_VERSION" | |
git tag -d "v$NEW_VERSION" | |
echo "Deleted existing tag v$NEW_VERSION." | |
else | |
echo "No existing tag v$NEW_VERSION found." | |
fi | |
- name: Create and Push New Git Tag | |
if: env.run_versioning == 'true' | |
run: | | |
# Fetch the PR title from GitHub event payload | |
PR_TITLE="${{ github.event.pull_request.title }}" | |
echo "::notice::PR Title: $PR_TITLE" | |
# Create an annotated tag with the PR title as the description | |
git tag -a "v$NEW_VERSION" -m "$PR_TITLE" | |
# Push the tag to the remote repository | |
git push origin "v$NEW_VERSION" |