|
1 | 1 | name: Build and Push Docker Image |
2 | 2 |
|
3 | 3 | on: |
4 | | - push: |
5 | | - branches: |
6 | | - - main |
7 | | - - add-docker-workflow |
8 | | - tags: ['v*'] |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + tag: |
| 7 | + description: 'Docker image tag' |
| 8 | + required: true |
| 9 | + type: string |
9 | 10 | pull_request: |
10 | 11 | branches: [main] |
11 | 12 |
|
@@ -35,51 +36,12 @@ jobs: |
35 | 36 | username: ${{ github.actor }} |
36 | 37 | password: ${{ secrets.GITHUB_TOKEN }} |
37 | 38 |
|
38 | | - - name: Extract metadata (tags, labels) |
39 | | - id: meta |
40 | | - uses: docker/metadata-action@v5.7.0 |
41 | | - with: |
42 | | - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
43 | | - tags: | |
44 | | - type=raw,value=latest,enable={{is_default_branch}} |
45 | | - type=sha,prefix={{branch}}-,format=short |
46 | | - type=semver,pattern={{version}} |
47 | | -
|
48 | | - - name: Check if image exists |
49 | | - id: check |
50 | | - if: github.event_name != 'pull_request' |
51 | | - continue-on-error: true |
52 | | - run: | |
53 | | - TAGS="${{ steps.meta.outputs.tags }}" |
54 | | - IMAGE_EXISTS=false |
55 | | -
|
56 | | - for TAG in $TAGS; do |
57 | | - echo "Checking if image exists: $TAG" |
58 | | - if docker manifest inspect $TAG > /dev/null 2>&1; then |
59 | | - echo "Image already exists: $TAG" |
60 | | - IMAGE_EXISTS=true |
61 | | - else |
62 | | - echo "Image does not exist: $TAG" |
63 | | - IMAGE_EXISTS=false |
64 | | - break |
65 | | - fi |
66 | | - done |
67 | | -
|
68 | | - echo "should_push=$([[ "$IMAGE_EXISTS" == "false" ]] && echo "true" || echo "false")" >> $GITHUB_OUTPUT |
69 | | -
|
70 | 39 | - name: Build and push Docker image |
71 | | - if: github.event_name == 'pull_request' || steps.check.outputs.should_push == 'true' |
72 | 40 | uses: docker/build-push-action@v6 |
73 | 41 | with: |
74 | 42 | context: . |
75 | 43 | push: ${{ github.event_name != 'pull_request' }} |
76 | | - tags: ${{ steps.meta.outputs.tags }} |
77 | | - labels: ${{ steps.meta.outputs.labels }} |
| 44 | + tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ inputs.tag }} |
78 | 45 | cache-from: type=gha |
79 | 46 | cache-to: type=gha,mode=max |
80 | 47 | platforms: linux/amd64,linux/arm64 |
81 | | - |
82 | | - - name: Image push skipped |
83 | | - if: github.event_name != 'pull_request' && steps.check.outputs.should_push != 'true' |
84 | | - run: | |
85 | | - echo "All image tags already exist in registry. Skipping build and push." |
0 commit comments