summaryrefslogtreecommitdiff
path: root/.gitlab-ci.yml
blob: 8d701c2a747f57408335f4d96a21f16c446984eb (plain)
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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
stages:
  - build
  - build docker image
  - test
  - upload artifacts

variables:
  GIT_SUBMODULE_STRATEGY: recursive
  FF_USE_FASTZIP: 1
  CACHE_COMPRESSION_LEVEL: fastest
  # Docker in Docker
  DOCKER_HOST: tcp://docker:2375/
  DOCKER_TLS_CERTDIR: ""
  DOCKER_DRIVER: overlay2

# --------------------------------------------------------------------- #
#  Cargo: Compiling for different architectures                         #
# --------------------------------------------------------------------- #

.build-cargo-shared-settings:
  stage: "build"
  needs: []
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
    - if: '$CI_COMMIT_BRANCH == "next"'
    - if: "$CI_COMMIT_TAG"
    - if: '($CI_MERGE_REQUEST_APPROVED == "true") || $BUILD_EVERYTHING' # Once MR is approved, test all builds. Or if BUILD_EVERYTHING is set.
  interruptible: true
  image: "registry.gitlab.com/jfowl/conduit-containers/rust-with-tools:latest"
  tags: ["docker"]
  services: ["docker:dind"]
  variables:
    SHARED_PATH: $CI_PROJECT_DIR/shared
    CARGO_PROFILE_RELEASE_LTO: "true"
    CARGO_PROFILE_RELEASE_CODEGEN_UNITS: "1"
    CARGO_INCREMENTAL: "false" # https://matklad.github.io/2021/09/04/fast-rust-builds.html#ci-workflow
  before_script:
    - 'echo "Building for target $TARGET"'
    - "rustup show && rustc --version && cargo --version" # Print version info for debugging
    # fix cargo and rustup mounts from this container (https://gitlab.com/gitlab-org/gitlab-foss/-/issues/41227)
    - "mkdir -p $SHARED_PATH/cargo"
    - "cp -r $CARGO_HOME/bin $SHARED_PATH/cargo"
    - "cp -r $RUSTUP_HOME $SHARED_PATH"
    - "export CARGO_HOME=$SHARED_PATH/cargo RUSTUP_HOME=$SHARED_PATH/rustup"
    # If provided, bring in caching through sccache, which uses an external S3 endpoint to store compilation results.
    - if [ -n "${SCCACHE_ENDPOINT}" ]; then export RUSTC_WRAPPER=/sccache; fi
  script:
    # cross-compile conduit for target
    - 'time cross build --target="$TARGET" --locked --release'
    - 'mv "target/$TARGET/release/conduit" "conduit-$TARGET"'
    # print information about linking for debugging
    - "file conduit-$TARGET" # print file information
    - 'readelf --dynamic conduit-$TARGET | sed -e "/NEEDED/q1"' # ensure statically linked
  cache:
    # https://doc.rust-lang.org/cargo/guide/cargo-home.html#caching-the-cargo-home-in-ci
    key: "cargo-cache-$TARGET"
    paths:
      - $SHARED_PATH/cargo/registry/index
      - $SHARED_PATH/cargo/registry/cache
      - $SHARED_PATH/cargo/git/db
  artifacts:
    expire_in: never

build:release:cargo:x86_64-unknown-linux-musl-with-debug:
  extends: .build-cargo-shared-settings
  variables:
    CARGO_PROFILE_RELEASE_DEBUG: 2 # Enable debug info for flamegraph profiling
    TARGET: "x86_64-unknown-linux-musl"
  after_script:
    - "mv ./conduit-x86_64-unknown-linux-musl ./conduit-x86_64-unknown-linux-musl-with-debug"
  artifacts:
    name: "conduit-x86_64-unknown-linux-musl-with-debug"
    paths:
      - "conduit-x86_64-unknown-linux-musl-with-debug"
    expose_as: "Conduit for x86_64-unknown-linux-musl-with-debug"

build:release:cargo:x86_64-unknown-linux-musl:
  extends: .build-cargo-shared-settings
  variables:
    TARGET: "x86_64-unknown-linux-musl"
  artifacts:
    name: "conduit-x86_64-unknown-linux-musl"
    paths:
      - "conduit-x86_64-unknown-linux-musl"
    expose_as: "Conduit for x86_64-unknown-linux-musl"

build:release:cargo:arm-unknown-linux-musleabihf:
  extends: .build-cargo-shared-settings
  variables:
    TARGET: "arm-unknown-linux-musleabihf"
  artifacts:
    name: "conduit-arm-unknown-linux-musleabihf"
    paths:
      - "conduit-arm-unknown-linux-musleabihf"
    expose_as: "Conduit for arm-unknown-linux-musleabihf"

