.gitlab-ci.yml 8.67 KB
Newer Older
1
2
3
4
5
#-------------------------
# Global Templates
#-------------------------
.docker_image_template: &docker_image_template
  image: $CI_REGISTRY_PROXY_URL/docker:18-git
6

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
.docker_login: &docker_login
  - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY_URL

.docker_login_proxy: &docker_login_proxy
  - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY_PROXY_URL

.helm_kubectl_image_template: &helm_kubectl_image_template
  image: repo.tecgraf.puc-rio.br:18089/soma/helm-kubectl:0.0.1

.services_docker_dind_template: &services_docker_dind_template
  services:
    - name: $CI_REGISTRY_PROXY_URL/docker:18-dind

#-------------------------
# Global Variables
#-------------------------
23
24
25
variables:
  DOCKER_HOST: tcp://localhost:2375
  DOCKER_DRIVER: overlay2
26

27
28
29
#-------------------------
# Job Stages
#-------------------------
30
31
stages:
  - version
32
33
  - test
  - release
34
  - deploy
35

36
37
38
#-------------------------
# Versioning Jobs
#-------------------------
39
.versioning_template:
40
41
42
43
44
45
46
47
48
  stage: version
  <<: *docker_image_template
  artifacts:
    paths:
      - shared-vars.sh
    expire_in: 1 week
  script:
    - echo $SGA_VERSION
    - echo "export VERSION=$SGA_VERSION" >> shared-vars.sh
49

50
51
52
versioning_master:
  extends: .versioning_template
  before_script:
53
    - export SGA_VERSION="snapshot"
54
55
56
57
58
59
60
61
62
63
  only:
    - master

