9
9
push_to_registry :
10
10
name : Build and push Docker image to Docker Hub
11
11
runs-on : ubuntu-22.04
12
+ env :
13
+ DOCKER_REPO : ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}
12
14
13
15
steps :
14
- - name : Check out the repo with the latest code
16
+ - name : Fetch full Git history and tags
15
17
uses : actions/checkout@v4
18
+ with :
19
+ fetch-depth : 0
20
+
21
+ - name : Get the latest tag
22
+ id : get_tag
23
+ run : |
24
+ TAG=$(git describe --tags `git rev-list --tags --max-count=1`)
25
+ echo "TAG=$TAG" >> $GITHUB_ENV
26
+ echo "::set-output name=tag::$TAG"
27
+
28
+ - name : Checkout latest tag
29
+ run : git checkout tags/${{ env.TAG }}
16
30
17
31
- name : Set up Docker Buildx
18
32
uses : docker/setup-buildx-action@v3
@@ -23,26 +37,19 @@ jobs:
23
37
username : ${{ secrets.DOCKERHUB_USERNAME }}
24
38
password : ${{ secrets.DOCKERHUB_PASSWORD }}
25
39
26
- - name : Get the current tag
27
- id : currentTag
28
- run : |
29
- git fetch --prune --unshallow
30
- TAG=$(git describe --tags --abbrev=0)
31
- echo "TAG=$TAG" >> $GITHUB_ENV
32
-
33
40
- name : Check if Docker tag exists
34
- id : tagCheck
41
+ id : tag_check
35
42
run : |
36
- REPO="${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}"
37
- TAG_EXISTS=$(curl -s -o /dev/null -w "%{http_code}" " https://hub.docker.com/v2/repositories/${REPO }/tags/${TAG}/" )
38
- echo "TAG_EXISTS =$TAG_EXISTS" >> $GITHUB_ENV
43
+ TAG_EXISTS=$(curl -s -o /dev/null -w "%{http_code}" \
44
+ https://hub.docker.com/v2/repositories/${DOCKER_REPO }/tags/${TAG}/)
45
+ echo "exists =$TAG_EXISTS" >> $GITHUB_OUTPUT
39
46
40
47
- name : Build and push Docker image
41
- if : env.TAG_EXISTS != '200'
48
+ if : steps.tag_check.outputs.exists != '200'
42
49
uses : docker/build-push-action@v6
43
50
with :
44
51
context : .
45
52
push : true
46
53
tags : |
47
- ${{ secrets.DOCKERHUB_REPOSITORY }}:latest
48
- ${{ secrets.DOCKERHUB_REPOSITORY }}:${{ env.TAG }}
54
+ ${{ env.DOCKER_REPO }}:latest
55
+ ${{ env.DOCKER_REPO }}:${{ env.TAG }}
0 commit comments