From 6150489ecf1eeb0d7ef68f7e553e472520fc8a82 Mon Sep 17 00:00:00 2001 From: Tortue Torche <126358-tortuetorche@users.noreply.gitlab.com> Date: Mon, 27 Jan 2020 15:51:02 +0100 Subject: [PATCH] Fix scheduled pipeline who update Docker images for the latest stable git tag Escape $CI_COMMIT_SHA variable in the GitLab CI configuration and tests. And evaluates its value at runtime. See: https://docs.gitlab.com/12.7/ee/ci/variables/README.html#gitlab-ciyml-defined-variables --- .gitlab-ci.yml | 26 +++++++++++++++++--------- tests/run.sh | 18 ++++++++++++++++-- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 6672c86..3880bd4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,8 +6,8 @@ variables: TRAEFIK_VERSION: "2.1" PORTAINER_VERSION: latest PSU_IMAGE: ${CI_REGISTRY_IMAGE}/builds - PSU_TAG: $CI_COMMIT_SHA - PSU_TAG_CORE: core-${CI_COMMIT_SHA} + PSU_TAG: $$CI_COMMIT_SHA + PSU_TAG_CORE: core-$$CI_COMMIT_SHA DOCKER_REGISTRY_IMAGE: $DOCKER_REGISTRY/$CI_PROJECT_PATH stages: @@ -29,9 +29,16 @@ stages: - setup_docker - registry_login - cd ${CONTAINER_FOLDER:-.} + - | + CONTAINER_COMMIT_SHA=$(if [ -n "$CONTAINER_COMMIT_SHA" ]; then + eval echo "$CONTAINER_COMMIT_SHA"; + else + echo "$CI_COMMIT_SHA"; + fi) + export CONTAINER_COMMIT_SHA - docker pull $CONTAINER_IMAGE:${CONTAINER_VERSION:-latest} || true - - docker build --pull $(if [[ -n "$DOCKER_CACHE_DISABLED" ]]; then echo "--no-cache "; else echo "--cache-from $CONTAINER_IMAGE:${CONTAINER_VERSION:-latest}"; fi) --file ${DOCKER_FILE:-Dockerfile} --tag $CONTAINER_IMAGE:${CONTAINER_COMMIT_SHA:-$CI_COMMIT_SHA} --tag $CONTAINER_IMAGE:${CONTAINER_VERSION:-latest} --build-arg HTTP_PROXY="$HTTP_PROXY" --build-arg http_proxy="$http_proxy" --build-arg HTTPS_PROXY="$HTTPS_PROXY" --build-arg https_proxy="$https_proxy" --build-arg FTP_PROXY="$FTP_PROXY" --build-arg ftp_proxy="$ftp_proxy" --build-arg NO_PROXY="$NO_PROXY" --build-arg no_proxy="$no_proxy" . - - docker push $CONTAINER_IMAGE:${CONTAINER_COMMIT_SHA:-$CI_COMMIT_SHA} + - docker build --pull $(if [[ -n "$DOCKER_CACHE_DISABLED" ]]; then echo "--no-cache "; else echo "--cache-from $CONTAINER_IMAGE:${CONTAINER_VERSION:-latest}"; fi) --file ${DOCKER_FILE:-Dockerfile} --tag $CONTAINER_IMAGE:$CONTAINER_COMMIT_SHA --tag $CONTAINER_IMAGE:${CONTAINER_VERSION:-latest} --build-arg HTTP_PROXY="$HTTP_PROXY" --build-arg http_proxy="$http_proxy" --build-arg HTTPS_PROXY="$HTTPS_PROXY" --build-arg https_proxy="$https_proxy" --build-arg FTP_PROXY="$FTP_PROXY" --build-arg ftp_proxy="$ftp_proxy" --build-arg NO_PROXY="$NO_PROXY" --build-arg no_proxy="$no_proxy" . + - docker push $CONTAINER_IMAGE:$CONTAINER_COMMIT_SHA - docker push $CONTAINER_IMAGE:${CONTAINER_VERSION:-latest} build:main: @@ -46,7 +53,7 @@ build:debian: variables: CONTAINER_NAME: $CI_PROJECT_NAME CONTAINER_VERSION: $CI_COMMIT_REF_SLUG-build-debian - CONTAINER_COMMIT_SHA: debian-$CI_COMMIT_SHA + CONTAINER_COMMIT_SHA: debian-$$CI_COMMIT_SHA DOCKER_FILE: Dockerfile.debian CONTAINER_IMAGE: ${CI_REGISTRY_IMAGE}/builds @@ -55,15 +62,16 @@ build:core: variables: CONTAINER_NAME: $CI_PROJECT_NAME CONTAINER_VERSION: $CI_COMMIT_REF_SLUG-build-core - CONTAINER_COMMIT_SHA: core-$CI_COMMIT_SHA + CONTAINER_COMMIT_SHA: core-$$CI_COMMIT_SHA DOCKER_FILE: Dockerfile.core CONTAINER_IMAGE: ${CI_REGISTRY_IMAGE}/builds + build:debian-core: <<: *build_definition variables: CONTAINER_NAME: $CI_PROJECT_NAME CONTAINER_VERSION: $CI_COMMIT_REF_SLUG-build-debian-core - CONTAINER_COMMIT_SHA: debian-core-$CI_COMMIT_SHA + CONTAINER_COMMIT_SHA: debian-core-$$CI_COMMIT_SHA DOCKER_FILE: Dockerfile.debian-core CONTAINER_IMAGE: ${CI_REGISTRY_IMAGE}/builds @@ -99,8 +107,8 @@ test:portainer-latest: test:portainer-latest:debian: <<: *test_definition variables: - PSU_TAG: debian-${CI_COMMIT_SHA} - PSU_TAG_CORE: debian-core-${CI_COMMIT_SHA} + PSU_TAG: debian-$$CI_COMMIT_SHA + PSU_TAG_CORE: debian-core-$$CI_COMMIT_SHA release: stage: deploy diff --git a/tests/run.sh b/tests/run.sh index cb3b8b3..7b2dff0 100644 --- a/tests/run.sh +++ b/tests/run.sh @@ -9,15 +9,29 @@ PSU_URL="https://portainer.$BASE_DOMAIN" PSU_USER="admin" PSU_PASSWORD="mypassword" +PSU_TAG=$(if [ -n "$PSU_TAG" ]; then + eval echo "$PSU_TAG"; + else + echo "$CI_COMMIT_SHA"; + fi) +export PSU_TAG + +PSU_TAG_CORE=$(if [ -n "$PSU_TAG_CORE" ]; then + eval echo "$PSU_TAG_CORE"; + else + echo "core-$CI_COMMIT_SHA"; + fi) +export PSU_TAG_CORE + # Change working directory to 'tests/' cd "$(dirname "$0")" function psu_wrapper() { - docker run --rm $PSU_IMAGE:${PSU_TAG:-$CI_COMMIT_SHA} "$@" + docker run --rm $PSU_IMAGE:$PSU_TAG "$@" } function psu_core_wrapper() { - docker run --rm $PSU_IMAGE:${PSU_TAG_CORE:-core-$CI_COMMIT_SHA} "$@" + docker run --rm $PSU_IMAGE:$PSU_TAG_CORE "$@" } function application_exists() {