1+ name : Build and Push Docker Image
2+
3+ on :
4+ push :
5+ branches :
6+ - docker-image # Configure this to your target branch
7+ pull_request :
8+ branches :
9+ - docker-image
10+ types : [closed]
11+
12+ env :
13+ DOCKER_IMAGE_NAME : elevate-entity-management # Configure your image name here
14+ DOCKER_REGISTRY : docker.io
15+ DOCKER_NAMESPACE : shikshalokamqa
16+
17+ jobs :
18+ build-and-push :
19+ if : github.event_name == 'push' || (github.event.pull_request.merged == true)
20+ runs-on : ubuntu-latest
21+
22+ steps :
23+ - name : Checkout code
24+ uses : actions/checkout@v4
25+
26+ - name : Set up Docker Buildx
27+ uses : docker/setup-buildx-action@v3
28+
29+ - name : Login to Docker Hub
30+ uses : docker/login-action@v3
31+ with :
32+ username : ${{ secrets.DOCKERHUB_USERNAME }}
33+ password : ${{ secrets.DOCKERHUB_TOKEN }}
34+
35+ - name : Fetch latest version from Docker Hub
36+ id : fetch-version
37+ run : |
38+ # Fetch tags from Docker Hub API
39+ TAGS=$(curl -s "https://hub.docker.com/v2/namespaces/${{ env.DOCKER_NAMESPACE }}/repositories/${{ env.DOCKER_IMAGE_NAME }}/tags?page_size=100" | jq -r '.results[].name')
40+ # Filter semantic version tags (e.g., 3.3.3) and sort to get the latest
41+ LATEST_TAG=$(echo "$TAGS" | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1)
42+ if [ -z "$LATEST_TAG" ]; then
43+ echo "No valid semver tag found, defaulting to 1.0.0"
44+ echo "current_version=1.0.0" >> $GITHUB_OUTPUT
45+ else
46+ echo "current_version=$LATEST_TAG" >> $GITHUB_OUTPUT
47+ fi
48+ env :
49+ DOCKERHUB_TOKEN : ${{ secrets.DOCKERHUB_TOKEN }}
50+
51+ - name : Calculate new version
52+ id : increment-version
53+ run : |
54+ CURRENT_VERSION=${{ steps.fetch-version.outputs.current_version }}
55+ # Split version into major, minor, patch
56+ IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
57+ if [ -z "$MAJOR" ] || [ -z "$MINOR" ] || [ -z "$PATCH" ]; then
58+ echo "Invalid version format, defaulting to 1.0.0"
59+ NEW_VERSION="1.0.0"
60+ else
61+ # Increment patch, or minor if patch reaches 10
62+ if [ "$PATCH" -ge 9 ]; then
63+ NEW_MINOR=$((MINOR + 1))
64+ NEW_PATCH=0
65+ else
66+ NEW_MINOR=$MINOR
67+ NEW_PATCH=$((PATCH + 1))
68+ fi
69+ NEW_VERSION="$MAJOR.$NEW_MINOR.$NEW_PATCH"
70+ fi
71+ echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
72+ echo "Calculated new version: $NEW_VERSION"
73+ shell : bash
74+
75+ - name : Check if new version exists on Docker Hub
76+ id : check-version
77+ run : |
78+ NEW_VERSION=${{ steps.increment-version.outputs.new_version }}
79+ # Check if the new version tag exists
80+ RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "https://hub.docker.com/v2/namespaces/${{ env.DOCKER_NAMESPACE }}/repositories/${{ env.DOCKER_IMAGE_NAME }}/tags/$NEW_VERSION")
81+ if [ "$RESPONSE" -eq 200 ]; then
82+ echo "Error: Tag $NEW_VERSION already exists on Docker Hub"
83+ exit 1
84+ else
85+ echo "Tag $NEW_VERSION does not exist, proceeding with build"
86+ echo "version_exists=false" >> $GITHUB_OUTPUT
87+ fi
88+ env :
89+ DOCKERHUB_TOKEN : ${{ secrets.DOCKERHUB_TOKEN }}
90+
91+ - name : Extract metadata
92+ id : meta
93+ uses : docker/metadata-action@v5
94+ with :
95+ images : ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_NAMESPACE }}/${{ env.DOCKER_IMAGE_NAME }}
96+ tags : |
97+ type=raw,value=${{ steps.increment-version.outputs.new_version }}
98+
99+ - name : Build and push Docker image
100+ id : build
101+ uses : docker/build-push-action@v5
102+ with :
103+ context : .
104+ file : ./Dockerfile
105+ push : true
106+ tags : ${{ steps.meta.outputs.tags }}
107+ labels : ${{ steps.meta.outputs.labels }}
108+ platforms : linux/amd64,linux/arm64
109+ cache-from : type=gha
110+ cache-to : type=gha,mode=max
111+
112+ - name : Image digest
113+ run : echo "Image pushed with digest ${{ steps.build.outputs.digest }}"
114+
115+ - name : Print pushed tags
116+ run : |
117+ echo "Pushed tags:"
118+ echo "${{ steps.meta.outputs.tags }}" | tr ',' '\n'
0 commit comments