-
Notifications
You must be signed in to change notification settings - Fork 1
101 lines (82 loc) · 3.57 KB
/
update-manifest.yml
File metadata and controls
101 lines (82 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
name: Update K8s Manifest
on:
workflow_run:
workflows: ["Build and Test"]
types: [completed]
branches: ['main', 'dev']
jobs:
update-manifest:
name: Update Image Tag in k8s-config
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
steps:
- name: Get branch and SHA info
id: info
run: |
BRANCH="${{ github.event.workflow_run.head_branch }}"
SHORT_SHA="$(echo ${{ github.event.workflow_run.head_sha }} | cut -c1-7)"
echo "branch=${BRANCH}" >> $GITHUB_OUTPUT
echo "sha=${SHORT_SHA}" >> $GITHUB_OUTPUT
echo "📍 Branch: ${BRANCH}, SHA: ${SHORT_SHA}"
- name: Checkout k8s-config repo
uses: actions/checkout@v4
with:
repository: 'CSO2/Infrastructure'
token: ${{ secrets.REPO_ACCESS_TOKEN }}
ref: ${{ steps.info.outputs.branch }}
path: 'Infrastructure'
- name: Install Kustomize
run: |
curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash
sudo mv kustomize /usr/local/bin/
- name: Update image tag in Kustomize overlay
env:
SERVICE_NAME_KUSTOMIZE: "cso2/frontend"
SERVICE_NAME_GHCR: "frontend_web"
BRANCH: ${{ steps.info.outputs.branch }}
run: |
cd Infrastructure
# Determine overlay based on branch
if [[ "${BRANCH}" == "main" ]]; then
OVERLAY="prod"
else
OVERLAY="dev"
fi
echo "🎯 Targeting overlay: ${OVERLAY}"
cd cso2/k8s/overlays/${OVERLAY}
# Force lowercase repo owner for Docker compatibility
OWNER="${{ github.repository_owner }}"
OWNER="${OWNER,,}"
NEW_IMAGE="ghcr.io/${OWNER}/${SERVICE_NAME_GHCR}:${{ steps.info.outputs.branch }}-${{ steps.info.outputs.sha }}"
echo "🔄 Updating ${SERVICE_NAME_KUSTOMIZE} to use image: ${NEW_IMAGE}"
kustomize edit set image ${SERVICE_NAME_KUSTOMIZE}=${NEW_IMAGE}
echo "✅ Updated kustomization.yaml:"
cat kustomization.yaml | grep ${SERVICE_NAME_KUSTOMIZE} -A 2
- name: Commit and push changes
env:
SERVICE_NAME: "frontend"
run: |
cd Infrastructure
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add cso2/k8s/overlays/
if git diff --cached --quiet; then
echo "⚠️ No changes detected, skipping commit"
exit 0
fi
git commit -m "chore(${SERVICE_NAME}): update image to ${{ steps.info.outputs.branch }}-${{ steps.info.outputs.sha }}" \
-m "Triggered by: ${{ github.event.workflow_run.html_url }}"
# Retry loop for race conditions
MAX_RETRIES=5
for ((i=1; i<=MAX_RETRIES; i++)); do
echo "🔄 Attempt $i of $MAX_RETRIES to push changes..."
git pull --rebase origin ${{ steps.info.outputs.branch }}
if git push origin ${{ steps.info.outputs.branch }}; then
echo "✅ Push successful!"
exit 0
fi
echo "⚠️ Push failed, retrying in roughly 5 seconds..."
sleep $((RANDOM % 5 + 3))
done
echo "❌ Failed to push changes after $MAX_RETRIES attempts."
exit 1