8. Build Options
In this section we’ll talk about global pipeline configuration. There are different options for global pipeline configuration. This helps to reduce code duplication in your pipeline.
8.1: Global pipeline configuration default
You can set global defaults for some keywords. Jobs that do not define one or more of the listed keywords use the value defined in the default section, therefore you can simply overwrite default values, by specifying the values explicitly in a job.
There are a couple of default settings you can define inside a default section:
after_scriptartifactsbefore_scriptcacheimageinterruptibleretryservicestagstimeout
Let’s have a look at an example
default:
before_script:
- "This will be executed as default if no other before script is set"
job1:
script:
- echo "This script executes first. When it completes, the job's `after_script` executes."
job2:
before_script:
- echo "This script executes before the script block"
script:
- echo "This script executes first. When it completes, the job's `after_script` executes."
after_script:
- echo "Execute this script after the normal script."
This produces the following output:
This will be executed as default if no other before script is set
This script executes first. When it completes, the jobs `after_script` executes.
This script executes before the script block
This script executes first. When it completes, the jobs `after_script` executes.
Execute this script after the normal script.
Task 8.2: Global pipeline configuration lab
In this task you’re going to configure several things for your pipeline.
- Create the default section
- Create a global timeout of 5 minutes.
- Add a retry number of 2 times to the
infojob, this means, that the job will be retried to succeed twice.
Edit the .gitlab-ci.yml, commit and push your changes and then verify the output of the pipeline run.
Solution
Updated .gitlab-ci.yml file for this lab:
show solution
stages:
- info
- build
- test
- package
variables:
GIT_STRATEGY: 'clone'
COMPILE: 'false'
IMAGE_HOST: 'quay.io'
IMAGE_REPOSITORY: 'puzzle'
IMAGE_NAME: 'example-spring-boot'
default:
timeout: 5 minutes
info:
stage: info
retry: 2
script:
- echo "This is your first stage (ツ)"
- echo "Username is ${USERNAME} with ${PASSWORD}"
build_application:
stage: build
tags:
- build
- mobiliar
image: registry.access.redhat.com/ubi9/openjdk-17:latest
script:
- ./gradlew assemble
test_application:
stage: test
image: registry.access.redhat.com/ubi9/openjdk-17:latest
script:
- ./gradlew check
artifacts:
when: always
reports:
junit: build/test-results/test/**/TEST-*.xml
build_image:
image: diemobiliar.azurecr.io/dlp-cicd-dockercli-image:254
tags:
- build
- mobiliar
services:
- docker:dind
stage: package
before_script:
- docker info
script:
- docker build --no-cache -t $IMAGE_NAME .
- IMAGE_PATH=$IMAGE_HOST/$IMAGE_REPOSITORY/$IMAGE_NAME:${CI_COMMIT_SHA:0:8}
- echo "docker image path is - $IMAGE_PATH"
- docker tag $IMAGE_NAME $IMAGE_PATH
# - docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} ${IMAGE_HOST}
# - docker push $IMAGE_PATH