-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
200 lines (184 loc) · 6.18 KB
/
.gitlab-ci.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
variables:
APPLICATION_PORT: "8080"
KUBECTL_IMAGE: "gitlab.example.com:5050/kubernetes/gitops/gitops"
KUBECTL_CONTEXT_NAME: "kubernetes/gitops:kubernetes-agent"
DEVNAMESPACE: "${GITLAB_USER_LOGIN}-${CI_COMMIT_REF_SLUG}-${CI_PROJECT_NAME}-ns"
DEVSVC: "${GITLAB_USER_LOGIN}-${CI_COMMIT_REF_SLUG}-${CI_PROJECT_NAME}-svc"
DEVDEPLOYMENT: "${GITLAB_USER_LOGIN}-${CI_COMMIT_REF_SLUG}-${CI_PROJECT_NAME}"
DEVDOMAIN: "example.com"
DEVINGRESS: "${GITLAB_USER_LOGIN}-${CI_COMMIT_REF_SLUG}-${CI_PROJECT_NAME}.${DEVDOMAIN}"
DEVINGRESSNAME: "${GITLAB_USER_LOGIN}-${CI_COMMIT_REF_SLUG}-${CI_PROJECT_NAME}-ing"
stages:
- destroy_previous_deployment
- build_app
- build_container
- tag_container
- test
- generate_manifests_pipeline
- deploy_pipeline
- test_deployment_pipeline
- destroy_deployment_pipeline
- deploy_tst_environment
- deploy_acc_environment
- deploy_prd_environment
destroy_previous_deployment:
stage: destroy_previous_deployment
image:
name: $KUBECTL_IMAGE # should be own image from gitops repo
entrypoint: ['']
script:
- kubectl config use-context ${KUBECTL_CONTEXT_NAME}
- /bin/sh -c 'if kubectl get ns | grep ${DEVNAMESPACE}; then echo "old resources found, deleting" && kubectl delete namespace ${DEVNAMESPACE}; else exit 0; fi'
tags:
- kubernetes
rules:
- if: '$CI_COMMIT_BRANCH != "develop" && $CI_COMMIT_BRANCH != "main" && $CI_PIPELINE_SOURCE != "merge_request_event" && $CI_COMMIT_TAG == null'
build_app: # This job runs in the build stage, which runs first.
stage: build_app
image: docker.io/library/golang:1.20.4-alpine3.18
script:
- go build -o dist/
- echo $CI_COMMIT_TAG | grep -q -E '^$' && go build -ldflags="-X 'main.commit=$CI_COMMIT_SHORT_SHA'" -o dist/ || go build -ldflags="-X 'main.commit=$CI_COMMIT_TAG'" -o dist/
artifacts:
name: go-app
paths:
- dist/
expire_in: 1 hour
tags:
- kubernetes
rules:
- if: '$CI_PIPELINE_SOURCE == "push"'
build_container:
stage: build_container
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
script:
- mkdir -p /kaniko/.docker
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
- /kaniko/executor
--context $CI_PROJECT_DIR
--dockerfile $CI_PROJECT_DIR/Containerfile
--destination $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
--snapshotMode=redo
--use-new-run
tags:
- kubernetes
rules:
- if: '$CI_PIPELINE_SOURCE == "push"'
tag_container:
stage: tag_container
tags:
- kubernetes
image:
name: gcr.io/go-containerregistry/crane:debug
entrypoint: [""]
script:
- crane auth login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- crane tag $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG $CI_COMMIT_TAG
rules:
- if: $CI_COMMIT_TAG != null
test:
stage: test
image: $KUBECTL_IMAGE
services:
- name: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
alias: pipeline-app
script:
- /bin/sh -c 'until nc -z pipeline-app $APPLICATION_PORT; do sleep 1; done'
timeout: 5 minutes
tags:
- kubernetes
rules:
- if: '$CI_PIPELINE_SOURCE == "push"'
generate_manifests:
stage: generate_manifests_pipeline
image: $KUBECTL_IMAGE
script:
- cd $CI_PROJECT_DIR
- mkdir -p manifests
- envsubst < templates/namespace.yml > manifests/1-namespace.yml
- envsubst < templates/deployment.yml > manifests/2-deployment.yml
- envsubst < templates/service.yml > manifests/3-service.yml
- envsubst < templates/ingress.yml > manifests/4-ingress.yml
artifacts:
untracked: true
expire_in: "1 hour"
paths:
- "manifests/"
tags:
- kubernetes
rules:
- if: '$CI_COMMIT_BRANCH != "develop" && $CI_COMMIT_BRANCH != "main" && $CI_PIPELINE_SOURCE != "merge_request_event" && $CI_COMMIT_TAG == null'
deploy:
stage: deploy_pipeline
image:
name: $KUBECTL_IMAGE
entrypoint: ['']
script:
- kubectl config use-context ${KUBECTL_CONTEXT_NAME}
- kubectl apply -f $CI_PROJECT_DIR/manifests/ --namespace ${DEVNAMESPACE}
- export INGRESS_URL=`kubectl get -n ${DEVNAMESPACE} ingress -o json 2> /dev/null| jq -r '.items[] | .spec.rules[] | .host as $host | .http.paths[] | ( $host + .path)' | sort | grep -v ^/`
- echo "Ingress is reachable on https://$INGRESS_URL"
tags:
- kubernetes
rules:
- if: '$CI_COMMIT_BRANCH != "develop" && $CI_COMMIT_BRANCH != "main" && $CI_PIPELINE_SOURCE != "merge_request_event" && $CI_COMMIT_TAG == null'
test_deployment:
stage: test_deployment_pipeline
image: $KUBECTL_IMAGE
script:
- /bin/sh -c 'until nc -z $DEVSVC.$DEVNAMESPACE $APPLICATION_PORT; do sleep 1; done'
timeout: 5 minutes
tags:
- kubernetes
rules:
- if: '$CI_COMMIT_BRANCH != "develop" && $CI_COMMIT_BRANCH != "main" && $CI_PIPELINE_SOURCE != "merge_request_event" && $CI_COMMIT_TAG == null'
destroy_deployment:
stage: destroy_deployment_pipeline
image:
name: $KUBECTL_IMAGE
entrypoint: ['']
script:
- kubectl config use-context ${KUBECTL_CONTEXT_NAME}
- kubectl delete -f $CI_PROJECT_DIR/manifests/ --namespace ${DEVNAMESPACE}
tags:
- kubernetes
when: manual
rules:
- if: '$CI_COMMIT_BRANCH != "develop" && $CI_COMMIT_BRANCH != "main" && $CI_PIPELINE_SOURCE != "merge_request_event" && $CI_COMMIT_TAG == null'
allow_failure: false
deploy_tst_environment:
stage: deploy_tst_environment
trigger: "kubernetes/gitops"
variables:
APPLICATION: "$CI_PROJECT_NAME"
TAG: "$CI_COMMIT_TAG"
ENVIRONMENT: "tst"
allow_failure: false
rules:
- if: $CI_COMMIT_BRANCH == "develop"
deploy_acc_environment:
stage: deploy_acc_environment
trigger: "kubernetes/gitops"
variables:
APPLICATION: "$CI_PROJECT_NAME"
TAG: "$CI_COMMIT_TAG"
IMAGE: "$CI_REGISTRY_IMAGE"
ENVIRONMENT: "acc"
allow_failure: false
when: manual
rules:
- if: $CI_COMMIT_TAG != null
deploy_prd_environment:
stage: deploy_prd_environment
trigger: "kubernetes/gitops"
variables:
APPLICATION: "$CI_PROJECT_NAME"
TAG: "$CI_COMMIT_TAG"
IMAGE: "$CI_REGISTRY_IMAGE"
ENVIRONMENT: "prd"
allow_failure: false
when: manual
rules:
- if: $CI_COMMIT_TAG != null