Can you use a varialben to build a variable in Gitlab CI? - variables

Is it possible in a gitlab CICD pipline to build a variable dynamically with a variable?
Sample:
i have a variable in gitlab "TEST_MASTER".
script:
- echo "$TEST_"$CI_COMMIT_BRANCH""
I need the result from the variable TEST_MASTER, but the part of MASTER must come from the branch variable.

That looks like a bash script to me so assuming TEST_MASTER already has a value, you should be able echo it like this:
script:
- myvar=TEST_"$CI_COMMIT_BRANCH"
echo "${!myvar}"
For more information, check this question and it's answers

Related

Newman pass variable from GitLab

I have pipeline on GitLab and there the variable - ENV_VAR. This variable is changing based on branch for pipeline.
In the same yml file I have script with newman, where I want to pass this variable like this -> newman run ... -e test/apis/$ENV_VAR_environment.json
But the issue I have right now is that it seems the variable is not being passed as i want. The pipeline shows error - cannot read the test/apis/here_should_be_the_variable_name.json
Is there a way to pass this variable into the file source?
It looks like you only need to enclose the variable name in braces:
-e test/apis/${ENV_VAR}_environment.json
because test/apis/$ENV_VAR_environment.json means that it looks for $ENV_VAR_environment variable which obviously does not exist.

with gitlab ci, can I do something like "if tag == 'xx'"?

with Gitlab CI, I am trying to do something like this:
script:
- do some stuff
- if tag / branch name == "xx":
- do additional things
is there wa way to do this?
Gitlab exports many predefined environmental variables to the script environment, one of them is CI_COMMIT_TAG, which is set when building on tags. With it, assuming the script is interpreted with posix shell, you could normally script it with for example if [ "${CI_COMMIT_TAG}" == "xx" ]; then something; fi.

how to pass value from commit to GitLab CI pipeline as variable?

I need to dynamically pass value to GitLab CI pipeline to pass the value further to jobs. The problem is: the value cannot be stored in the code and no pipeline reconfiguration should be needed (e.g. I can pass the value in "variables" section of .gitlab-ci.yml but it means store value in the code, or changes in "Environment variables" section of "CI / CD Settings" means manual reconfiguration). Also, branch name cannot be used for that purpose too.
It is not a secret string but a keyword which modifies pipeline execution.
So, how can I do it?
You didn't specify the source of this value.
You say "pass value from commit to ..."
If it's some meta information about the commit itself, look at the list of Predefined environment variables
There's quite a lot of vars named CI_COMMIT_* which might work for you.
However,
if it's some value that you generate in the pipeline in one job and want to pass to another job - it's a different case.
There is a long-living request to Pass variables between jobs, which is still not implemented.
The workaround for this moment is to use artifacts - files to pass information between jobs in stages.
Our use case is to extract Java app version from pom.xml and pass it to some packaging job later.
Here is how we do it in our .gitlab-ci.yml:
...
variables:
VARIABLES_FILE: ./variables.txt # "." is required for image that have sh not bash
...
get-version:
stage: prepare
image: ...
script:
- APP_VERSION=...
- echo "export APP_VERSION=$APP_VERSION" > $VARIABLES_FILE
artifacts:
paths:
- $VARIABLES_FILE
...
package:
stage: package
image: ...
script:
- source $VARIABLES_FILE
- echo "Use env var APP_VERSION here as you like ..."

How to use variables while matching branches in gitlab-ci?

How can I match a branch name a variable? If I set the variable to 1.1.0 I want it to only match release branches that are for that version. I can't seem to do variable replacement here or in a regex (as seen commented out). Are there any options? The 1.1.0 would actually be coming from the project variables, not hard coded in the file so hard coding it really isn't an option here.
This does not seem to do variable replacement.
stages:
- build
variables:
buildNum: $CI_PIPELINE_ID
stagingVersion: 1.1.0
before_script:
- export MYTEAM_BUILD_NUM=${buildNum}
- export MYTEAM_VERSION=${stagingVersion}
build_staging:
stage: build
only:
- release/v${stagingVersion}#myTeam/myProject
# - /^release\/v${stagingVersion}#myTeam\/myProject$/
script:
- doStuff
I was able to come up with a hacky solution to achieve this. It's not great but it gets the job done. It will fail jobs that match the branch criteria but do not match the branch version criteria:
only:
- /^release\/v\d+.\d+.\d+$/#myTeam/myProject
script:
- if ! [ "${CI_COMMIT_REF_NAME}#${CI_PROJECT_PATH}" = "release/v${stagingVersion}#myTeam/myProject" ]; then echo "Staging branch? NO; Exiting"; exit 1; else echo "Staging branch? YES; Continuing"; fi
The only section first filters down to branches that match the release/v0.0.0#myTeam/myProject format
Then the first line of the script runs a bash command which compares the stagingVersion (set in gitlab project variables to the current version for the staging environment) and the $CI_COMMIT_REF_NAME
If the branches match, it prints a success message, if they do not match, it runs exit 1 and fails the build

Does Gitlab-CI support variable expansion within only:refs?

I've got a large .gitlab-ci.yml file with lots of jobs in it. Many of these jobs are filtered to only run on certain branches. When managing this file it would be convenient to define the names of these branches as variables at the top of the file so that only the variables need to be updated if the branch names change. This is a pretty standard practice for constants in most programming languages.
Unfortunately, it doesn't look like this works in Gitlab-CI:
variables:
THIS_DOES_NOT_WORK: "this_works"
lots:
only:
refs:
- this_works
script:
- echo "lots"
of:
only:
refs:
- $THIS_DOES_NOT_WORK
script:
- echo "of"
jobs:
only:
refs:
- $THIS_DOES_NOT_WORK
script:
- echo "jobs"
In the above example, only the "lots" job will be run since the THIS_DOES_NOT_WORK variable is not expanded in the "of" and "jobs" jobs.
The closest documentation which I can find doesn't mention anything about the only:refs keyword. It does go into details on the only:variables keyword. This keyword could provide a nice workaround if we could do something like this instead:
variables:
THIS_DOES_NOT_WORK: "this_works"
lots:
only:
variables:
- $CI_COMMIT_REF_NAME == "this_works"
script:
- echo "lots"
of:
only:
variables:
- $CI_COMMIT_REF_NAME == $THIS_DOES_NOT_WORK
script:
- echo "of"
jobs:
only:
variables:
- $CI_COMMIT_REF_NAME == $THIS_DOES_NOT_WORK
script:
- echo "jobs"
In this case it's explicitly stated in the documentation that this won't work.
The only:variables keyword used for filtering on variable comparisons is ironically incapable of expanding variables.
Is there some other workaround here? Am I missing something?
According to the https://docs.gitlab.com/ee/ci/variables/where_variables_can_be_used.html, it seems that the capability of the ci pipeline increased and it can actually expand variables, now. For me the solution was to use parts of your example:
jobs:
only:
variables:
- $THIS_DOES_WORK == $CI_COMMIT_REF_NAME
script:
- echo "jobs"
In my case $THIS_DOES_WORK is a variable passed from the gitlab ui via the CI/CD variables tab. Gitlab states the constraints variables used in this scope have:
The variable must be in the form of $variable. Not supported are the following:
Variables that are based on the environment’s name (CI_ENVIRONMENT_NAME, CI_ENVIRONMENT_SLUG).
Any other variables related to environment (currently only CI_ENVIRONMENT_URL).
Persisted variables.
Additionally, you should pay attention that the variable is not set to protected, if working on an unprotected branch.