I have YAML file in my Gitlab branch. As per the requirement i want to create one random mergeID in YAML file.
Asked
Active
Viewed 7,680 times
3
-
Can you give code that you have tested? – Anic17 Jul 11 '20 at 11:27
-
1YAML is not a programming language and therefore can't generate random numbers. The gitlab-ci-runner tag makes no sense because that is configured with TOML, not YAML. – flyx Jul 11 '20 at 14:09
2 Answers
7
Another simple solution without using script is to use the gitlab env variable CI_JOB_ID
.
It's not a random number, but it's unique.
variables:
MERGE_ID: $CI_JOB_ID

Tomas
- 1,849
- 1
- 13
- 13
4
If you're referring to the gitlab-ci.yml file, this is possible. Each GitLab job defined in the YAML file provides a script
section where you can define bash commands that will get executed by GitLab when the job is run. If you want more complex operations, you can call Python scripts from the script section.
Fortunately, in this case, bash has a built-in $RANDOM
function, described in How to generate random number in Bash?.
gitlab-ci.yml:
my-job:
stage: my-stage
script:
# Generate random number from 1 to 10:
- let MR_ID=$((1 + RANDOM % 10))
More info on GitLab scripts: https://docs.gitlab.com/ee/ci/yaml/#script

DV82XL
- 5,350
- 5
- 30
- 59
-
-
Here I have one more requirement i.e: Generate seven digit random number. So I modified the code like below - let SevenDigitMR_ID=RANDOM -MINIMUM 999999 -MAXIMUM 9999999 - echo $SevenDigitMR_ID But still it's generate the same five digit random number only. By default $RANDOM generate the number up to 5 digits only. – ramkee gurrala Jul 15 '20 at 09:45
-
Search stackoverflow for an answer. If you can't find one, post another question ;) – DV82XL Jul 15 '20 at 11:41