Skip to content

Commit 8100799

Browse files
mistralozNgob
authored andcommitted
Add Kube in CI/CD (after review)
1 parent 5189b07 commit 8100799

File tree

9 files changed

+93
-34
lines changed

9 files changed

+93
-34
lines changed

.gitlab-ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ stages:
22
- build
33
- test
44
- deploy
5+
- cleanup
56

67
# It is shared between includes anyway
78
default:
@@ -22,14 +23,16 @@ default:
2223
variables:
2324
GIT_STRATEGY: fetch
2425
GIT_DEPTH: 1
25-
CURRENT_WORKFLOW: "undefined"
2626
TAG_NAME: ${CI_COMMIT_REF_SLUG}
2727
BACK_IMAGE: ${CI_REGISTRY_IMAGE}/back
2828
BACK_IMAGE_DEV: ${BACK_IMAGE}:${TAG_NAME}-dev
2929
BACK_IMAGE_STABLE: ${BACK_IMAGE}:${TAG_NAME}
3030
FRONT_IMAGE: ${CI_REGISTRY_IMAGE}/front
3131
FRONT_IMAGE_DEV: ${FRONT_IMAGE}:${TAG_NAME}-dev
3232
FRONT_IMAGE_STABLE: ${FRONT_IMAGE}:${TAG_NAME}
33+
FEATURE_DEPLOY_MR_ON_K8S: 1
34+
KUBERNETES_URL_SUFFIX: "boilerplate.test.thecodingmachine.com"
35+
#KUBERNETES_PASSWORD_SECRET: "CHANGE-THIS-SECRET" # Define it in gitlab CI/CD variables
3336
# DEPLOYMENT_PATH: /tcm_deployment/${CI_PROJECT_NAME}
3437
# DEPLOYMENT_IP: PUT_YOUR_DEPLOYMENT_IP_HERE
3538

deployments/kubernetes/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ kind: Ingress
44
metadata:
55
name: ingress
66
annotations:
7-
cert-manager.io/cluster-issuer: letsencrypt-staging
7+
cert-manager.io/cluster-issuer: letsencrypt-prod
88
kubernetes.io/ingress.global-static-ip-name: web-static-ip
99
nginx.ingress.kubernetes.io/proxy-body-size: 1024m
1010
nginx.ingress.kubernetes.io/force-ssl-redirect: "false"

gitlab-ci/build.gitlab-ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ build back dev:
99
TARGET: dev
1010
CONTEXT_PATH: ./apps/back
1111
IMAGE_DESTINATION: $BACK_IMAGE_DEV
12+
rules:
13+
- !reference [.except hotfix rules, rules]
14+
- when: always
1215

1316
build back stable:
1417
stage: 'build'
@@ -31,6 +34,9 @@ build front dev:
3134
TARGET: dev
3235
CONTEXT_PATH: ./apps/front
3336
IMAGE_DESTINATION: $FRONT_IMAGE_DEV
37+
rules:
38+
- !reference [.except hotfix rules, rules]
39+
- when: always
3440

3541
build front stable:
3642
stage: 'build'

gitlab-ci/deploy-k8s.gitlab-ci.yml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
include:
22
- local: 'gitlab-ci/templates/k8s.gitlab-ci.yml'
33

4-
variables:
5-
KUBERNETES_URL_SUFFIX: "boilerplate.test.thecodingmachine.com"
6-
74
deploy k8s start:
85
stage: 'deploy'
96
extends:
107
- '.deploy k8s start'
118
environment:
9+
deployment_tier: development
1210
on_stop: deploy k8s stop
1311
auto_stop_in: 6 week
1412
needs:
1513
- 'build back stable'
1614
- 'build front stable'
1715
rules:
16+
- if: '$FEATURE_DEPLOY_MR_ON_K8S != "1"'
17+
when: never
1818
- !reference [.MR rules, rules]
1919
when: manual
2020

@@ -25,5 +25,23 @@ deploy k8s stop:
2525
needs:
2626
- 'deploy k8s start'
2727
rules:
28+
- if: '$FEATURE_DEPLOY_MR_ON_K8S != "1"'
29+
when: never
2830
- !reference [.MR rules, rules]
2931
when: manual
32+
33+
remove stable images:
34+
stage: 'cleanup'
35+
extends:
36+
- '.delete image'
37+
needs:
38+
- 'deploy k8s stop'
39+
script:
40+
- ./reg rm -d --auth-url $CI_REGISTRY -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $BACK_IMAGE_STABLE
41+
- ./reg rm -d --auth-url $CI_REGISTRY -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $FRONT_IMAGE_STABLE
42+
rules:
43+
- if: '$FEATURE_DEPLOY_MR_ON_K8S != "1"'
44+
when: never
45+
- !reference [.MR rules, rules]
46+
when: on_success
47+

gitlab-ci/rules.gitlab-ci.yml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44
if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
55
.rc rules:
66
rules:
7-
- # Target only tags "a.b.c-rcX" and develop branch
8-
if: '$CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+-rc\.\d+$/ || $CI_COMMIT_REF_NAME =~ /^develop$/'
7+
- # Target only tags "a.b.c-rcX" and main branch
8+
if: '$CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+-rc\.\d+$/ || $CI_COMMIT_REF_NAME =~ /^main/'
99
.stable rules:
1010
rules:
11-
- # Target only tags "a.b.c" and main branch
12-
if: '$CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/ || $CI_COMMIT_REF_NAME =~ /^main$/'
11+
- # Target only tags "a.b.c"
12+
if: '$CI_COMMIT_TAG =~ /^v\d+\.\d+\.\d+$/'
13+
.hotfix rules:
14+
rules:
15+
- # Target tags and branches named `hotfix/...`
16+
if: '$CI_COMMIT_TAG =~ /^hotfix\// || $CI_COMMIT_REF_NAME =~ /^hotfix\//'
17+
.except hotfix rules:
18+
rules:
19+
- # Target tags and branches named `hotfix/...`
20+
if: '$CI_COMMIT_TAG =~ /^hotfix\// || $CI_COMMIT_REF_NAME =~ /^hotfix\//'
21+
when: never
1322

