-
Notifications
You must be signed in to change notification settings - Fork 0
233 lines (190 loc) · 7.15 KB
/
cd.yaml
File metadata and controls
233 lines (190 loc) · 7.15 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
name: Feature Flags CD Pipeline
on:
push:
branches:
- 'dev'
- 'staging'
- 'main'
workflow_dispatch:
jobs:
tests:
uses: ./.github/workflows/reusable-tests.yaml
secrets: inherit
version:
needs: [tests]
permissions:
contents: write
runs-on: ubuntu-latest
outputs:
image_tag: ${{ steps.set_tag.outputs.tag }}
steps:
- name: Check out code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for tags - required for version calculation
- name: Bump version and push tag(Main branch only)
if: github.ref == 'refs/heads/main'
id: version
uses: anothrNick/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
INITIAL_VERSION: "1.0.0"
DEFAULT_BUMP: "patch"
FORCE_WITHOUT_CHANGES: "true"
WITH_V: true
- name: Calculate Image Tag
id: set_tag
run: |
SHORT_SHA=$(git rev-parse --short HEAD)
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
echo "tag=${{ steps.version.outputs.new_tag }}" >> $GITHUB_OUTPUT
elif [[ "${{ github.ref }}" == "refs/heads/staging" ]]; then
echo "tag=rc-${SHORT_SHA}" >> $GITHUB_OUTPUT
else
echo "tag=dev-${SHORT_SHA}" >> $GITHUB_OUTPUT
fi
publish:
env:
ECR_REGISTRY: ${{ vars.ECR_REGISTRY }}
ECR_REPO: ${{ vars.ECR_REPO }}
GHCR: ghcr.io/${{ github.repository_owner }}/${{ github.event.repository.name }}
AWS_REGION: ${{ vars.AWS_REGION }}
TAG: ${{ needs.version.outputs.image_tag }}
needs: version
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/dev' || github.ref == 'refs/heads/staging')
runs-on: ubuntu-latest
permissions:
packages: write
id-token: write # for OIDC
contents: read
timeout-minutes: 5
steps:
- name: Check out code
uses: actions/checkout@v4
## Commented out artifact step due to free tier storage limits,
## Left here for demonstration purposes
# - name: Download image artifact
# uses: actions/download-artifact@v4
# with:
# name: docker-image
# - name: Load Docker image
# run: docker load -i feature-flags-api.tar
- name: Configure AWS credentials using OIDC
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.OIDC_AWS_ROLE_ARN }}
aws-region: ${{ env.AWS_REGION }}
- name: Authenticate Docker to ECR
run: aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REGISTRY
- name: Login to ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build and Tag Docker image for ECR & GHCR
run: |
set -euo pipefail
docker compose -f docker-compose.local.yaml build && \
docker tag feature-flags-api:latest feature-flags-api:$TAG
# - name: Tag Docker image for ECR
# run: |
# docker tag feature-flags-api:$TAG $ECR_REGISTRY/$ECR_REPO:$TAG
# if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
# docker tag feature-flags-api:$TAG $ECR_REGISTRY/$ECR_REPO:latest
# fi
# - name: Push Docker image to ECR
# run: |
# docker push $ECR_REGISTRY/$ECR_REPO:$TAG
# ## push latest tag only if on main branch
# if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
# docker push $ECR_REGISTRY/$ECR_REPO:latest
# fi
- name: Login to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Tag for GHCR
run: |
docker tag feature-flags-api:$TAG $GHCR/feature-flags-api:$TAG
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
docker tag feature-flags-api:$TAG $GHCR/feature-flags-api:latest
fi
- name: Push to GHCR
run: |
docker push $GHCR/feature-flags-api:$TAG
if [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
docker push $GHCR/feature-flags-api:latest
fi
image-update-gitops:
runs-on: ubuntu-latest
needs: [version, publish]
steps:
- name: Install yq
uses: dcarbone/[email protected]
with:
version: 'v4.40.5'
- name: Checkout Resources Repo
uses: actions/checkout@v4
with:
repository: shaarron/feature-flags-resources
token: ${{ secrets.API_TOKEN_GITHUB }}
ref: main
- name: Update Helm values
env:
NEW_TAG: ${{ needs.version.outputs.image_tag }}
BRANCH_NAME: ${{ github.ref_name }}
GH_TOKEN: ${{ secrets.API_TOKEN_GITHUB }}
run: |
git config user.name "GitHub Actions Bot"
git config user.email "github-actions[bot]@users.noreply.github.com"
case "$BRANCH_NAME" in
"dev")
TARGET_ENV="dev"
;;
"staging")
TARGET_ENV="staging"
;;
"main")
TARGET_ENV="prod"
;;
*)
echo "Branch $BRANCH_NAME is not mapped to a GitOps environment. Skipping."
exit 0
;;
esac
TARGET_FILE="argocd/environments/$TARGET_ENV/values.yaml"
if [[ ! -f "$TARGET_FILE" ]]; then
echo "Target file $TARGET_FILE not found."
echo "Skipping update for $TARGET_ENV."
exit 0
fi
echo "Updating $TARGET_FILE with tag: $NEW_TAG"
if [[ "$TARGET_ENV" == "prod" ]]; then
# === PROD FLOW: CREATE PULL REQUEST ===
echo "Detected Production. Starting Pull Request flow..."
PR_BRANCH="release/prod-$NEW_TAG"
git checkout -b $PR_BRANCH
yq -i '.image.tag = env(NEW_TAG)' $TARGET_FILE
if [[ -z $(git status -s) ]]; then
echo "No changes detected. Image tag is already up to date."
exit 0
fi
git commit -am "chore(prod): promote $NEW_TAG"
git push origin $PR_BRANCH
gh pr create \
--title "Promote $NEW_TAG to Production" \
--body "Automated update of image tag $NEW_TAG triggered by pipeline." \
--base main \
--head $PR_BRANCH
else
# === DEV/STAGING FLOW: DIRECT COMMIT ===
echo "Detected $TARGET_ENV. Starting Direct Commit flow..."
yq -i '.image.tag = env(NEW_TAG)' $TARGET_FILE
if [[ -n $(git status -s) ]]; then
git add $TARGET_FILE
git commit -m "chore($TARGET_ENV): update image tag to $NEW_TAG"
git push origin main
else
echo "No changes needed."
fi
fi