diff --git a/.github/scripts/auto_tag.sh b/.github/scripts/auto_tag.sh index 0a2c224..441ecae 100755 --- a/.github/scripts/auto_tag.sh +++ b/.github/scripts/auto_tag.sh @@ -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 diff --git a/.github/workflows/auto-tag.yml b/.github/workflows/auto-tag.yml index 8770629..b75d25b 100644 --- a/.github/workflows/auto-tag.yml +++ b/.github/workflows/auto-tag.yml @@ -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 diff --git a/.github/workflows/deploy-web.yml b/.github/workflows/deploy-web.yml index 4b43d83..2635e49 100644 --- a/.github/workflows/deploy-web.yml +++ b/.github/workflows/deploy-web.yml @@ -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: @@ -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 diff --git a/project.godot b/project.godot index b8dff95..c3474b9 100644 --- a/project.godot +++ b/project.godot @@ -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"