Use a manual job to trigger a selective build/push

This commit is contained in:
Kamil Trzciński 2021-02-02 13:30:18 +01:00
parent a9204d345d
commit 3acad93b15
4 changed files with 34 additions and 61 deletions

View file

@ -17,43 +17,51 @@ stages:
variables:
DOCKER_DRIVER: overlay2
DOCKER_HOST: tcp://docker:2375
FORCE_BUILD: "false"
# Run test job always
.test:
stage: test
rules:
- when: always
# Run build job only on a default branch when triggered manually
.build:
stage: build
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: manual
.test_build: &test_build
stage: test
extends: .test
script:
# Hack to set an array in /bin/sh
# http://unix.stackexchange.com/a/137571
- set -- $CI_JOB_NAME
- docker build -f "Dockerfile.$1" .
except:
- master
- docker pull "$CI_REGISTRY_IMAGE:$1" || true
- docker build --from-cache="$CI_REGISTRY_IMAGE:$1" -f "Dockerfile.$1" .
.build_and_deploy: &build_and_deploy
stage: build
extends: .build
script:
- docker build --label "ci_pipeline_url=$CI_PIPELINE_URL" --label "ci_job_url=$CI_JOB_URL" -t "$CI_REGISTRY_IMAGE:$CI_JOB_NAME" -f "Dockerfile.$CI_JOB_NAME" .
- docker pull "$CI_REGISTRY_IMAGE:$1" || true
- docker build --from-cache="$CI_REGISTRY_IMAGE:$1" --label "ci_pipeline_url=$CI_PIPELINE_URL" --label "ci_job_url=$CI_JOB_URL" -t "$CI_REGISTRY_IMAGE:$CI_JOB_NAME" -f "Dockerfile.$CI_JOB_NAME" .
- docker push "$CI_REGISTRY_IMAGE:$CI_JOB_NAME"
only:
- master
.test_custom: &test_custom
stage: test
extends: .test
script:
# Hack to set an array in /bin/sh
# http://unix.stackexchange.com/a/137571
- set -- $CI_JOB_NAME
- ./scripts/custom-docker-build $1
except:
- master
rules:
- when: always
.build_and_deploy_custom: &build_and_deploy_custom
stage: build
extends: .build
script:
- ./scripts/custom-docker-build $CI_JOB_NAME --label "ci_pipeline_url=$CI_PIPELINE_URL" --label "ci_job_url=$CI_JOB_URL" -t "$CI_REGISTRY_IMAGE:$CI_JOB_NAME"
- push_if_needed "$CI_REGISTRY_IMAGE:$CI_JOB_NAME"
only:
- master
- docker push "$CI_REGISTRY_IMAGE:$CI_JOB_NAME"
# Tests