-
Notifications
You must be signed in to change notification settings - Fork 3
/
.gitlab-ci.yml
120 lines (110 loc) · 4.54 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
variables:
MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
SPRING_CONFIG_MAP: spring-configmap
FILEBEAT_CONFIG_MAP: filebeat-config
DOCKER_PULL_SECRET: pride-gitlab-docker-secret
IMAGE: $CI_REGISTRY_IMAGE/$CI_COMMIT_REF_SLUG:${CI_PIPELINE_ID}-${CI_COMMIT_SHA}
#include:
# - template: Container-Scanning.gitlab-ci.yml
# - template: Dependency-Scanning.gitlab-ci.yml
# - template: License-Management.gitlab-ci.yml
cache:
paths:
- .m2/repository/
- target/
#build:
# image: maven:3.3.9-jdk-8-alpine
# stage: build
# script:
# - mvn $MAVEN_CLI_OPTS compile
docker_build:
image: docker:18.09
stage: build
services:
- docker:18.09-dind
script:
- apk add --update bash gettext tzdata coreutils ca-certificates wget
- update-ca-certificates
- wget -O ${APM_AGENT_JAR} ${APM_AGENT_URL}
- ls
- envsubst '${JAR_FILE_NAME} ${APM_AGENT_JAR} ${NFS_UID} ${NFS_GID} ${NFS_GID2}' < .Dockerfile > Dockerfile
- cat Dockerfile
- docker info
- docker build -t $IMAGE .
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
- docker push $IMAGE
when: manual
kube_deploy_prod_hh:
environment: prod-k8s-hh
extends: .kube_deploy_script
kube_deploy_prod_hx:
environment: prod-k8s-hx
extends: .kube_deploy_script
.kube_deploy_script:
stage: deploy
image: dtzar/helm-kubectl
script:
- apk add --update bash gettext tzdata coreutils
- export HOSTNAME_BCKUP=$HOSTNAME && export HOSTNAME='${HOSTNAME}'
- envsubst < config/.application.yml > config/application.yml
- envsubst < .filebeat.yml > filebeat.yml
- export HOSTNAME=$HOSTNAME_BCKUP
- mkdir -p ~/.kube
- echo $KUBE_CONFIG > kubeconfig
- base64 -d kubeconfig > ~/.kube/config
- kubectl config get-contexts
- kubectl create namespace $k8s_namespace || true
- kubectl -n $k8s_namespace delete secret $DOCKER_PULL_SECRET || true
- kubectl -n $k8s_namespace create secret docker-registry $DOCKER_PULL_SECRET --docker-server=$CI_REGISTRY --docker-username=$CI_DEPLOY_USER --docker-password=$CI_DEPLOY_PASSWORD
# create configmap for first time
- kubectl -n $k8s_namespace create configmap $SPRING_CONFIG_MAP --from-file=config/application.yml || true
# replace configmap if it already exists
- kubectl -n $k8s_namespace create configmap $SPRING_CONFIG_MAP --from-file=config/application.yml -o yaml --dry-run=client | kubectl replace -f -
# create configmap for first time
- kubectl -n $k8s_namespace create configmap $FILEBEAT_CONFIG_MAP --from-file=filebeat.yml || true
# replace configmap if it already exists
- kubectl -n $k8s_namespace create configmap $FILEBEAT_CONFIG_MAP --from-file=filebeat.yml -o yaml --dry-run=client | kubectl replace -f -
- envsubst < .kubernetes.yml > kubernetes.yml
- cat kubernetes.yml
- kubectl -n $k8s_namespace apply -f kubernetes.yml
when: manual
.deploy_script:
stage: deploy
image: maven:3.3.9-jdk-8-alpine
script:
- apk add --update openssh-client bash openssl rsync gettext tzdata
- eval $(ssh-agent -s)
- echo $SSH_KEY | base64 -d | tr -d '\r' | ssh-add - > /dev/null
- mkdir ~/.ssh
- chmod 700 ~/.ssh
- DEPLOY_SERVERS_ARRAY=(`echo $DEPLOY_SERVER | tr ',' ' '`)
- for i in "${DEPLOY_SERVERS_ARRAY[@]}"; do ssh-keyscan $i >> ~/.ssh/known_hosts; chmod 600 ~/.ssh/*; done
- export HOSTNAME_BCKUP=$HOSTNAME && export HOSTNAME='${HOSTNAME}'
- envsubst < config/.application.yml > config/application.yml
- chmod -R 700 config
- (rm src/main/resources/application.properties || true)
- export HOSTNAME=$HOSTNAME_BCKUP
- mvn $MAVEN_CLI_OPTS clean package -DjarFinalName=${JAR_FILE_NAME}
- DEPLOY_DIR=$(date +"%Y%m%d_%H%M%S")
- mkdir $DEPLOY_DIR
- cp -r target/${JAR_FILE_NAME}.jar config $DEPLOY_DIR/
- DEPLOY_SERVERS_ARRAY=(`echo $DEPLOY_SERVER | tr ',' ' '`)
- ssh $SERVER_USER@${DEPLOY_SERVERS_ARRAY[0]} "bash && mkdir -p $DEPLOY_PATH"
- rsync -a $DEPLOY_DIR $SERVER_USER@${DEPLOY_SERVERS_ARRAY[0]}:$DEPLOY_PATH/
- ssh $SERVER_USER@${DEPLOY_SERVERS_ARRAY[0]} "bash && cd $DEPLOY_PATH && ln -snf $DEPLOY_DIR current"
- for i in "${DEPLOY_SERVERS_ARRAY[@]}"; do ssh $SERVER_USER@$i "bash && cd $DEPLOY_PATH && (bash -l $STOP_SCRIPT || true) && (bash -l $START_SCRIPT > /dev/null 2>&1 &) && exit"; sleep 2m; done
# only:
## - master
deploy_prod:
environment: prod
extends: .deploy_script
# only:
# - master
when: manual
deploy_dev:
environment: dev
extends: .deploy_script
# only:
# - master
when: manual