From 55b84262b5054805a46ee5cfae320c8feb054371 Mon Sep 17 00:00:00 2001 From: overtrue Date: Thu, 17 Jul 2025 08:01:33 +0800 Subject: [PATCH] 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. --- .github/workflows/docker.yml | 71 +++++++++++++++++------------------- 1 file changed, 34 insertions(+), 37 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 93d989b4..49c24669 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -83,6 +83,8 @@ jobs: uses: actions/checkout@v4 with: 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 id: check @@ -114,51 +116,46 @@ jobs: # Use Git to generate consistent short SHA (ensures uniqueness like build.yml) short_sha=$(git rev-parse --short "${{ github.event.workflow_run.head_sha }}") - # Determine build type based on event type and git refs - # Check if this is a tag push (release build) - if [[ "${{ github.event.workflow_run.event }}" == "push" ]]; then - # Get git refs to determine if this is a tag or branch push - git_ref="${{ github.event.workflow_run.head_branch }}" + # Determine build type based on GitHub releases + # Use GitHub API to check if this commit has a release/tag + echo "๐Ÿ” Checking for releases/tags on commit ${{ github.event.workflow_run.head_sha }}" - # Check if this is a tag push by looking at the git ref - if git show-ref --tags | grep -q "${{ github.event.workflow_run.head_sha }}"; then - # 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 + # Get all tags for this repository and check if any point to this commit + GITHUB_API_URL="https://api.github.com/repos/${{ github.repository }}/tags" - 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 - else - # Regular branch push - build_type="development" - version="dev-${short_sha}" - should_build=false - echo "โญ๏ธ Skipping Docker build for development version (branch push)" - fi + # Use curl to get tags (more reliable than git in workflow_run context) + if command -v gh >/dev/null 2>&1; then + # 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 + 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 - # Regular branch push - build_type="development" - version="dev-${short_sha}" - should_build=false - echo "โญ๏ธ Skipping Docker build for development version (branch push)" + build_type="release" + create_latest=true + echo "๐Ÿš€ Building Docker image for release: $version" fi else + # No tag found, treat as development build build_type="development" version="dev-${short_sha}" 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 echo "๐Ÿ”„ Build triggered by workflow_run:"