1- ---
2- name : Docker Build and Publish
1+ name : Docker Build and Publish
2+
3+ on :
4+ push :
5+ branches :
6+ - main
7+ paths :
8+ - ' api/**' # Only trigger when API code changes
9+ - ' api/VERSION'
10+ - ' Dockerfile'
11+ - ' .github/workflows/docker-publish.yml'
12+ workflow_dispatch : # Allows manual trigger
13+
14+ jobs :
15+ check-api-version :
16+ name : Check API Version
17+ runs-on : ubuntu-latest
18+ outputs :
19+ version : ${{ steps.get_version.outputs.version }}
20+ version_changed : ${{ steps.check_version.outputs.changed }}
321
4- on :
5- release :
6- types : [created]
7- workflow_dispatch : # Allows manual trigger
8-
9- jobs :
10- build-and-push :
11- name : Build and Push Docker image
12- runs-on : ubuntu-latest
13-
14- steps :
15- - name : Check out the repo
16- uses : actions/checkout@v3
17-
18- - name : Set up Docker Buildx
19- uses : docker/setup-buildx-action@v2
20-
21- - name : Log in to Docker Hub
22- uses : docker/login-action@v2
23- with :
24- username : ${{ secrets.DOCKERHUB_USERNAME }}
25- password : ${{ secrets.DOCKERHUB_TOKEN }}
22+ steps :
23+ - name : Check out the repo
24+ uses : actions/checkout@v3
25+ with :
26+ fetch-depth : 0 # Fetch all history for version comparisons
27+
28+ - name : Get current API version
29+ id : get_version
30+ run : |
31+ # Check if api/VERSION exists
32+ if [ ! -f "api/VERSION" ]; then
33+ echo "API VERSION file doesn't exist. Creating with default version."
34+ mkdir -p api
35+ echo "1.0.0" > api/VERSION
36+ fi
2637
27- - name : Extract metadata (tags, labels) for Docker
28- id : meta
29- uses : docker/metadata-action@v4
30- with :
31- images : malithrukshan/geoip-api
32- tags : |
33- type=raw,value=latest,enable=${{ github.ref ==
34- format('refs/heads/{0}', 'main') || startsWith(github.ref, 'refs/tags/') }}
35- type=semver,pattern={{version}},enable=${{
36- startsWith(github.ref, 'refs/tags/') }}
38+ # Get current version
39+ CURRENT_VERSION=$(cat api/VERSION)
40+ echo "version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
41+
42+ - name : Check for version changes
43+ id : check_version
44+ run : |
45+ # Default to changed=false
46+ echo "changed=false" >> $GITHUB_OUTPUT
3747
38- - name : Build and push Docker image
39- uses : docker/build-push-action@v4
40- with :
41- context : .
42- push : true
43- tags : ${{ steps.meta.outputs.tags }}
44- labels : ${{ steps.meta.outputs.labels }}
45- platforms : linux/amd64,linux/arm64
46- cache-from : type=gha
47- cache-to : type=gha,mode=max
48-
49- - name : Update Docker Hub Description
50- uses : peter-evans/dockerhub-description@v4
51- with :
52- username : ${{ secrets.DOCKERHUB_USERNAME }}
53- password : ${{ secrets.DOCKERHUB_TOKEN }}
54- repository : malithrukshan/geoip-api
55- short-description : >-
56- Self-hosted IP geolocation API that works completely offline!
57- readme-filepath : ./README.md
48+ # For push events, check if VERSION file changed
49+ if [[ "${{ github.event_name }}" == "push" ]]; then
50+ # Try to get previous version from git history
51+ git show HEAD~1:api/VERSION > /tmp/previous_version || echo "0.0.0" > /tmp/previous_version
52+ PREVIOUS_VERSION=$(cat /tmp/previous_version)
53+ CURRENT_VERSION=${{ steps.get_version.outputs.version }}
54+
55+ if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then
56+ echo "API version changed from $PREVIOUS_VERSION to $CURRENT_VERSION"
57+ echo "changed=true" >> $GITHUB_OUTPUT
58+ else
59+ echo "API version unchanged: $CURRENT_VERSION"
60+ fi
61+ else
62+ # For manual triggers, always consider as changed
63+ echo "Manual trigger detected, treating as version change"
64+ echo "changed=true" >> $GITHUB_OUTPUT
65+ fi
66+
67+ build-and-push :
68+ name : Build and Push Docker image
69+ needs : check-api-version
70+ if : needs.check-api-version.outputs.version_changed == 'true'
71+ runs-on : ubuntu-latest
72+
73+ steps :
74+ - name : Check out the repo
75+ uses : actions/checkout@v3
76+
77+ - name : Set up Docker Buildx
78+ uses : docker/setup-buildx-action@v2
79+
80+ - name : Log in to Docker Hub
81+ uses : docker/login-action@v2
82+ with :
83+ username : ${{ secrets.DOCKERHUB_USERNAME }}
84+ password : ${{ secrets.DOCKERHUB_TOKEN }}
85+
86+ - name : Build and push Docker image
87+ uses : docker/build-push-action@v4
88+ with :
89+ context : .
90+ push : true
91+ tags : |
92+ malithrukshan/geoip-api:${{ needs.check-api-version.outputs.version }}
93+ malithrukshan/geoip-api:latest
94+ platforms : linux/amd64,linux/arm64
95+ cache-from : type=gha
96+ cache-to : type=gha,mode=max
97+
98+ - name : Update Docker Hub Description
99+ uses : peter-evans/dockerhub-description@v4
100+ with :
101+ username : ${{ secrets.DOCKERHUB_USERNAME }}
102+ password : ${{ secrets.DOCKERHUB_TOKEN }}
103+ repository : malithrukshan/geoip-api
104+ short-description : >-
105+ Self-hosted IP geolocation API that works completely offline!
106+ readme-filepath : ./README.md
0 commit comments