1- # API_Gateway/.github/workflows/build.yml
2- # This workflow builds, tests, AND packages the Go API Gateway
3-
4- name : Build, Test, and Package Gateway
1+ name : Build and Push Docker Image
52
63on :
74 push :
85 branches :
96 - ' main'
10- - ' devOps'
11- - ' dev'
12- pull_request :
13- branches :
14- - ' main'
15- - ' devOps'
167 - ' dev'
178
189permissions :
1910 contents : read
20- packages : write
11+ packages : write
2112
2213jobs :
23- # --- JOB 1: Build and Test (From your file) ---
24- # This runs on all pushes AND all pull requests to verify the code
25- build-and-test :
26- name : Build and Smoke Test
14+ build-and-push :
15+ name : Build & Push Docker Image
2716 runs-on : ubuntu-latest
2817
2918 steps :
3019 - name : Checkout code
3120 uses : actions/checkout@v4
3221
33- - name : Set up Go
34- uses : actions/setup-go@v5
35- with :
36- go-version : ' 1.22'
37- cache-dependency-path : go.mod
38-
39- - name : Download dependencies
40- run : go mod download
41-
42- - name : Build Go Application
43- run : go build -v -o ./app ./cmd/gateway
44-
45- - name : Verify config.yaml exists
22+ - name : Extract branch name
23+ id : branch
4624 run : |
47- if [ ! -f config.yaml ]; then
48- echo "Error: config.yaml not found!"
49- exit 1
50- fi
51- echo "✓ config.yaml found"
25+ BRANCH_NAME=${GITHUB_REF#refs/heads/}
26+ echo "name=${BRANCH_NAME}" >> $GITHUB_OUTPUT
27+ echo "📍 Building for branch: ${BRANCH_NAME}"
5228
53- - name : Start Gateway (smoke test)
54- run : |
55- echo "Starting API Gateway with config.yaml..."
56- # We need to set a dummy secret for the smoke test
57- export JWT_SECRET="dummy-secret-for-test"
58- timeout 5s ./app || code=$?
59- if [ ${code:-0} -eq 124 ]; then
60- echo "✓ Gateway started successfully (terminated after 5s)"
61- exit 0
62- elif [ ${code:-0} -eq 0 ]; then
63- echo "✓ Gateway started and stopped cleanly"
64- exit 0
65- else
66- echo "✗ Gateway failed to start (exit code: ${code})"
67- exit 1
68- fi
69-
70- # --- JOB 2: Package as Docker Image (From my file) ---
71- # This runs ONLY after Job 1 succeeds, AND
72- # This runs ONLY on pushes to your main branches (not PRs)
73- build-and-push-docker :
74- name : Build & Push Docker Image
75- needs : build-and-test # Depends on the first job
76-
77- # This logic ensures it only runs on pushes, not PRs
78- if : github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/devOps' || github.ref == 'refs/heads/dev')
79- runs-on : ubuntu-latest
80-
81- steps :
82- - name : Checkout code
83- uses : actions/checkout@v4
84-
85- - name : Docker meta
29+ - name : Docker meta (with branch-aware tags)
8630 id : meta
8731 uses : docker/metadata-action@v5
8832 with :
89- images : ghcr.io/${{ github.repository }}
33+ images : ghcr.io/techtorque-2025/api_gateway
9034 tags : |
91- type=sha,prefix=
92- type:raw,value=latest,enable={{is_default_branch}}
35+ type=raw,value=${{ steps.branch.outputs.name }}-{{sha}},enable=true
36+ type=raw,value=latest,enable={{is_default_branch}}
37+ flavor : |
38+ latest=false
9339
9440 - name : Log in to GHCR
9541 uses : docker/login-action@v3
@@ -102,7 +48,15 @@ jobs:
10248 uses : docker/build-push-action@v5
10349 with :
10450 context : .
105- file : ./Dockerfile
10651 push : true
10752 tags : ${{ steps.meta.outputs.tags }}
108- labels : ${{ steps.meta.outputs.labels }}
53+ labels : ${{ steps.meta.outputs.labels }}
54+
55+ - name : Image Summary
56+ run : |
57+ echo "### 🐳 Docker Image Built" >> $GITHUB_STEP_SUMMARY
58+ echo "" >> $GITHUB_STEP_SUMMARY
59+ echo "**Tags pushed:**" >> $GITHUB_STEP_SUMMARY
60+ echo '```' >> $GITHUB_STEP_SUMMARY
61+ echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY
62+ echo '```' >> $GITHUB_STEP_SUMMARY
0 commit comments