From 0df354f2b929c530485e65c0c5f1a0e4df8b88a1 Mon Sep 17 00:00:00 2001 From: Bastien Abadie <abadie@teklia.com> Date: Tue, 9 Nov 2021 13:59:36 +0000 Subject: [PATCH] Build docker image on CI --- .gitlab-ci.yml | 15 +++++++++++++++ ci/build.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100755 ci/build.sh diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index fec24b7a7..29144715f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -85,6 +85,21 @@ frontend-build: script: - npm run production +docker-build: + stage: build + image: docker:20.10 + services: + - docker:dind + + # Needed to avoid triggering global npm ci + before_script: + - '' + except: + - schedules + + script: + - ci/build.sh + frontend-deploy: image: python:3-alpine stage: deploy diff --git a/ci/build.sh b/ci/build.sh new file mode 100755 index 000000000..861b3ea6d --- /dev/null +++ b/ci/build.sh @@ -0,0 +1,28 @@ +#!/bin/sh -e +# Build and publish a Docker image. +# Requires CI_PROJECT_DIR and CI_REGISTRY_IMAGE to be set. +# VERSION defaults to latest. +# Will only push an image if the commit is on the main branch or if a tag is defined. +# Will automatically login to a registry if CI_REGISTRY, CI_REGISTRY_USER and CI_REGISTRY_PASSWORD are set. + +VERSION=${CI_COMMIT_TAG:-latest} + +if [ -z "$VERSION" -o -z "$CI_PROJECT_DIR" -o -z "$CI_REGISTRY_IMAGE" ]; then + echo Missing environment variables to build the image… + exit 1 +fi + +docker build $CI_PROJECT_DIR -f $CI_PROJECT_DIR/Dockerfile -t "$CI_REGISTRY_IMAGE:$VERSION" + +# Publish the image on the main branch or on a tag +if [ "$CI_COMMIT_REF_NAME" = "master" -o -n "$CI_COMMIT_TAG" ]; then + if [ -n "$CI_REGISTRY" -a -n "$CI_REGISTRY_USER" -a -n "$CI_REGISTRY_PASSWORD" ]; then + echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY + docker push "$CI_REGISTRY_IMAGE:$VERSION" + else + echo "Missing environment variables to log in to the container registry…" + exit 1 + fi +else + echo "The build was not published to the repository registry (only for main branch or tags)…" +fi -- GitLab