diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0283fdf..336b6b6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,6 +3,7 @@ image: docker:20.10.22 stages: - test - build + - push ############## # TEST STAGE # @@ -37,10 +38,42 @@ build: services: - docker:dind before_script: - - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY + - echo -n $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY script: - - docker build --pull -t "$CI_REGISTRY_IMAGE" . - - docker push "$CI_REGISTRY_IMAGE" + # fetches the latest image (not failing if image is not found) + - docker pull $CI_REGISTRY_IMAGE:latest || true + - > + docker build + --pull + --cache-from $CI_REGISTRY_IMAGE:latest + --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA + . + - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA + +############## +# PUSH STAGE # +############## + +push latest: + stage: push + variables: + GIT_STRATEGY: none + before_script: + - echo -n $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY only: - main - when: manual + script: + - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA + - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:latest + - docker push $CI_REGISTRY_IMAGE:latest + +push tag: + stage: push + variables: + GIT_STRATEGY: none + only: + - tags + script: + - docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA + - docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME + - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME