1- name : Docker Build and Push
1+ # Workflow to build and publish Docker images for DBHub
2+ #
3+ # This workflow:
4+ # 1. Always pushes to the 'latest' tag when changes are pushed to the main branch
5+ # 2. If package.json version changes, also pushes a version-specific tag
6+ # 3. Builds for both amd64 and arm64 architectures
7+
8+ name : Publish to docker hub
29
310on :
411 push :
1421 steps :
1522 - name : Checkout repository
1623 uses : actions/checkout@v4
24+ with :
25+ fetch-depth : 2 # Fetch two commits to detect changes in package.json
26+
27+ - name : Check for package.json version changes
28+ id : check-version
29+ run : |
30+ # Get current and previous package.json content
31+ git show HEAD:package.json > package.json.current
32+ git show HEAD~1:package.json > package.json.previous 2>/dev/null || cp package.json.current package.json.previous
33+
34+ # Extract versions
35+ CURRENT_VERSION=$(jq -r '.version' package.json.current)
36+ PREVIOUS_VERSION=$(jq -r '.version' package.json.previous)
37+
38+ echo "Current version: $CURRENT_VERSION"
39+ echo "Previous version: $PREVIOUS_VERSION"
40+
41+ # Set output based on whether version changed
42+ if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then
43+ echo "Version changed from $PREVIOUS_VERSION to $CURRENT_VERSION"
44+ echo "VERSION_CHANGED=true" >> $GITHUB_OUTPUT
45+ echo "VERSION=$CURRENT_VERSION" >> $GITHUB_OUTPUT
46+ else
47+ echo "Version unchanged: $CURRENT_VERSION"
48+ echo "VERSION_CHANGED=false" >> $GITHUB_OUTPUT
49+ fi
1750
1851 - name : Set up Docker Buildx
1952 uses : docker/setup-buildx-action@v3
@@ -24,12 +57,29 @@ jobs:
2457 username : ${{ secrets.DOCKERHUB_USERNAME }}
2558 password : ${{ secrets.DOCKERHUB_TOKEN }}
2659
60+ - name : Prepare Docker tags
61+ id : prep
62+ run : |
63+ # Always include latest tag
64+ TAGS="${{ env.IMAGE_NAME }}:latest"
65+
66+ # Add version tag if version changed
67+ if [[ "${{ steps.check-version.outputs.VERSION_CHANGED }}" == "true" ]]; then
68+ VERSION="${{ steps.check-version.outputs.VERSION }}"
69+ TAGS="$TAGS,${{ env.IMAGE_NAME }}:$VERSION"
70+ echo "Publishing with tags: latest, $VERSION"
71+ else
72+ echo "Publishing with tag: latest only"
73+ fi
74+
75+ echo "TAGS=$TAGS" >> $GITHUB_OUTPUT
76+
2777 - name : Build and push Docker image
2878 uses : docker/build-push-action@v5
2979 with :
3080 context : .
3181 push : true
3282 platforms : linux/amd64,linux/arm64
33- tags : ${{ env.IMAGE_NAME }}:latest
83+ tags : ${{ steps.prep.outputs.TAGS }}
3484 cache-from : type=gha
3585 cache-to : type=gha,mode=max
0 commit comments