-
Notifications
You must be signed in to change notification settings - Fork 1
204 lines (167 loc) · 6.1 KB
/
Copy pathdeploy.yaml
File metadata and controls
204 lines (167 loc) · 6.1 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
# deploy.yaml
# Copyright (c) 2025 Affinity7 Consulting Ltd
# SPDX-License-Identifier: MIT
#
# This workflow is licensed under the MIT License.
# See https://github.com/gitopsmanager/k8s-deploy/blob/main/LICENSE for details.
# .github/workflows/deploy.yaml
on:
workflow_call:
inputs:
environment:
required: true
type: string
application:
required: true
type: string
namespace:
required: true
type: string
repo:
required: true
type: string
repo_path:
required: true
type: string
repo_commit_id:
required: false
type: string
branch_name:
required: false
type: string
default: "main"
overlay_dir:
required: true
type: string
image_tag:
required: false
type: string
image_base_name:
required: false
type: string
image_base_names:
required: false
type: array
kustomize_version:
required: false
type: string
default: "5.0.1"
jobs:
deploy:
runs-on: ubuntu-latest
outputs:
cd_path: ${{ steps.cdpath.outputs.cd_path }}
steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
repository: ${{ inputs.repo }}
path: source
ref: ${{ inputs.repo_commit_id || inputs.branch_name }}
- name: Setup yq
uses: mikefarah/yq@v4
- name: Load environment config
id: env
run: |
ENV="${{ inputs.environment }}"
CONFIG="cd/config/env-map.yaml"
CLUSTER=$(yq ".${ENV}.cluster" "$CONFIG")
DNS_ZONE=$(yq ".${ENV}.dns_zone" "$CONFIG")
NAMESPACE=$(yq ".${ENV}.namespace" "$CONFIG")
echo "cluster=$CLUSTER" >> "$GITHUB_OUTPUT"
echo "dns_zone=$DNS_ZONE" >> "$GITHUB_OUTPUT"
echo "namespace=$NAMESPACE" >> "$GITHUB_OUTPUT"
- name: Export CD_PATH
id: cdpath
run: |
CD_PATH="continuous-deployment/${{ steps.env.outputs.cluster }}/${{ steps.env.outputs.namespace }}/${{ inputs.application }}"
echo "CD_PATH=$CD_PATH" >> "$GITHUB_ENV"
echo "cd_path=$CD_PATH" >> "$GITHUB_OUTPUT"
- name: Template manifest files with envsubst
uses: nowactions/envsubst@v1
with:
input: source/${{ inputs.source_path }}
output: source/${{ inputs.source_path }}
env:
CLUSTER_NAME: ${{ steps.env.outputs.cluster }}
DNS_ZONE: ${{ steps.env.outputs.dns_zone }}
NAMESPACE: ${{ steps.env.outputs.namespace }}
APPLICATION: ${{ inputs.application }}
- name: Checkout continuous-deployment repo
uses: actions/checkout@v3
with:
repository: ${{ github.repository_owner }}/continuous-deployment
path: cd
- name: Copy templated files to CD_PATH
run: |
mkdir -p cd/${{ env.CD_PATH }}
cp -r source/${{ inputs.source_path }}/* cd/${{ env.CD_PATH }}/
- name: Setup Kustomize
uses: imranismail/setup-kustomize@v2
with:
kustomize-version: ${{ inputs.kustomize_version }}
- name: Patch image tag(s) if provided
if: ${{ inputs.image_tag != '' }}
run: |
cd cd/${{ env.CD_PATH }}
echo "Setting image tag to ${{ inputs.image_tag }}"
if [ -n "${{ inputs.image_base_name }}" ]; then
echo "Using image_base_name: ${{ inputs.image_base_name }}"
kustomize edit set image "${{ inputs.image_base_name }}=*:${{ inputs.image_tag }}"
fi
if [ -n "${{ inputs.image_base_names }}" ]; then
for name in "${{ inputs.image_base_names[@] }}"; do
echo "Setting image tag for $name"
kustomize edit set image "$name=*:${{ inputs.image_tag }}"
done
fi
- name: Run kustomize build
id: kustomize
run: |
cd cd/${{ env.CD_PATH }}
kustomize build . > ../../../build-output.yaml
- name: Upload built manifest as artifact
uses: actions/upload-artifact@v3
with:
name: built-kustomize-manifest
path: build-output.yaml
- name: Upload templated source manifests as artifact
uses: actions/upload-artifact@v3
with:
name: templated-source-manifests
path: source/${{ inputs.source_path }}
- name: Commit and push to continuous-deployment repo
run: |
cd cd
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
git add "${{ env.CD_PATH }}"
git commit -m "Deploy ${{ inputs.application }} to ${{ steps.env.outputs.cluster }}/${{ steps.env.outputs.namespace }}"
git push
- name: Sync ArgoCD app via REST API
id: env
run: |
CLUSTER="${{ steps.env.outputs.cluster }}"
NAMESPACE="${{ steps.env.outputs.namespace }}"
DNS_ZONE="${{ steps.env.outputs.dns_zone }}"
APP_NAME="${{ inputs.application }}-${{ steps.env.outputs.cluster }}-${{ steps.env.outputs.namespace }}"
ARGOCD_URL="https://${CLUSTER}-argocd-argocd-web-ui.${DNS_ZONE}"
SYNC_URL="$ARGOCD_URL/api/v1/applications/$APP_NAME/sync"
STATUS_URL="$ARGOCD_URL/api/v1/applications/$APP_NAME"
echo "Syncing ArgoCD app $APP_NAME using $ARGOCD_URL..."
curl -X POST "$SYNC_URL" \
-H "Content-Type: application/json" \
-u "${{ secrets.ARGO_CD_ADMIN_USER }}:${{ secrets.ARGO_CD_ADMIN_PASSWORD }}" \
-d '{}'
echo "Waiting for sync to complete..."
for i in {1..12}; do
STATUS=$(curl -s -u "${{ secrets.ArgoCDAdminUser }}:${{ secrets.ArgoCDAdminPassword }}" "$STATUS_URL" | jq -r '.status.sync.status')
echo "Current sync status: $STATUS"
if [ "$STATUS" = "Synced" ]; then
echo "ArgoCD sync completed."
exit 0
fi
sleep 10
done
echo "ArgoCD sync did not complete in time."
exit 1