99 push_to_registry :
1010 name : Build and push Docker image to Docker Hub
1111 runs-on : ubuntu-22.04
12+ env :
13+ DOCKER_REPO : ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}
1214
1315 steps :
14- - name : Check out the repo with the latest code
16+ - name : Fetch full Git history and tags
1517 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 }}
1630
1731 - name : Set up Docker Buildx
1832 uses : docker/setup-buildx-action@v3
@@ -23,26 +37,19 @@ jobs:
2337 username : ${{ secrets.DOCKERHUB_USERNAME }}
2438 password : ${{ secrets.DOCKERHUB_PASSWORD }}
2539
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-
3340 - name : Check if Docker tag exists
34- id : tagCheck
41+ id : tag_check
3542 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
3946
4047 - name : Build and push Docker image
41- if : env.TAG_EXISTS != '200'
48+ if : steps.tag_check.outputs.exists != '200'
4249 uses : docker/build-push-action@v6
4350 with :
4451 context : .
4552 push : true
4653 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