forked from SwiftPackageIndex/SwiftPackageIndex-Server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
196 lines (173 loc) · 6.22 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
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
# Copyright Dave Verwer, Sven A. Schmidt, and other contributors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
image: docker:stable
variables:
REGISTRY_IMAGE: registry.gitlab.com/finestructure/swiftpackageindex
DOCKER_TLS_CERTDIR: '/certs'
services:
- docker:stable-dind
before_script:
- docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
stages:
- build
- deploy
- smoke-test
# We build only on the PROD project's pipeline, because we can read the image
# for dev deployment. There is no need to build the images twice in parallel.
# The PROD projects registry already has a build history, so we keep pushing to it.
build:
rules:
- if: $ENV == "prod" && $CI_PIPELINE_SOURCE != "schedule"
stage: build
tags:
- spi-server-deploy
script: |
VERSION=${CI_COMMIT_TAG:-$CI_COMMIT_SHA}
echo 'let appVersion: String? = "'${VERSION}'"' > ./Sources/App/Core/AppVersion.swift
make build-front-end
# Login in again in case the previous steps were slow and made the token time out
docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
docker build -t $REGISTRY_IMAGE:$VERSION .
retries=3
until docker push $REGISTRY_IMAGE:$VERSION ; do
[[ $retries -eq 0 ]] && echo "docker push failed" && exit 1
sleep 5
echo Retrying ...
docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
echo $((retries--)) retries left
done
.deploy-common: &deploy-common
stage: deploy
image: docker
script: |
set -eu
VERSION=${CI_COMMIT_TAG:-$CI_COMMIT_SHA}
discord() {
curl -s -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data '{"content": "'"$1"'"}' $DISCORD_WEBHOOK
}
get_version() {
url=$1
set +e # don't fail if curl sends garbage to jq
DEPLOYED_VERSION=$(curl -s $url | jq -r '.version' 2> /dev/null)
if [ $? -ne 0 ]; then DEPLOYED_VERSION="none"; fi
set -e
echo "$DEPLOYED_VERSION"
}
post_annotation() {
msg=$1
# we create this weird one-off service command in order to post a
# notification that our monitoring can pick up for a release annotation
# in Grafana
docker service rm deploy > /dev/null 2>&1 || true
docker service create --quiet --name deploy --mode=replicated-job --replicas=1 busybox sh -c "echo $msg"
docker service rm deploy > /dev/null 2>&1 || true
}
# pull image (retry for up to 30 minutes to allow builds on another pipeline to complete)
echo Pulling image ...
retries=180
until docker pull $REGISTRY_IMAGE:$VERSION; do
[[ $retries -eq 0 ]] && echo "docker pull failed" && exit 1
sleep 10
echo Retrying ...
echo $((retries--)) retries left
done
echo Deploying VERSION $VERSION with SITE_URL: ${SITE_URL} ...
discord "🚧 deploying version $VERSION [↗]($CI_JOB_URL) ..."
retries=3
until env VERSION=$VERSION \
docker stack deploy --with-registry-auth -c app.yml app ; do
sleep 5
echo Retrying ...
echo $((retries--)) retries left
done
retries=3
until docker stack deploy --with-registry-auth -c mon.yml mon ; do
sleep 5
echo Retrying ...
echo $((retries--)) retries left
done
# poll for new version to be available
ATTEMPTS=120
for i in $(seq 1 $ATTEMPTS); do
echo "Waiting for $VERSION to be active ($i)"
sleep 2
DEPLOYED_VERSION=$(get_version $SITE_URL/api/version)
echo "DEPLOYED_VERSION $DEPLOYED_VERSION"
if [[ "$DEPLOYED_VERSION" == "$VERSION" ]]; then break; fi
done
# wait for server to be fully available before reporting result
SVC=app_server
ATTEMPTS=120
for i in $(seq 1 $ATTEMPTS); do
echo "Waiting for all replicas to be available ($i)"
sleep 2
DEPLOYED=$(docker service ls -f "Name=${SVC}" --format "{{.Replicas}}")
echo "DEPLOYED $DEPLOYED"
IFS='/' read -ra COUNT <<< "$DEPLOYED"
if [[ "${COUNT[0]}" == "${COUNT[1]}" ]]; then break; fi
done
if [[ "$DEPLOYED_VERSION" == "$VERSION" && "${COUNT[0]}" == "${COUNT[1]}" ]]; then
post_annotation "✅ deployed version $VERSION"
discord "✅ deployed version $VERSION [↗]($CI_JOB_URL)"
else
discord "🛑 failed to deploy version $VERSION [↗]($CI_JOB_URL)"
exit 1
fi
deploy prod: # PROD: auto-deploy tags
rules:
- if: $ENV == "prod" && $CI_COMMIT_TAG != null
tags:
- spi-p1
<<: *deploy-common
deploy dev: # DEV: auto-deploy main
rules:
- if: $ENV == "dev" && $CI_PIPELINE_SOURCE != "schedule" && $CI_COMMIT_BRANCH == "main"
- if: $ENV == "dev" && $CI_PIPELINE_SOURCE != "schedule" && $CI_COMMIT_TAG != null
tags:
- spi-d1
<<: *deploy-common
deploy dev (ad hoc): # DEV: deploy any revision manually
rules:
- if: $ENV == "dev" && $CI_PIPELINE_SOURCE != "schedule"
when: manual
tags:
- spi-d1
<<: *deploy-common
smoke-test:
rules:
- if: $ENV == "prod" && $CI_COMMIT_TAG != null
- if: $ENV == "prod" && $CI_PIPELINE_SOURCE == "schedule"
- if: $ENV == "dev"
stage: smoke-test
tags:
- spi-server-deploy
script: |
rester() {
docker run --rm -t -e base_url="$SITE_URL" -e api_token="$api_token" -v $PWD:/host -w /host finestructure/rester:0.8.1 "$1"
}
echo Testing with SITE_URL: ${SITE_URL}
rester restfiles/smoke-test.restfile
doc-test:
rules:
- if: $ENV == "prod" && $CI_COMMIT_TAG != null
- if: $ENV == "prod" && $CI_PIPELINE_SOURCE == "schedule"
stage: smoke-test
tags:
- spi-server-deploy
script: |
rester() {
docker run --rm -t -e base_url="$SITE_URL" -v $PWD:/host -w /host finestructure/rester:0.8.1 "$1"
}
echo Testing with SITE_URL: ${SITE_URL}
rester restfiles/doc-test.restfile