Skip to content

Commit

Permalink
Merge pull request #174 from joshuafolkken/173-fix-auto-tag-workflow
Browse files Browse the repository at this point in the history
fix auto tag workflow #173
  • Loading branch information
joshuafolkken authored Sep 27, 2024
2 parents 67a7028 + fe8d4b9 commit a4ee08a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 46 deletions.
90 changes: 58 additions & 32 deletions .github/scripts/auto_tag.sh
Original file line number Diff line number Diff line change
@@ -1,34 +1,60 @@
#!/bin/bash

# Set Git configuration
git config --global user.name "$GITHUB_ACTOR"
git config --global user.email "$GITHUB_ACTOR@users.noreply.github.com"

# Move to the project root
cd "$(git rev-parse --show-toplevel)" || exit

# Extract version from project.godot
VERSION=v$(grep 'config/version=' project.godot | cut -d'=' -f2 | tr -d '"')
echo "VERSION: $VERSION"

# Get the latest tag
LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0")
echo "LATEST_TAG: $LATEST_TAG"

# Create a new tag if the version has changed
if [ "$VERSION" = "$LATEST_TAG" ]; then
echo "Version unchanged. No new tag will be created."
exit 0
fi

if ! git tag -a "$VERSION" -m "Release $VERSION"; then
echo "Failed to create tag $VERSION"
exit 0
fi

if ! git push origin "$VERSION"; then
echo "Failed to push tag $VERSION"
exit 0
fi

echo "Created and pushed new tag $VERSION"
set -euo pipefail

configure_git() {
git config --global user.name "$GITHUB_ACTOR"
git config --global user.email "$GITHUB_ACTOR@users.noreply.github.com"
}

check_project_file() {
if [ ! -f "project.godot" ]; then
echo "Error: project.godot file not found" >&2
exit 2
fi
}

get_version() {
sed -n 's/^config\/version="\(.*\)"/\1/p' project.godot
}

get_latest_tag() {
git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0"
}

create_and_push_tag() {
local version=$1
if ! git tag -a "$version" -m "Release $version"; then
echo "Failed to create tag $version" >&2
exit 4
fi

if ! git push origin "$version"; then
echo "Failed to push tag $version" >&2
exit 5
fi

echo "Created and pushed new tag $version"
}

main() {
configure_git
check_project_file

cd "$(git rev-parse --show-toplevel)" || exit 1

VERSION="v$(get_version)"
echo "VERSION: $VERSION"

LATEST_TAG=$(get_latest_tag)
echo "LATEST_TAG: $LATEST_TAG"

if [ "$VERSION" = "$LATEST_TAG" ]; then
echo "No version change. No new tag will be created."
exit 3
fi

create_and_push_tag "$VERSION"
}

main
20 changes: 17 additions & 3 deletions .github/workflows/auto-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,33 @@ name: Auto Tag on Version Change
on:
push:
branches: [main]
paths: [project.godot]

jobs:
auto-tag:
runs-on: ubuntu-latest
permissions:
contents: write
outputs:
tag_created: ${{ steps.auto_tag.outputs.tag_created }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Run Auto Tag script
id: auto_tag
run: |
chmod +x .github/scripts/auto_tag.sh
.github/scripts/auto_tag.sh
tag_created=$(.github/scripts/auto_tag.sh && echo true || echo false)
echo "tag_created=$tag_created" >> $GITHUB_OUTPUT
- name: Print tag creation status
run: |
echo "Tag created: ${{ steps.auto_tag.outputs.tag_created }}"
- name: Trigger next workflow
if: steps.auto_tag.outputs.tag_created == 'true'
uses: peter-evans/repository-dispatch@v2
with:
event-type: new-tag-created
19 changes: 9 additions & 10 deletions .github/workflows/deploy-web.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
name: Deploy to GitHub Pages

on:
push:
tags:
- "v*.*.*"
workflow_run:
workflows: ["Auto Tag on Version Change"]
types:
- completed
repository_dispatch:
types: [new-tag-created]
# push:
# tags: [v*.*.*]

jobs:
build-and-deploy:
Expand All @@ -23,10 +20,12 @@ jobs:
with:
fetch-depth: 0

- name: Validate tag on main branch
- name: Check current branch
run: |
if ! git branch --contains ${{ github.ref_name }} | grep -q "main"; then
echo "Tag must be on the main branch"
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
echo "Current branch: $CURRENT_BRANCH"
if [ "$CURRENT_BRANCH" != "main" ]; then
echo "Error: Workflow must run on the main branch"
exit 1
fi
Expand Down
2 changes: 1 addition & 1 deletion project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ config_version=5
[application]

config/name="Tic Tac Toe"
config/version="0.54.2"
config/version="0.54.3"
run/main_scene="res://scenes/main.tscn"
config/features=PackedStringArray("4.3", "GL Compatibility")
config/icon="res://icon.svg"
Expand Down

0 comments on commit a4ee08a

Please sign in to comment.