|
1 | 1 | name: Publish Docker Image |
2 | 2 |
|
3 | 3 | on: |
4 | | - release: |
5 | | - types: [published] |
| 4 | + # Trigger when Release workflow completes successfully |
| 5 | + workflow_run: |
| 6 | + workflows: |
| 7 | + - Release |
| 8 | + types: |
| 9 | + - completed |
| 10 | + branches: |
| 11 | + - main |
| 12 | + |
| 13 | + # Manual trigger for existing tags or re-runs |
6 | 14 | workflow_dispatch: |
| 15 | + inputs: |
| 16 | + tag: |
| 17 | + description: 'Tag to build (e.g., v1.0.0). Uses latest tag if not specified.' |
| 18 | + required: false |
| 19 | + type: string |
7 | 20 |
|
8 | 21 | permissions: |
9 | 22 | contents: read |
|
16 | 29 | jobs: |
17 | 30 | build-and-push: |
18 | 31 | runs-on: ubuntu-latest |
| 32 | + # Only run if: |
| 33 | + # 1. Triggered by successful Release workflow, OR |
| 34 | + # 2. Manually dispatched |
| 35 | + if: | |
| 36 | + (github.event.workflow_run.conclusion == 'success') || |
| 37 | + github.event_name == 'workflow_dispatch' |
| 38 | +
|
19 | 39 | steps: |
20 | | - - uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 |
| 40 | + - name: Checkout repository |
| 41 | + uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 |
| 42 | + with: |
| 43 | + fetch-depth: 0 |
| 44 | + ref: ${{ github.event.workflow_run.head_sha || github.sha }} |
| 45 | + |
| 46 | + - name: Get tag name |
| 47 | + id: get-tag |
| 48 | + run: | |
| 49 | + # Fetch all tags to ensure we have the latest |
| 50 | + git fetch --tags --force |
| 51 | +
|
| 52 | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then |
| 53 | + # Manual trigger |
| 54 | + if [ -n "${{ inputs.tag }}" ]; then |
| 55 | + TAG="${{ inputs.tag }}" |
| 56 | + else |
| 57 | + # No tag specified - use the latest tag |
| 58 | + TAG=$(git tag -l 'v*' --sort=-v:refname | head -n1) |
| 59 | + if [ -z "$TAG" ]; then |
| 60 | + echo "Error: No tag specified and no tags found in repository" |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | + echo "No tag specified, using latest tag: $TAG" |
| 64 | + fi |
| 65 | + else |
| 66 | + # Auto trigger from workflow_run |
| 67 | + # Find tag pointing to the HEAD commit (the commit that was just released) |
| 68 | + TAG=$(git tag --points-at HEAD | grep '^v' | sort -V | tail -n1) |
| 69 | + if [ -z "$TAG" ]; then |
| 70 | + echo "No tag found on commit ${{ github.event.workflow_run.head_sha }}" |
| 71 | + echo "::notice::Skipping Docker build - no release tag found on this commit" |
| 72 | + echo "skip=true" >> $GITHUB_OUTPUT |
| 73 | + exit 0 |
| 74 | + fi |
| 75 | + fi |
| 76 | +
|
| 77 | + echo "tag=$TAG" >> $GITHUB_OUTPUT |
| 78 | + echo "skip=false" >> $GITHUB_OUTPUT |
| 79 | + echo "Docker image tag: $TAG" |
| 80 | +
|
| 81 | + # Extract version without 'v' prefix for semver tags |
| 82 | + VERSION="${TAG#v}" |
| 83 | + echo "version=$VERSION" >> $GITHUB_OUTPUT |
| 84 | +
|
| 85 | + - name: Checkout tag |
| 86 | + if: steps.get-tag.outputs.skip != 'true' |
| 87 | + uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1 |
21 | 88 | with: |
22 | | - ref: ${{ github.event.release.tag_name || github.ref }} |
| 89 | + ref: ${{ steps.get-tag.outputs.tag }} |
23 | 90 |
|
24 | 91 | - name: Set up QEMU |
| 92 | + if: steps.get-tag.outputs.skip != 'true' |
25 | 93 | uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0 |
26 | 94 |
|
27 | 95 | - name: Set up Docker Buildx |
| 96 | + if: steps.get-tag.outputs.skip != 'true' |
28 | 97 | uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0 |
29 | 98 |
|
30 | 99 | - name: Login to GitHub Container Registry |
| 100 | + if: steps.get-tag.outputs.skip != 'true' |
31 | 101 | uses: docker/login-action@5e57cd118135c172c3672efd75eb46360885c0ef # v3.6.0 |
32 | 102 | with: |
33 | 103 | registry: ${{ env.REGISTRY }} |
34 | 104 | username: ${{ github.actor }} |
35 | 105 | password: ${{ secrets.GITHUB_TOKEN }} |
36 | 106 |
|
37 | 107 | - name: Extract metadata |
| 108 | + if: steps.get-tag.outputs.skip != 'true' |
38 | 109 | id: meta |
39 | 110 | uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0 |
40 | 111 | with: |
41 | 112 | images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
42 | 113 | tags: | |
43 | | - type=semver,pattern={{version}} |
44 | | - type=semver,pattern={{major}}.{{minor}} |
45 | | - type=semver,pattern={{major}} |
| 114 | + type=semver,pattern={{version}},value=${{ steps.get-tag.outputs.tag }} |
| 115 | + type=semver,pattern={{major}}.{{minor}},value=${{ steps.get-tag.outputs.tag }} |
| 116 | + type=semver,pattern={{major}},value=${{ steps.get-tag.outputs.tag }} |
46 | 117 | type=sha,prefix= |
47 | | - type=raw,value=latest,enable=${{ github.event_name == 'release' }} |
| 118 | + type=raw,value=latest,enable=${{ github.event_name != 'workflow_dispatch' }} |
48 | 119 |
|
49 | 120 | - name: Build and push |
| 121 | + if: steps.get-tag.outputs.skip != 'true' |
50 | 122 | uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0 |
51 | 123 | with: |
52 | 124 | context: . |
|
0 commit comments