From 2501d7d24127b8af18e6b2caed3ba09438246bc0 Mon Sep 17 00:00:00 2001 From: overtrue Date: Thu, 17 Jul 2025 08:13:34 +0800 Subject: [PATCH] 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. --- .github/workflows/docker.yml | 81 +++++++++++++++++++++--------------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 49c24669..dc5fc216 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -38,7 +38,6 @@ on: workflow_run: workflows: ["Build and Release"] types: [completed] - branches: [main] # Manual trigger with same parameters for consistency workflow_dispatch: inputs: @@ -116,46 +115,62 @@ 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 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 }}" + # Determine build type based on triggering workflow event and ref + triggering_event="${{ github.event.workflow_run.event }}" + head_branch="${{ github.event.workflow_run.head_branch }}" - # 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" + echo "๐Ÿ” Analyzing triggering workflow:" + 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) - 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" + # Check if this was triggered by a tag push + if [[ "$triggering_event" == "push" ]]; then + # For tag pushes, head_branch will be like "refs/tags/v1.0.0" or just "v1.0.0" + if [[ "$head_branch" == refs/tags/* ]]; then + # Extract tag name from refs/tags/TAG_NAME + tag_name="${head_branch#refs/tags/}" + version="$tag_name" + elif [[ "$head_branch" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+ ]]; then + # Direct tag name like "v1.0.0" or "1.0.0-alpha.1" + version="$head_branch" + elif [[ "$head_branch" == "main" ]]; then + # Regular branch push to main + build_type="development" + version="dev-${short_sha}" + should_build=false + echo "โญ๏ธ Skipping Docker build for development version (main branch push)" else - build_type="release" - create_latest=true - echo "๐Ÿš€ Building Docker image for release: $version" + # Other branch push + build_type="development" + 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 else - # No tag found, treat as development build + # Non-push events build_type="development" version="dev-${short_sha}" should_build=false - echo "โญ๏ธ Skipping Docker build for development version (no tag found)" - echo "๐Ÿ” Debug: Checked commit ${{ github.event.workflow_run.head_sha }} for tags" + echo "โญ๏ธ Skipping Docker build for development version (event: $triggering_event)" fi echo "๐Ÿ”„ Build triggered by workflow_run:"