1423

1524
workflow:
@@ -20,3 +29,5 @@ workflow:
2029
- !reference [.MR rules, rules]
2130
- !reference [.rc rules, rules]
2231
- !reference [.stable rules, rules]
32+
- !reference [.hotfix rules, rules]
33+
- !reference [.except hotfix rules, rules]

gitlab-ci/templates/build.gitlab-ci.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,19 @@
5757
.build image:
5858
extends:
5959
- '.build image kaniko'
60+
61+
# IMAGE_TAG is required except if script part is overwrited (to allow multiple remove in one task).
62+
.delete image:
63+
image: curlimages/curl:7.86.0
64+
variables:
65+
IMAGE_TAG: "" # Required
66+
REG_SHA256: ade837fc5224acd8c34732bf54a94f579b47851cc6a7fd5899a98386b782e228
67+
REG_VERSION: 0.16.1
68+
GIT_STRATEGY: none
69+
before_script:
70+
- curl --fail --show-error --location "https://github.com/genuinetools/reg/releases/download/v$REG_VERSION/reg-linux-amd64" --output ./reg
71+
- echo "$REG_SHA256 ./reg" | sha256sum -c -
72+
- chmod a+x ./reg
73+
script:
74+
- if [[ -z $IMAGE_TAG ]]; then echo "IMAGE_TAG is required"; exit 1; fi
75+
- ./reg rm -d --auth-url $CI_REGISTRY -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $IMAGE_TAG

gitlab-ci/templates/k8s.gitlab-ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
.deploy k8s start:
22
image: thecodingmachine/k8s_gitlabci:latest
33
variables:
4+
#KUBERNETES_URL_SUFFIX: "" # Required
5+
#KUBERNETES_PASSWORD_SECRET: "" # Required
46
AUTOCONNECT: 1
57
KUBECONFIG: "/root/.kube/config"
68
before_script:
9+
- if [[ -z $KUBERNETES_URL_SUFFIX ]]; then echo "KUBERNETES_URL_SUFFIX is required"; exit 1; fi
10+
- if [[ -z $KUBERNETES_PASSWORD_SECRET ]]; then echo "KUBERNETES_PASSWORD_SECRET is required"; exit 1; fi
711
- mkdir -p ~/.kube
812
- echo "$KUBE_CONFIG" > ~/.kube/config
913
- 'kubectl create namespace ${CI_PROJECT_PATH_SLUG}-${CI_COMMIT_REF_SLUG} || true'
@@ -42,3 +46,4 @@
4246
environment:
4347
name: "review/${CI_COMMIT_REF_NAME}"
4448
action: stop
49+

gitlab-ci/test-back.gitlab-ci.yml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ test back build:
1111
- exit 0
1212
needs:
1313
- 'build back dev'
14+
rules:
15+
- !reference [.except hotfix rules, rules]
16+
- when: always
1417

1518
test back analyze:
1619
stage: 'test'
@@ -22,17 +25,9 @@ test back analyze:
2225
- composer run phpmd
2326
needs:
2427
- 'test back build'
25-
26-
test back analyze:
27-
stage: 'test'
28-
extends:
29-
- '.app back'
30-
script:
31-
- composer run phpstan -- --no-progress
32-
- composer run cs-check -- -n
33-
- composer run phpmd
34-
needs:
35-
- 'test back build'
28+
rules:
29+
- !reference [.except hotfix rules, rules]
30+
- when: always
3631

3732
# TODO : have to be implemented
3833
#back unit test:
@@ -52,3 +47,6 @@ test back analyze:
5247
# - composer run test
5348
# needs:
5449
# - 'test back build'
50+
# rules:
51+
# - !reference [.except hotfix rules, rules]
52+
# - when: always

gitlab-ci/test-front.gitlab-ci.yml

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
include:
22
- local: 'gitlab-ci/templates/apps.gitlab-ci.yml'
33

4-
# required only if there is multiple tests for front part
5-
#test front build:
6-
# stage: "test"
7-
# needs:
8-
# - 'build front dev'
9-
# extends:
10-
# - '.app front'
11-
# script:
12-
# - exit 0
13-
# cache:
14-
# - <<: *app_front_cache
15-
# policy: pull-push
4+
test front build:
5+
stage: "test"
6+
needs:
7+
- 'build front dev'
8+
extends:
9+
- '.app front cache-rw'
10+
script:
11+
- exit 0
12+
rules:
13+
- !reference [.except hotfix rules, rules]
14+
- when: always
1615

1716
front linter:
1817
stage: 'test'
1918
needs:
20-
- 'build front dev'
19+
- 'test front build'
2120
extends:
22-
- '.app front cache-rw'
21+
- '.app front'
2322
script:
2423
- yarn lint
24+
rules:
25+
- !reference [.except hotfix rules, rules]
26+
- when: always

0 commit comments

Comments
 (0)