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

7
8
9
.docker_login: &docker_login
  - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY_URL

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

13
14
15
16
17
.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:
18
19
    - name: $CI_REGISTRY_PROXY_URL/docker:20.10.2-dind
      alias: docker
20
21
22
23

#-------------------------
# Global Variables
#-------------------------
24
25
variables:
  DOCKER_DRIVER: overlay2
26
  DOCKER_TLS_CERTDIR: ""
27

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

37
38
39
#-------------------------
# Versioning Jobs
#-------------------------
40
.versioning_template:
41
42
43
44
45
46
47
48
49
  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
50

51
52
53
versioning_master:
  extends: .versioning_template
  before_script:
54
55
56
    - export LAST_VERSION=$(git describe --tags --abbrev=0)
    - export NEXT_VERSION=$(echo $LAST_VERSION | awk -F. -v OFS=. '{$NF++;print}')
    - export SGA_VERSION="$NEXT_VERSION-SNAPSHOT"
57
58
59
60
61
62
63
64
65
66
  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

67
68
69
#-------------------------
# Test Job
#-------------------------
70
lint:
71
  stage: test
72
73
74
  image: repo.tecgraf.puc-rio.br:18082/node:14.15-stretch
  cache:
    paths:
75
      - test/daemon/node_modules
76
  script:
77
    - cd test/daemon
78
79
80
81
82
    - npm install
    - npx eslint --ext js .

test:
  stage: test
83
  image: repo.tecgraf.puc-rio.br:18089/soma/sga-test-ubuntu:20.04
84
85
86
  cache:
    paths:
      - test/lua_runtime
87
      - test/daemon/node_modules
88
  script:
89
    - bash install.sh --force --posix --pbs --slurm $(pwd)/test/lua_runtime
90
    - cd test/daemon
91
92
    - npm install
    - npm run test
93
94
95
96
97
    - cd ..
    - lua_runtime/bin/luarocks install --server=https://luarocks.org busted
    - useradd -M tester -p ''
    - chown tester:tester $(pwd)
    - su -c $(pwd)/test_drivers.sh tester
98
99
100
101
102
103
104
105
106

#-------------------------
# Docker Jobs
#-------------------------
.docker_deploy:
  stage: release
  <<: *docker_image_template
  <<: *services_docker_dind_template
  script:
107
    - export DOCKER_BUILDKIT=1
108
109
110
111
112
    - 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
113
    - docker pull $DOCKER_REGISTRY$DOCKER_REGISTRY_IMAGE_DIND:$LAST || true
114
    - | 
115
      docker build \
116
      --tag $DOCKER_REGISTRY$CI_REGISTRY_IMAGE:$VERSION \
117
      --tag $DOCKER_REGISTRY$CI_REGISTRY_IMAGE:$LAST \
118
      -f Docker/Dockerfile --target deploy .
119
    - |
120
      docker build \
121
      --tag $DOCKER_REGISTRY$DOCKER_REGISTRY_IMAGE_DIND:$VERSION \
122
      --tag $DOCKER_REGISTRY$DOCKER_REGISTRY_IMAGE_DIND:$LAST \
123
      -f Docker/Dockerfile --target deploy-with-docker .
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
    - 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"
  only:
    - tags

#-------------------------
# Helm Jobs
#-------------------------
.lint_helm:
  stage: test
  <<: *docker_image_template
149
  <<: *services_docker_dind_template
150
  script:
151
    - *docker_login_proxy
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
    - 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