Variables

Covered Gitlab features : variables

Like "Perceval de Galles" says, "what’s matters is the value". It’s of course better in French with "De toute façon, ce qui compte, c’est les valeurs". This is a rule coming from the famous game called the Sirop. Here, we will see how much the value and variables are important in our quest of the great CI.

sirop

Est-ce que vous avez assez de dés pour remplir un seau comme ça ? [Le tavernier n’en a que trois] C’est plus facile d’en tirer cent ou deux-cents d’un coup et d’additionner, parce que là avec trois, on va être obligés de tirer quarante fois de suite à chaque tour.

— Perceval
Kaamelott S03E50

Global Variable(s)

You will use the project created at the previous step

The main goal is to simplify each of your jobs by using variables

📝 Instructions

➡️ Edit the the .gitlab-ci.yml

➡️ Add, at the top of the file a key name variables:

➡️ Add a key inside the variables block named MAVEN_OPTS with the following value:

"-Dhttps.protocols=TLSv1.2 -Dmaven.color=false -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"

➡️ Add another key inside the variables block named MAVEN_CLI_OPTS with the following value:

"--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
You can use the value MAVEN_OPTS and MAVEN_CLI_OPTS in any of your jobs and so, they can be accessed by any executable 👍
Those values are advised by Maven project when running inside a Continuous Integration System

➡️ Replace all the ./mvnw --batch-mode in the file by ./mvnw $MAVEN_CLI_OPTS in each job. You should have something looking like this

image: openjdk:8-jdk-slim

variables:
  MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.color=false -Dmaven.repo.local=.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"

stages:
  - tests
  - build

build:
  stage: build
  script:
    - ./mvnw $MAVEN_CLI_OPTS install -DskipTests

unit:
  stage: tests
  script:
    - ./mvnw $MAVEN_CLI_OPTS test -Dtests.tags="unit"

e2e:
  stage: tests
  script:
    - ./mvnw $MAVEN_CLI_OPTS test -Dtests.tags="e2e"

➡️ Use git to commit and push your modifications :

git add .
git commit -m "chore(ci): add global variables to configure maven"
git push


Local Variable(s)

📝 Instructions

In the previous step, we leverage the power of global variable to simplify our CI scripts. In this step we will define some variables just for one job.

➡️ Edit the the .gitlab-ci.yml

➡️ Add a variables block inside the unit job

➡️ Add a key name SCOPE with value unit

➡️ Add a variables block inside the e2e job

➡️ Add a key name SCOPE with value e2e

➡️ Remove in each script block the parameter -Dtests.tags="…​"

➡️ We should have a .gitlab-ci.yml like this

image: openjdk:8-jdk-slim

variables:
  MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.color=false -Dmaven.repo.local=.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"

stages:
  - tests
  - build

build:
  stage: build
  script:
    - ./mvnw $MAVEN_CLI_OPTS install -DskipTests

unit:
  stage: tests
  variables:
    SCOPE: unit
  script:
    - ./mvnw $MAVEN_CLI_OPTS test

e2e:
  stage: tests
  variables:
    SCOPE: e2e
  script:
    - ./mvnw $MAVEN_CLI_OPTS test
The SCOPE variable trigger a specific profile in Maven enabling a specific set of tests

➡️ Use git to commit and push your modifications :

git add .
git commit -m "chore(ci): add local variables to configure maven tests"
git push


Environment Variable(s)

Some variables are too sensible to be put under git version control. So, we can use a feature provided by Gitlab to host some variables out of the git and still be able to reference it from the .gitlab-ci.yml.

Mainly, those variables are credentials which shouldn’t be exposed to everyone

In this step, we will publish our application as a docker image in the Gitlab docker registry.

📝 Instructions

➡️ Go to the CI/CD Settings of your project

https://gitlab.com/gitlab-workshop/swiss-institute-of-bioinformatics/<your-project>/settings/ci_cd

➡️ Add REGISTRY_USER variable with following value : gitlab-ci-token. The variable should not be masked.

env variables
Don’t forget to save 😊

➡️ Modify the command in the build job to trigger a docker image creation

./mvnw $MAVEN_CLI_OPTS clean compile jib:build

➡️ Use git to commit and push your modifications :

git add .
git commit -m "chore(ci): add global variables to publish application in the gitlab docker registry"
git push

➡️ Follow your job execution using :

https://gitlab.com/gitlab-workshop/swiss-institute-of-bioinformatics/<your-project>/-/jobs

➡️ View your deployed image on Gitlab docker registry using :

https://gitlab.com/gitlab-workshop/swiss-institute-of-bioinformatics/<your-project>/container_registry

Jib permits to build java docker images without the docker binary, read this great article (french only) for more information
REGISTRY_USER environment variable is used in the pom.xml file here

Predefined Variables

You could also use predefined variables

Theses variables are provided by the Gitlab runner and can be used in your jobs.

📝 Instructions

➡️ Edit your .gitlab-ci.yml and modify your SCOPE variable values with : $CI_JOB_NAME

➡️ Use git to commit and push your modifications :

git add .
git commit -m "chore(ci): use job name as scope for triggering tests"
git push
The C.I now use the job name as value for your test SCOPE variable