fix: remove branch restriction from Docker workflow_run trigger

The Docker workflow was not triggering for tag-based releases because it had
'branches: [main]' restriction in the workflow_run configuration. When pushing
tags, the triggering workflow runs on the tag, not on main branch.

Changes:
- Remove 'branches: [main]' from workflow_run trigger
- Simplify tag detection using github.event.workflow_run context instead of API calls
- Use official workflow_run event properties (head_branch, event) for reliable detection
- Support both 'refs/tags/VERSION' and direct 'VERSION' formats
- Add better logging for debugging workflow trigger issues

This fixes the issue where Docker images were not built for tagged releases.
This commit is contained in:
overtrue
2025-07-17 08:13:34 +08:00
parent 55b84262b5
commit 2501d7d241
+48 -33
View File
@@ -38,7 +38,6 @@ on:
workflow_run: workflow_run:
workflows: ["Build and Release"] workflows: ["Build and Release"]
types: [completed] types: [completed]
branches: [main]
# Manual trigger with same parameters for consistency # Manual trigger with same parameters for consistency
workflow_dispatch: workflow_dispatch:
inputs: inputs:
@@ -116,46 +115,62 @@ jobs:
# Use Git to generate consistent short SHA (ensures uniqueness like build.yml) # Use Git to generate consistent short SHA (ensures uniqueness like build.yml)
short_sha=$(git rev-parse --short "${{ github.event.workflow_run.head_sha }}") short_sha=$(git rev-parse --short "${{ github.event.workflow_run.head_sha }}")
# Determine build type based on GitHub releases # Determine build type based on triggering workflow event and ref
# Use GitHub API to check if this commit has a release/tag triggering_event="${{ github.event.workflow_run.event }}"
echo "🔍 Checking for releases/tags on commit ${{ github.event.workflow_run.head_sha }}" head_branch="${{ github.event.workflow_run.head_branch }}"
# Get all tags for this repository and check if any point to this commit echo "🔍 Analyzing triggering workflow:"
GITHUB_API_URL="https://api.github.com/repos/${{ github.repository }}/tags" echo " 📋 Event: $triggering_event"
echo " 🌿 Head branch: $head_branch"
echo " 📎 Head SHA: ${{ github.event.workflow_run.head_sha }}"
# Use curl to get tags (more reliable than git in workflow_run context) # Check if this was triggered by a tag push
if command -v gh >/dev/null 2>&1; then if [[ "$triggering_event" == "push" ]]; then
# Use GitHub CLI if available # For tag pushes, head_branch will be like "refs/tags/v1.0.0" or just "v1.0.0"
tag_info=$(gh api repos/${{ github.repository }}/tags --jq '.[] | select(.commit.sha == "${{ github.event.workflow_run.head_sha }}") | .name' | head -n1) if [[ "$head_branch" == refs/tags/* ]]; then
else # Extract tag name from refs/tags/TAG_NAME
# Fallback to curl tag_name="${head_branch#refs/tags/}"
tag_info=$(curl -s "$GITHUB_API_URL" | jq -r '.[] | select(.commit.sha == "${{ github.event.workflow_run.head_sha }}") | .name' | head -n1) version="$tag_name"
fi elif [[ "$head_branch" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+ ]]; then
# Direct tag name like "v1.0.0" or "1.0.0-alpha.1"
if [[ -n "$tag_info" ]] && [[ "$tag_info" != "null" ]]; then version="$head_branch"
# This commit has a tag, treat as release build elif [[ "$head_branch" == "main" ]]; then
version="$tag_info" # Regular branch push to main
# Remove 'v' prefix if present for consistent version format build_type="development"
if [[ "$version" == v* ]]; then version="dev-${short_sha}"
version="${version#v}" should_build=false
fi echo "⏭️ Skipping Docker build for development version (main branch push)"
if [[ "$version" == *"alpha"* ]] || [[ "$version" == *"beta"* ]] || [[ "$version" == *"rc"* ]]; then
build_type="prerelease"
is_prerelease=true
echo "🧪 Building Docker image for prerelease: $version"
else else
build_type="release" # Other branch push
create_latest=true build_type="development"
echo "🚀 Building Docker image for release: $version" version="dev-${short_sha}"
should_build=false
echo "⏭️ Skipping Docker build for development version (branch: $head_branch)"
fi
# If we extracted a version (tag), determine release type
if [[ -n "$version" ]] && [[ "$version" != "dev-${short_sha}" ]]; then
# Remove 'v' prefix if present for consistent version format
if [[ "$version" == v* ]]; then
version="${version#v}"
fi
if [[ "$version" == *"alpha"* ]] || [[ "$version" == *"beta"* ]] || [[ "$version" == *"rc"* ]]; then
build_type="prerelease"
is_prerelease=true
echo "🧪 Building Docker image for prerelease: $version"
else
build_type="release"
create_latest=true
echo "🚀 Building Docker image for release: $version"
fi
fi fi
else else
# No tag found, treat as development build # Non-push events
build_type="development" build_type="development"
version="dev-${short_sha}" version="dev-${short_sha}"
should_build=false should_build=false
echo "⏭️ Skipping Docker build for development version (no tag found)" echo "⏭️ Skipping Docker build for development version (event: $triggering_event)"
echo "🔍 Debug: Checked commit ${{ github.event.workflow_run.head_sha }} for tags"
fi fi
echo "🔄 Build triggered by workflow_run:" echo "🔄 Build triggered by workflow_run:"