How to Generate Random Number in Gitlab Yaml File

I have YAML file in my Gitlab branch. As per the requirement i want to create one random mergeID in YAML file.

2

2 Answers

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

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:

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest