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
+34 -37
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,51 +116,46 @@ 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)
if [[ -n "$tag_name" ]]; then
version="$tag_name"
# 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 # Use curl to get tags (more reliable than git in workflow_run context)
build_type="prerelease" if command -v gh >/dev/null 2>&1; then
is_prerelease=true # Use GitHub CLI if available
echo "🧪 Building Docker image for prerelease: $version" tag_info=$(gh api repos/${{ github.repository }}/tags --jq '.[] | select(.commit.sha == "${{ github.event.workflow_run.head_sha }}") | .name' | head -n1)
else else
build_type="release" # Fallback to curl
create_latest=true tag_info=$(curl -s "$GITHUB_API_URL" | jq -r '.[] | select(.commit.sha == "${{ github.event.workflow_run.head_sha }}") | .name' | head -n1)
echo "🚀 Building Docker image for release: $version" fi
fi
else if [[ -n "$tag_info" ]] && [[ "$tag_info" != "null" ]]; then
# Regular branch push # This commit has a tag, treat as release build
build_type="development" version="$tag_info"
version="dev-${short_sha}" # Remove 'v' prefix if present for consistent version format
should_build=false if [[ "$version" == v* ]]; then
echo "⏭️ Skipping Docker build for development version (branch push)" version="${version#v}"
fi fi
if [[ "$version" == *"alpha"* ]] || [[ "$version" == *"beta"* ]] || [[ "$version" == *"rc"* ]]; then
build_type="prerelease"
is_prerelease=true
echo "🧪 Building Docker image for prerelease: $version"
else else
# Regular branch push build_type="release"
build_type="development" create_latest=true
version="dev-${short_sha}" echo "🚀 Building Docker image for release: $version"
should_build=false
echo "⏭️ Skipping Docker build for development version (branch push)"
fi fi
else else
# 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 (non-push event)" echo "⏭️ Skipping Docker build for development version (no tag found)"
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:"