-
Notifications
You must be signed in to change notification settings - Fork 0
238 lines (235 loc) · 9.23 KB
/
deploy.yml
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
233
234
235
236
237
238
name: Deploy
on: deployment
jobs:
build_job:
runs-on: ubuntu-latest
name: build dockers
if: |
github.event.deployment.environment == 'dev1' ||
github.event.deployment.environment == 'cicd' ||
github.event.deployment.environment == 'test'
steps:
- name: get environment
run: |
echo "environment is: $DEPLOY_ENVIRONMENT"
URL=''
case "$DEPLOY_ENVIRONMENT" in
*dev1*)
export URL=$DEV1_URL
export KUBE_CONFIG_DATA=$DEV1_KUBE_CONFIG_DATA
;;
*test*)
export URL=$TEST_URL
export KUBE_CONFIG_DATA=$TEST_KUBE_CONFIG_DATA
;;
*cicd*)
export URL=$CICD_URL
export KUBE_CONFIG_DATA=$CICD_KUBE_CONFIG_DATA
;;
**)
echo "unknown environment. defaulting to cicd"
export URL=$CICD_URL
export KUBE_CONFIG_DATA=$CICD_KUBE_CONFIG_DATA
;;
esac
echo URL=$URL >> $GITHUB_ENV
# echo KUBE_CONFIG_DATA=$KUBE_CONFIG_DATA >> $GITHUB_ENV
echo "$KUBE_CONFIG_DATA" | base64 --decode > /tmp/config
echo KUBECONFIG=/tmp/config >> $GITHUB_ENV
env:
DEPLOY_ENVIRONMENT: "${{ github.event.deployment.environment }}"
CICD_URL: ${{ secrets.CICD_URL }}
CICD_KUBE_CONFIG_DATA: ${{ secrets.CICD_KUBECONFIG }}
DEV1_URL: ${{ secrets.DEV1_URL }}
DEV1_KUBE_CONFIG_DATA: ${{ secrets.DEV1_KUBECONFIG }}
TEST_URL: ${{ secrets.TEST_KUBERNETES_MASTER_IP }}
TEST_KUBE_CONFIG_DATA: ${{ secrets.TEST_KUBECONFIG }}
- name: set deployment status to in progress
id: start_deployment
uses: octokit/[email protected]
with:
route: POST /repos/:repository/deployments/:deployment/statuses
repository: ${{ github.repository }}
deployment: ${{ github.event.deployment.id }}
environment: dev
environment_url: https://${{ env.URL }}/distributedkube/dashboard/
log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
state: in_progress
mediaType: '{"previews": ["flash", "ant-man"]}'
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
- name: Checkout
uses: actions/checkout@v2
- name: stam
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: |
echo "$GITHUB_CONTEXT"
env
- name: Checkout helm
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git clone "https://${{ secrets.GH_TOKEN }}@github.com/danielpickens/helm.git" "$HOME/helm"
cd $HOME/helm
git checkout ${{ github.event.deployment.ref }} || git checkout -b ${{ github.event.deployment.ref }}
git status
- uses: actions/setup-node@v2-beta
with:
node-version: 18.x
- name: Docker login
run: echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USER }} --password-stdin
- name: build changed
id: build_changed
uses: yehiyam/deploy-to-k8s@master
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"
prNumber: ${{ github.event.deployment.payload.prNumber }}
branchName: ${{ github.event.ref }}
- uses: actions/upload-artifact@v2
with:
name: values.yaml
path: $HOME/helm/distributedkube/values.yaml
- name: update helm repo
run: |
cd $HOME/helm/distributedkube
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
set +e
git diff --exit-code --quiet
if [ "$?" == 1 ]; then
set -e
git add values.yaml
git commit -m "update version [skip ci]"
git push --set-upstream origin ${{ github.event.deployment.ref }}
fi
- name: get-helm
run: |
curl -LO https://get.helm.sh/helm-v3.2.4-linux-amd64.tar.gz
tar -zxvf helm-v3.2.4-linux-amd64.tar.gz
mv linux-amd64/helm $HOME/helm3
- name: package
run: |
cd $HOME/helm/distributedkube
$HOME/helm3 package . --app-version=${{ steps.build_changed.outputs.version}} --version=${{ steps.build_changed.outputs.version}} -d /tmp/
mv /tmp/distributedkube-${{ steps.build_changed.outputs.version}}.tgz /tmp/chart.tgz
- uses: actions/upload-artifact@v2
with:
name: chart.tgz
path: /tmp/chart.tgz
- name: set values
run: |
envsubst < ${{ github.workspace }}/scripts/values-${{ github.event.deployment.environment }}-template.yml > /tmp/dev1.yml
shell: bash
env:
DOCKER_BUILD_PUSH_PASSWORD: ${{ secrets.DOCKER_BUILD_PUSH_PASSWORD }}
DOCKER_BUILD_PUSH_USERNAME: ${{ secrets.DOCKER_BUILD_PUSH_USERNAME }}
- uses: actions/upload-artifact@v2
with:
name: dev1.yml
path: /tmp/dev1.yml
- name: set deployment status to failure
id: failed_deployment
uses: octokit/[email protected]
if: failure()
with:
route: POST /repos/:repository/deployments/:deployment/statuses
repository: ${{ github.repository }}
deployment: ${{ github.event.deployment.id }}
environment: dev
environment_url: https://${{ env.URL }}/distributedkube/dashboard/
log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
mediaType: '{"previews": ["ant-man"]}'
state: failure
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
outputs:
chart: chart.tgz
values: dev1.yml
deploy:
runs-on: ubuntu-latest
needs:
- build_job
name: deploy
steps:
- name: get environment
run: |
echo "environment is: $DEPLOY_ENVIRONMENT"
URL=''
case "$DEPLOY_ENVIRONMENT" in
*dev1*)
export URL=$DEV1_URL
export KUBE_CONFIG_DATA=$DEV1_KUBE_CONFIG_DATA
;;
*test*)
export URL=$TEST_URL
export KUBE_CONFIG_DATA=$TEST_KUBE_CONFIG_DATA
;;
*cicd*)
export URL=$CICD_URL
export KUBE_CONFIG_DATA=$CICD_KUBE_CONFIG_DATA
;;
**)
echo "unknown environment. defaulting to cicd"
export URL=$CICD_URL
export KUBE_CONFIG_DATA=$CICD_KUBE_CONFIG_DATA
;;
esac
echo "set params:"
echo URL=$URL >> $GITHUB_ENV
# echo KUBE_CONFIG_DATA=$KUBE_CONFIG_DATA >> $GITHUB_ENV
echo "######## update kubeconfig: #######"
echo "$KUBE_CONFIG_DATA" | base64 --decode > /tmp/config
echo KUBECONFIG=/tmp/config >> $GITHUB_ENV
env:
DEPLOY_ENVIRONMENT: "${{ github.event.deployment.environment }}"
CICD_URL: ${{ secrets.CICD_URL }}
CICD_KUBE_CONFIG_DATA: ${{ secrets.CICD_KUBECONFIG }}
DEV1_URL: ${{ secrets.DEV1_URL }}
DEV1_KUBE_CONFIG_DATA: ${{ secrets.DEV1_KUBECONFIG }}
TEST_URL: ${{ secrets.TEST_KUBERNETES_MASTER_IP }}
TEST_KUBE_CONFIG_DATA: ${{ secrets.TEST_KUBECONFIG }}
- name: download chart
uses: actions/download-artifact@v2
with:
name: ${{ needs.build_job.outputs.chart }}
- name: download values
uses: actions/download-artifact@v2
with:
name: ${{ needs.build_job.outputs.values }}
- name: verify kubectl
run: kubectl cluster-info
- name: deploy
run: |
helm ls
helm upgrade -i distributedkube --wait -f ${{ github.workspace }}/${{ needs.build_job.outputs.values }} ${{ github.workspace }}/${{ needs.build_job.outputs.chart }}
helm ls
- name: set deployment status to success
id: successful_deployment
uses: octokit/[email protected]
with:
route: POST /repos/:repository/deployments/:deployment/statuses
repository: ${{ github.repository }}
deployment: ${{ github.event.deployment.id }}
environment: dev
environment_url: https://${{ env.URL }}/distributedkube/dashboard/
log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
mediaType: '{"previews": ["ant-man"]}'
state: success
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
- name: set deployment status to failure
id: failed_deployment
uses: octokit/[email protected]
if: failure()
with:
route: POST /repos/:repository/deployments/:deployment/statuses
repository: ${{ github.repository }}
deployment: ${{ github.event.deployment.id }}
environment: dev
environment_url: https://${{ env.URL }}/distributedkube/dashboard/
log_url: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
mediaType: '{"previews": ["ant-man"]}'
state: failure
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"