fix: use GitHub API for reliable tag detection in Docker workflow

- Replace git commands with GitHub API calls for tag detection
- Add proper commit checkout for workflow_run events
- Use gh CLI and curl fallback for better reliability
- Add debug output to help troubleshoot tag detection issues

This should fix the issue where Docker builds were not triggered for tagged releases
due to missing tag information in the workflow_run environment.
This commit is contained in:
overtrue
2025-07-17 08:01:33 +08:00
parent ce4252eb1a
commit 55b84262b5
+23 -26
View File
@@ -83,6 +83,8 @@ jobs:
uses: actions/checkout@v4 uses: actions/checkout@v4
with: with:
fetch-depth: 0 fetch-depth: 0
# For workflow_run events, checkout the specific commit that triggered the workflow
ref: ${{ github.event.workflow_run.head_sha || github.sha }}
- name: Check build conditions - name: Check build conditions
id: check id: check
@@ -114,18 +116,25 @@ 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 event type and git refs # Determine build type based on GitHub releases
# Check if this is a tag push (release build) # Use GitHub API to check if this commit has a release/tag
if [[ "${{ github.event.workflow_run.event }}" == "push" ]]; then echo "🔍 Checking for releases/tags on commit ${{ github.event.workflow_run.head_sha }}"
# Get git refs to determine if this is a tag or branch push
git_ref="${{ github.event.workflow_run.head_branch }}"
# Check if this is a tag push by looking at the git ref # Get all tags for this repository and check if any point to this commit
if git show-ref --tags | grep -q "${{ github.event.workflow_run.head_sha }}"; then GITHUB_API_URL="https://api.github.com/repos/${{ github.repository }}/tags"
# This commit has tags, extract the tag name
tag_name=$(git tag --points-at "${{ github.event.workflow_run.head_sha }}" | head -n1) # Use curl to get tags (more reliable than git in workflow_run context)
if [[ -n "$tag_name" ]]; then if command -v gh >/dev/null 2>&1; then
version="$tag_name" # Use GitHub CLI if available
tag_info=$(gh api repos/${{ github.repository }}/tags --jq '.[] | select(.commit.sha == "${{ github.event.workflow_run.head_sha }}") | .name' | head -n1)
else
# Fallback to curl
tag_info=$(curl -s "$GITHUB_API_URL" | jq -r '.[] | select(.commit.sha == "${{ github.event.workflow_run.head_sha }}") | .name' | head -n1)
fi
if [[ -n "$tag_info" ]] && [[ "$tag_info" != "null" ]]; then
# This commit has a tag, treat as release build
version="$tag_info"
# Remove 'v' prefix if present for consistent version format # Remove 'v' prefix if present for consistent version format
if [[ "$version" == v* ]]; then if [[ "$version" == v* ]]; then
version="${version#v}" version="${version#v}"
@@ -141,24 +150,12 @@ jobs:
echo "🚀 Building Docker image for release: $version" echo "🚀 Building Docker image for release: $version"
fi fi
else else
# Regular branch push # No tag found, treat as development build
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 (branch push)" echo "⏭️ Skipping Docker build for development version (no tag found)"
fi echo "🔍 Debug: Checked commit ${{ github.event.workflow_run.head_sha }} for tags"
else
# Regular branch push
build_type="development"
version="dev-${short_sha}"
should_build=false
echo "⏭️ Skipping Docker build for development version (branch push)"
fi
else
build_type="development"
version="dev-${short_sha}"
should_build=false
echo "⏭️ Skipping Docker build for development version (non-push event)"
fi fi
echo "🔄 Build triggered by workflow_run:" echo "🔄 Build triggered by workflow_run:"