-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
90 lines (80 loc) · 2.64 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
variables:
APP_NAME: "monday-api"
# CONTAINER IMAGE
# Kaniko will use:
# - CONTAINER_REGISTRY_USERNAME
# - CONTAINER_REGISTRY_PASSWORD
# to authenticate to CONTAINER_REGISTRY
CONTAINER_REGISTRY: "$CI_REGISTRY"
CONTAINER_REGISTRY_USERNAME: "$CI_REGISTRY_USER"
CONTAINER_REGISTRY_PASSWORD: "$CI_REGISTRY_PASSWORD"
# Then, it'll take APP_DOCKERFILE as the bulid process
# and upload the image as APP_IMAGE
APP_DOCKERFILE: "./docker/prod.dockerfile"
APP_IMAGE: "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA"
# K8S MANIFEST
# On K8S_MANIFEST_SOURCE directory,
# And with respect to APP_IMAGE,
# Kustomize will do the build and output as K8S_MANIFEST_BUILD
K8S_MANIFEST_SOURCE: "./k8s/prod"
K8S_MANIFEST_BUILD: "build.yaml"
# NETWORK CONFIGURATION
# SSH client will use these settings to connect to the target server
# You should determine whether your pipeline need it or not!
NETWORK_SSH_PRIVATE_KEY: "$SSH_PRIVATE_KEY"
NETWORK_SSH_KNOWN_HOSTS: "$SSH_KNOWN_HOSTS"
NETWORK_SSH_CONFIG: "$SSH_CONFIG"
stages:
- test
- build
- deploy
test:
stage: test
image:
name: golang:1.16-buster
script:
- go test ./...
build:
stage: build
only:
- master
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
script:
- echo "$CONTAINER_REGISTRY $CONTAINER_REGISTRY_USERNAME $CONTAINER_REGISTRY_PASSWORD"
- mkdir -p /kaniko/.docker
- echo "{\"auths\":{\"$CONTAINER_REGISTRY\":{\"username\":\"$CONTAINER_REGISTRY_USERNAME\",\"password\":\"$CONTAINER_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json
- /kaniko/executor
--context $CI_PROJECT_DIR
--dockerfile $APP_DOCKERFILE
--destination $APP_IMAGE
deploy:
stage: deploy
cache:
key: kustomize
paths:
- $CI_PROJECT_DIR/$K8S_MANIFEST_SOURCE/kustomize
variables:
KUSTOMIZE_INSTALL_SCRIPT: https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh
script:
# Prepare ssh
- eval $(ssh-agent -s) # setup env vars required for ssh
- echo "$NETWORK_SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- echo "$NETWORK_SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
- echo "$NETWORK_SSH_CONFIG" > ~/.ssh/config
- chmod 644 ~/.ssh/known_hosts
# Workdir
- cd $K8S_MANIFEST_SOURCE
# Download kustomize when necessary
- '[ ! -f ./kustomize ] &&
curl -s "$KUSTOMIZE_INSTALL_SCRIPT" | bash'
# Build k8s manifest
- ./kustomize edit set image $APP_IMAGE
- ./kustomize build ./ > $K8S_MANIFEST_BUILD
artifacts:
paths:
- $K8S_MANIFEST_SOURCE/$K8S_MANIFEST_BUILD
expire_in: 1 week