1
+ # .github/workflows/docker-build-push.yaml
2
+ name : Echo-Pod | Docker Build & Push
3
+ # This workflow builds and pushes a Docker image for the Echo-Pod application
4
+ # and updates the Kubernetes manifests with the new image version.
5
+ # It is triggered on workflow dispatch or when changes are made to the specified paths.
6
+ # The workflow calculates the new version based on the latest version in Docker Hub
7
+ # and the specified bump type (major, minor, or patch).
8
+ # The new version is then used to build and push the Docker image,
9
+
10
+ on :
11
+ workflow_dispatch :
12
+ inputs :
13
+ bump-type :
14
+ description : ' Version bump type'
15
+ required : true
16
+ default : ' minor'
17
+ type : choice
18
+ options :
19
+ - major
20
+ - minor
21
+ - patch
22
+ push :
23
+ branches :
24
+ - main
25
+ - dev
26
+ paths :
27
+ - ' dockerfiles/echo-pod/**'
28
+ - ' .github/workflows/echo-pod.yaml'
29
+
30
+ jobs :
31
+ version-calculator :
32
+ runs-on : ubuntu-latest
33
+ outputs :
34
+ new-version : ${{ steps.get-version.outputs.NEW_VERSION }}
35
+ steps :
36
+ - name : Get latest version from Docker Hub
37
+ id : get-version
38
+ run : |
39
+ # Get auth token if needed (for private repos)
40
+ # TOKEN=$(curl -s "https://auth.docker.io/token?service=registry.docker.io&scope=repository:anvesh35/echo-pod-name:pull" | jq -r .token)
41
+
42
+ # Get all tags
43
+ TAGS=$(curl -s "https://hub.docker.com/v2/repositories/anvesh35/echo-pod-name/tags/?page_size=100" | jq -r '.results[].name')
44
+
45
+ # Filter and sort semantic versions
46
+ LATEST=$(echo "$TAGS" | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -1)
47
+
48
+ # Set default if no versions found
49
+ if [ -z "$LATEST" ]; then
50
+ LATEST="v0.0.0"
51
+ fi
52
+
53
+ # Remove 'v' prefix and split version
54
+ VERSION=${LATEST#v}
55
+ IFS=. read MAJOR MINOR PATCH <<<"$VERSION"
56
+
57
+ # Determine bump type
58
+ case "${{ inputs.bump-type || 'minor' }}" in
59
+ major) MAJOR=$((MAJOR+1)); MINOR=0; PATCH=0 ;;
60
+ minor) MINOR=$((MINOR+1)); PATCH=0 ;;
61
+ patch) PATCH=$((PATCH+1)) ;;
62
+ esac
63
+
64
+ NEW_VERSION="v${MAJOR}.${MINOR}.${PATCH}"
65
+ echo "NEW_VERSION=${NEW_VERSION}" >> $GITHUB_OUTPUT
66
+
67
+ build-push-update :
68
+ needs : version-calculator
69
+ runs-on : ubuntu-latest
70
+ steps :
71
+ - name : Checkout code
72
+ uses : actions/checkout@v3
73
+
74
+ - name : Set up Docker Buildx
75
+ uses : docker/setup-buildx-action@v2
76
+
77
+ - name : Login to Docker Hub
78
+ uses : docker/login-action@v2
79
+ with :
80
+ username : ${{ secrets.DOCKERHUB_USERNAME }}
81
+ password : ${{ secrets.DOCKERHUB_TOKEN }}
82
+
83
+ - name : Build and push Docker image
84
+ uses : docker/build-push-action@v4
85
+ with :
86
+ context : fluxcd/repos/app2/src/
87
+ tags : |
88
+ anvesh35/echo-pod-name:${{ needs.version-calculator.outputs.new-version }}
89
+ anvesh35/echo-pod-name:latest
90
+ push : true
0 commit comments