Skip to content

Commit 1556610

Browse files
committed
feat(workflows): add build and test workflow for Docker image creation and testing
1 parent 317527f commit 1556610

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

.github/workflows/build-test.yml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
name: Build and Test
2+
3+
on:
4+
push:
5+
branches:
6+
- 'main'
7+
- 'dev'
8+
9+
permissions:
10+
contents: read
11+
packages: write
12+
13+
jobs:
14+
build-test:
15+
name: Build, Test & Push Image
16+
runs-on: ubuntu-latest
17+
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v4
21+
22+
- name: Set up Go
23+
uses: actions/setup-go@v5
24+
with:
25+
go-version: '1.21'
26+
cache: true
27+
28+
- name: Build
29+
run: go build -v ./...
30+
31+
- name: Test
32+
run: go test -v ./...
33+
34+
- name: Extract branch name
35+
id: branch
36+
run: |
37+
BRANCH_NAME=${GITHUB_REF#refs/heads/}
38+
echo "name=${BRANCH_NAME}" >> $GITHUB_OUTPUT
39+
40+
- name: Docker meta
41+
id: meta
42+
uses: docker/metadata-action@v5
43+
with:
44+
images: ghcr.io/${{ github.repository_owner }}/api_gateway
45+
tags: |
46+
type=raw,value=${{ steps.branch.outputs.name }}-{{sha}},enable=true
47+
type=raw,value=latest,enable={{is_default_branch}}
48+
49+
- name: Log in to GHCR
50+
uses: docker/login-action@v3
51+
with:
52+
registry: ghcr.io
53+
username: ${{ github.actor }}
54+
password: ${{ secrets.GITHUB_TOKEN }}
55+
56+
- name: Build and push Docker image
57+
uses: docker/build-push-action@v5
58+
with:
59+
context: .
60+
push: true
61+
tags: ${{ steps.meta.outputs.tags }}
62+
labels: ${{ steps.meta.outputs.labels }}
63+
64+
- name: Image Summary
65+
run: |
66+
echo "### 🐳 Docker Image Built" >> $GITHUB_STEP_SUMMARY
67+
echo "**Tags pushed:**" >> $GITHUB_STEP_SUMMARY
68+
echo '```' >> $GITHUB_STEP_SUMMARY
69+
echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY
70+
echo '```' >> $GITHUB_STEP_SUMMARY
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Update K8s Manifest
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Build and Test"]
6+
types: [completed]
7+
branches: ['main', 'dev']
8+
9+
jobs:
10+
update-manifest:
11+
name: Update Image Tag in k8s-config
12+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
13+
runs-on: ubuntu-latest
14+
15+
steps:
16+
- name: Get branch and SHA info
17+
id: info
18+
run: |
19+
BRANCH="${{ github.event.workflow_run.head_branch }}"
20+
SHORT_SHA="$(echo ${{ github.event.workflow_run.head_sha }} | cut -c1-7)"
21+
echo "branch=${BRANCH}" >> $GITHUB_OUTPUT
22+
echo "sha=${SHORT_SHA}" >> $GITHUB_OUTPUT
23+
echo "📍 Branch: ${BRANCH}, SHA: ${SHORT_SHA}"
24+
25+
- name: Checkout k8s-config repo
26+
uses: actions/checkout@v4
27+
with:
28+
repository: 'CSO2/Infrastructure'
29+
token: ${{ secrets.REPO_ACCESS_TOKEN }}
30+
ref: ${{ steps.info.outputs.branch }}
31+
path: 'Infrastructure'
32+
33+
- name: Install Kustomize
34+
run: |
35+
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
36+
sudo mv kustomize /usr/local/bin/
37+
38+
- name: Update image tag in Kustomize overlay
39+
env:
40+
SERVICE_NAME_KUSTOMIZE: "cso2/api-gateway"
41+
SERVICE_NAME_GHCR: "api_gateway"
42+
BRANCH: ${{ steps.info.outputs.branch }}
43+
run: |
44+
cd Infrastructure
45+
46+
if [[ "${BRANCH}" == "main" ]]; then
47+
OVERLAY="prod"
48+
else
49+
OVERLAY="dev"
50+
fi
51+
52+
echo "🎯 Targeting overlay: ${OVERLAY}"
53+
cd cso2/k8s/overlays/${OVERLAY}
54+
55+
# Force lowercase repo owner for Docker compatibility
56+
OWNER="${{ github.repository_owner }}"
57+
OWNER="${OWNER,,}"
58+
59+
NEW_IMAGE="ghcr.io/${OWNER}/${SERVICE_NAME_GHCR}:${{ steps.info.outputs.branch }}-${{ steps.info.outputs.sha }}"
60+
61+
echo "🔄 Updating ${SERVICE_NAME_KUSTOMIZE} to use image: ${NEW_IMAGE}"
62+
63+
kustomize edit set image ${SERVICE_NAME_KUSTOMIZE}=${NEW_IMAGE}
64+
65+
echo "✅ Updated kustomization.yaml:"
66+
cat kustomization.yaml | grep ${SERVICE_NAME_KUSTOMIZE} -A 2
67+
68+
- name: Commit and push changes
69+
env:
70+
SERVICE_NAME: "api_gateway"
71+
run: |
72+
cd Infrastructure
73+
git config user.name "github-actions[bot]"
74+
git config user.email "github-actions[bot]@users.noreply.github.com"
75+
76+
git add cso2/k8s/overlays/
77+
78+
if git diff --cached --quiet; then
79+
echo "⚠️ No changes detected, skipping commit"
80+
exit 0
81+
fi
82+
83+
git commit -m "chore(${SERVICE_NAME}): update image to ${{ steps.info.outputs.branch }}-${{ steps.info.outputs.sha }}" \
84+
-m "Triggered by: ${{ github.event.workflow_run.html_url }}"
85+
86+
git push origin ${{ steps.info.outputs.branch }}

0 commit comments

Comments
 (0)