versioning_tag:
  extends: .versioning_template
  before_script:
    - export SGA_VERSION=$(cat sga-daemon-*.rockspec | grep 'version[[:space:]]=[[:space:]]' | awk '{print $3}' | sed -E s/-[0-9]+//g | sed -E s/\"//g)
  only:
    - tags

64
65
66
#-------------------------
# Test Job
#-------------------------
67
static_check:
68
  stage: test
69
70
71
72
73
74
75
76
77
78
  image: repo.tecgraf.puc-rio.br:18089/soma/ubuntu:16.04-sga-posix
  script:
    - mkdir sgarest-daemon
    - curl -L https://git.tecgraf.puc-rio.br/csbase-dev/sgarest-daemon/-/archive/${CI_COMMIT_REF_NAME}/sgarest-daemon-${CI_COMMIT_REF_NAME}.tar.gz | tar xzf - -C sgarest-daemon --strip-components 1
    - cd sgarest-daemon
    - export errors=$(luac5.3 -p sgad | find . -regex ".*\.lua"  | awk '{print "luac5.3 -p "$1}' | sh -) 
    - ([ -z "$errors" ] && exit 0 || exit 1)
  only:
    - master
    - tags
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

#-------------------------
# Docker Jobs
#-------------------------
.docker_deploy:
  stage: release
  <<: *docker_image_template
  <<: *services_docker_dind_template
  script:
    - *docker_login_proxy
    - export DOCKER_BUILDKIT=1  
    - source shared-vars.sh
    - export DOCKER_REGISTRY=${CI_REGISTRY_URL:+$CI_REGISTRY_URL/}
    - export DOCKER_REGISTRY_IMAGE_DIND="${CI_REGISTRY_IMAGE}-dind"
    - *docker_login
    - docker pull $DOCKER_REGISTRY$CI_REGISTRY_IMAGE:$LAST || true
    - docker pull $DOCKER_REGISTRY$DOCKER_REGISTRY_IMAGE_DIND:$LAST || true   
    - cd Docker/posix/
    - | 
98
      docker build $BUILD_ARGS --cache-from $DOCKER_REGISTRY$CI_REGISTRY_IMAGE:$LAST \
99
100
101
102
      --tag $DOCKER_REGISTRY$CI_REGISTRY_IMAGE:$VERSION \
      --tag $DOCKER_REGISTRY$CI_REGISTRY_IMAGE:$LAST .
    - sed -r "s%^FROM .*%FROM $DOCKER_REGISTRY$CI_REGISTRY_IMAGE:$VERSION%" Dockerfile_sgadocker > /tmp/sgadind
    - |
103
      docker build --cache-from $DOCKER_REGISTRY$DOCKER_REGISTRY_IMAGE_DIND:$LAST \
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
      --tag $DOCKER_REGISTRY$DOCKER_REGISTRY_IMAGE_DIND:$VERSION \
      --tag $DOCKER_REGISTRY$DOCKER_REGISTRY_IMAGE_DIND:$LAST -f /tmp/sgadind .
    - rm -f /tmp/sgadind         
    - docker push $DOCKER_REGISTRY$CI_REGISTRY_IMAGE:$VERSION
    - docker push $DOCKER_REGISTRY$CI_REGISTRY_IMAGE:$LAST
    - docker push $DOCKER_REGISTRY$DOCKER_REGISTRY_IMAGE_DIND:$VERSION
    - docker push $DOCKER_REGISTRY$DOCKER_REGISTRY_IMAGE_DIND:$LAST

docker_snapshot:
  extends: .docker_deploy
  before_script:
    - export LAST="snapshot"
  only:
    - master

docker_release:
  extends: .docker_deploy
  before_script:
    - export LAST="latest"
    - export BUILD_ARGS="--build-arg TAG=${CI_COMMIT_TAG}"
  only:
    - tags

#-------------------------
# Helm Jobs
#-------------------------
.lint_helm:
  stage: test
  <<: *docker_image_template
  <<: *services_docker_dind_template
  script:
    - *docker_login_proxy
    - source shared-vars.sh
    - cat ${CI_KUBE_CONFIG_FILE} > .kube/kube-config.yaml
    - sed -i.bak "s/__VERSION__/${VERSION}/" kubernetes/posix/helm/Chart.yaml
    - cat kubernetes/posix/helm/Chart.yaml
    - (docker run --rm -e KUBECONFIG=".kube/kube-config.yaml" -v $(pwd):/apps -v ~/.kube:/root/.kube -v ~/.helm:/root/.helm $CI_REGISTRY_PROXY_URL/alpine/helm:3.2.1 lint kubernetes/posix/helm/ --strict -f kubernetes/posix/helm/demo/${LINT_VALUES_FILE}) | tee /tmp/helm-lint.log
    - ([ $(grep -c ERROR /tmp/helm-lint.log) -gt 0 ] && exit 1 || exit 0)
  artifacts:
    paths:
      - kubernetes/posix/helm/Chart.yaml
    expire_in: 1 day

lint_helm_snapshot:
  extends: .lint_helm
  before_script:
    - export LINT_VALUES_FILE=dev-values.yaml
  only:
    - master

lint_helm_latest:
  extends: .lint_helm
  before_script:
    - export LINT_VALUES_FILE=hml-values.yaml
  only:
    - tags

#---------------------------------
# Helm Package
#---------------------------------
package_helm:
  stage: release
  <<: *helm_kubectl_image_template
  script: |
    source shared-vars.sh
    export CHART_NAME="$(grep -E "^name:" kubernetes/posix/helm/Chart.yaml | cut -d":" -f2 | sed -e 's/^[ \t]*//')"
    export CHART_VERSION="$(grep -E "^version:" kubernetes/posix/helm/Chart.yaml | cut -d":" -f2 | sed -e 's/^[ \t]*//')"
    export APP_VERSION="$(grep -E "^appVersion:" kubernetes/posix/helm/Chart.yaml | cut -d":" -f2 | sed -e 's/^[ \t]*//')"
    export PACKAGE_NAME="${CHART_NAME}-${CHART_VERSION}"
    printf "\n#--------------------------------------------------------------"
    printf "\n# Helm packaging..."
    printf "\n#--------------------------------------------------------------\n"    
    helm package kubernetes/posix/helm/ -d /tmp/
    
    printf "\n#--------------------------------------------------------------"
    printf "\n# Cleaning old Chart package $PACKAGE_NAME.tgz"
    printf "\n#--------------------------------------------------------------\n"
    curl -I -X DELETE -u ${CI_REGISTRY_USER}:${CI_REGISTRY_PASSWORD} ${CI_HELM_REGISTRY_URL}/$PACKAGE_NAME.tgz || true
    
    printf "\n#--------------------------------------------------------------"
    printf "\n# Uploading Chart package $PACKAGE_NAME.tgz"
    printf "\n#--------------------------------------------------------------\n"
    curl -I -u ${CI_REGISTRY_USER}:${CI_REGISTRY_PASSWORD} ${CI_HELM_REGISTRY_URL}/ --upload-file /tmp/$PACKAGE_NAME.tgz
  only:
    - master
    - tags

# -----------------------
# Deploy Stage
# -----------------------
.helm_upgrade:
  stage: deploy
  <<: *helm_kubectl_image_template
  script:
    - cat ${CI_KUBE_CONFIG_FILE} > .kube/kube-config.yaml
    - export KUBERNETES_CONTEXT="K8S-CONTEXT-PACKAGE-${CI_COMMIT_SHORT_SHA}"
    - kubectl config set-cluster "$CI_KUBERNETES_CLUSTER" --insecure-skip-tls-verify=true --server="$CI_KUBERNETES_URL"
    - kubectl config set-context "$KUBERNETES_CONTEXT" --cluster="$CI_KUBERNETES_CLUSTER" --user="$CI_KUBERNETES_USER_NAME"
    - kubectl config set-credentials "$CI_KUBERNETES_USER_NAME" --token="$CI_KUBERNETES_USER_TOKEN"
    - kubectl config use-context "$KUBERNETES_CONTEXT"
    - export CHART_NAME="$(grep -E "^name:" kubernetes/posix/helm/Chart.yaml | cut -d":" -f2 | sed -e 's/^[ \t]*//')"
    - export CHART_VERSION="$(grep -E "^version:" kubernetes/posix/helm/Chart.yaml | cut -d":" -f2 | sed -e 's/^[ \t]*//')"
    - export RELEASE_NAME="${CHART_NAME}-${RELEASE_NAME_SUFFIX}"
    - helm repo add soma-charts-nexus ${CI_HELM_REGISTRY_URL} --username ${CI_REGISTRY_USER} --password ${CI_REGISTRY_PASSWORD}
    - helm repo list | grep soma-charts-nexus
    - helm upgrade --install --namespace ${NAMESPACE} ${RELEASE_NAME} soma-charts-nexus/${CHART_NAME} --version=${CHART_VERSION} --values kubernetes/posix/helm/demo/${FILES_NAME} --set-string podAnnotations.commit=${CI_COMMIT_SHA} --dry-run >> ${ARTIFACT_NAME}
    - helm upgrade --install --namespace ${NAMESPACE} ${RELEASE_NAME} soma-charts-nexus/${CHART_NAME} --version=${CHART_VERSION} --values kubernetes/posix/helm/demo/${FILES_NAME} --set-string podAnnotations.commit=${CI_COMMIT_SHA}

deploy_snapshot:
  extends: .helm_upgrade
  before_script:
    - export NAMESPACE=soma-dev
    - export RELEASE_NAME_SUFFIX=snapshot
    - export FILES_NAME="dev-values.yaml"
    - export ARTIFACT_NAME="helm-dev.log"
  artifacts:
    paths:
      - helm-dev.log
    expire_in: 1 month
  only:
    - master

deploy_latest:
  extends: .helm_upgrade
  before_script:
    - export NAMESPACE=soma-hml
    - export RELEASE_NAME_SUFFIX=latest
    - export FILES_NAME="hml-values.yaml"
    - export ARTIFACT_NAME="helm-hml.log"
  artifacts:
    paths:
      - helm-hml.log
    expire_in: 1 month
  only:
    - tags