build:release:cargo:armv7-unknown-linux-musleabihf:
  extends: .build-cargo-shared-settings
  variables:
    TARGET: "armv7-unknown-linux-musleabihf"
  artifacts:
    name: "conduit-armv7-unknown-linux-musleabihf"
    paths:
      - "conduit-armv7-unknown-linux-musleabihf"
    expose_as: "Conduit for armv7-unknown-linux-musleabihf"

build:release:cargo:aarch64-unknown-linux-musl:
  extends: .build-cargo-shared-settings
  variables:
    TARGET: "aarch64-unknown-linux-musl"
  artifacts:
    name: "conduit-aarch64-unknown-linux-musl"
    paths:
      - "conduit-aarch64-unknown-linux-musl"
    expose_as: "Conduit for aarch64-unknown-linux-musl"

.cargo-debug-shared-settings:
  extends: ".build-cargo-shared-settings"
  rules:
    - when: "always"
  cache:
    key: "build_cache--$TARGET--$CI_COMMIT_BRANCH--debug"
  script:
    # cross-compile conduit for target
    - 'time time cross build --target="$TARGET" --locked'
    - 'mv "target/$TARGET/debug/conduit" "conduit-debug-$TARGET"'
    # print information about linking for debugging
    - "file conduit-debug-$TARGET" # print file information
    - 'readelf --dynamic conduit-debug-$TARGET | sed -e "/NEEDED/q1"' # ensure statically linked
  artifacts:
    expire_in: 4 weeks

build:debug:cargo:x86_64-unknown-linux-musl:
  extends: ".cargo-debug-shared-settings"
  variables:
    TARGET: "x86_64-unknown-linux-musl"
  artifacts:
    name: "conduit-debug-x86_64-unknown-linux-musl"
    paths:
      - "conduit-debug-x86_64-unknown-linux-musl"
    expose_as: "Conduit DEBUG for x86_64-unknown-linux-musl"

# --------------------------------------------------------------------- #
#  Create and publish docker image                                      #
# --------------------------------------------------------------------- #

.docker-shared-settings:
  stage: "build docker image"
  image: jdrouet/docker-with-buildx:stable
  tags: ["docker"]
  services:
    - docker:dind
  needs:
    - "build:release:cargo:x86_64-unknown-linux-musl"
    - "build:release:cargo:arm-unknown-linux-musleabihf"
    - "build:release:cargo:armv7-unknown-linux-musleabihf"
    - "build:release:cargo:aarch64-unknown-linux-musl"
  variables:
    PLATFORMS: "linux/arm/v6,linux/arm/v7,linux/arm64,linux/amd64"
    DOCKER_FILE: "docker/ci-binaries-packaging.Dockerfile"
  cache:
    paths:
      - docker_cache
    key: "$CI_JOB_NAME"
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    # Only log in to Dockerhub if the credentials are given:
    - if [ -n "${DOCKER_HUB}" ]; then docker login -u "$DOCKER_HUB_USER" -p "$DOCKER_HUB_PASSWORD" "$DOCKER_HUB"; fi
  script:
    # Prepare buildx to build multiarch stuff:
    - docker context create 'ci-context'
    - docker buildx create --name 'multiarch-builder' --use 'ci-context'
    # Copy binaries to their docker arch path
    - mkdir -p linux/ && mv ./conduit-x86_64-unknown-linux-musl linux/amd64
    - mkdir -p linux/arm/ && mv ./conduit-arm-unknown-linux-musleabihf linux/arm/v6
    - mkdir -p linux/arm/ && mv ./conduit-armv7-unknown-linux-musleabihf linux/arm/v7
    - mv ./conduit-aarch64-unknown-linux-musl linux/arm64
    - 'export CREATED=$(date -u +''%Y-%m-%dT%H:%M:%SZ'') && echo "Docker image creation date: $CREATED"'
    # Build and push image:
    - >
      docker buildx build
      --pull
      --push
      --cache-from=type=local,src=$CI_PROJECT_DIR/docker_cache
      --cache-to=type=local,dest=$CI_PROJECT_DIR/docker_cache
      --build-arg CREATED=$CREATED
      --build-arg VERSION=$(grep -m1 -o '[0-9].[0-9].[0-9]' Cargo.toml)
      --build-arg "GIT_REF=$CI_COMMIT_SHORT_SHA"
      --platform "$PLATFORMS"
      --tag "$TAG"
      --tag "$TAG-alpine"
      --tag "$TAG-commit-$CI_COMMIT_SHORT_SHA"
      --file "$DOCKER_FILE" .

docker:next:gitlab:
  extends: .docker-shared-settings
  rules:
    - if: '$CI_COMMIT_BRANCH == "next"'
  variables:
    TAG: "$CI_REGISTRY_IMAGE/matrix-conduit:next"

docker:next:dockerhub:
  extends: .docker-shared-settings
  rules:
    - if: '$CI_COMMIT_BRANCH == "next" && $DOCKER_HUB'
  variables:
    TAG: "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:next"

docker:master:gitlab:
  extends: .docker-shared-settings
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
  variables:
    TAG: "$CI_REGISTRY_IMAGE/matrix-conduit:latest"

docker:master:dockerhub:
  extends: .docker-shared-settings
  rules:
    - if: '$CI_COMMIT_BRANCH == "master" && $DOCKER_HUB'
  variables:
    TAG: "$DOCKER_HUB_IMAGE/matrixconduit/matrix-conduit:latest"

# --------------------------------------------------------------------- #
#  Run tests                                                            #
# --------------------------------------------------------------------- #

test:cargo:
  stage: "test"
  needs: []
  image: "registry.gitlab.com/jfowl/conduit-containers/rust-with-tools:latest"
  tags: ["docker"]
  variables:
    CARGO_INCREMENTAL: "false" # https://matklad.github.io/2021/09/04/fast-rust-builds.html#ci-workflow
  interruptible: true
  before_script:
    - rustup component add clippy rustfmt
    # If provided, bring in caching through sccache, which uses an external S3 endpoint to store compilation results:
    - if [ -n "${SCCACHE_ENDPOINT}" ]; then export RUSTC_WRAPPER=/usr/local/cargo/bin/sccache; fi
  script:
    - rustc --version && cargo --version # Print version info for debugging
    - cargo fmt --all -- --check
    - "cargo test --color always --workspace --verbose --locked --no-fail-fast -- -Z unstable-options --format json | gitlab-report -p test > $CI_PROJECT_DIR/report.xml"
    - "cargo clippy --color always --verbose --message-format=json | gitlab-report -p clippy > $CI_PROJECT_DIR/gl-code-quality-report.json"
  artifacts:
    when: always
    reports:
      junit: report.xml
      codequality: gl-code-quality-report.json

test:sytest:
  stage: "test"
  allow_failure: true
  needs:
    - "build:debug:cargo:x86_64-unknown-linux-musl"
  image:
    name: "valkum/sytest-conduit:latest"
    entrypoint: [""]
  tags: ["docker"]
  variables:
    PLUGINS: "https://github.com/valkum/sytest_conduit/archive/master.tar.gz"
  before_script:
    - "mkdir -p /app"
    - "cp ./conduit-debug-x86_64-unknown-linux-musl /app/conduit"
    - "chmod +x /app/conduit"
    - "rm -rf /src && ln -s $CI_PROJECT_DIR/ /src"
    - "mkdir -p /work/server-0/database/ && mkdir -p /work/server-1/database/ && mkdir -p /work/server-2/database/"
    - "cd /"
  script:
    - "SYTEST_EXIT_CODE=0"
    - "/bootstrap.sh conduit || SYTEST_EXIT_CODE=1"
    - 'perl /sytest/tap-to-junit-xml.pl --puretap --input /logs/results.tap --output $CI_PROJECT_DIR/sytest.xml "Sytest" && cp /logs/results.tap $CI_PROJECT_DIR/results.tap'
    - "exit $SYTEST_EXIT_CODE"
  artifacts:
    when: always
    paths:
      - "$CI_PROJECT_DIR/sytest.xml"
      - "$CI_PROJECT_DIR/results.tap"
    reports:
      junit: "$CI_PROJECT_DIR/sytest.xml"

# --------------------------------------------------------------------- #
#  Store binaries as package so they have download urls                 #
# --------------------------------------------------------------------- #

publish:package:
  stage: "upload artifacts"
  needs:
    - "build:release:cargo:x86_64-unknown-linux-musl"
    - "build:release:cargo:arm-unknown-linux-musleabihf"
    - "build:release:cargo:armv7-unknown-linux-musleabihf"
    - "build:release:cargo:aarch64-unknown-linux-musl"
  #    - "build:cargo-deb:x86_64-unknown-linux-gnu"
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
    - if: '$CI_COMMIT_BRANCH == "next"'
    - if: "$CI_COMMIT_TAG"
  image: curlimages/curl:latest
  tags: ["docker"]
  variables:
    GIT_STRATEGY: "none" # Don't need a clean copy of the code, we just operate on artifacts
  script:
    - 'BASE_URL="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/conduit-${CI_COMMIT_REF_SLUG}/build-${CI_PIPELINE_ID}"'
    - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file conduit-x86_64-unknown-linux-musl "${BASE_URL}/conduit-x86_64-unknown-linux-musl"'
    - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file conduit-arm-unknown-linux-musleabihf "${BASE_URL}/conduit-arm-unknown-linux-musleabihf"'
    - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file conduit-armv7-unknown-linux-musleabihf "${BASE_URL}/conduit-armv7-unknown-linux-musleabihf"'
    - 'curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file conduit-aarch64-unknown-linux-musl "${BASE_URL}/conduit-aarch64-unknown-linux-musl"'

# Avoid duplicate pipelines
# See: https://docs.gitlab.com/ee/ci/yaml/workflow.html#switch-between-branch-pipelines-and-merge-request-pipelines
workflow:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
    - if: "$CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS"
      when: never
    - if: "$CI_COMMIT_BRANCH"