Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set gitlab-ci.yml variable via bash command

variables:
  CUSTOM_NODE_VERSION: '$${cat .nvmrc}'

I'd like for the variable CUSTOM_NODE_VERSION to be populated via the contents of the .nvmrc file (which is located in the projects root directory). How does one do this in the gitlab-ci.yml file?

The example above isn't working. I've also tried the following:

  • CUSTOM_NODE_VERSION: $(cat .nvmrc) -> (cat .nvmrc)
  • CUSTOM_NODE_VERSION: "$(cat .nvmrc)" -> (cat .nvmrc)
  • CUSTOM_NODE_VERSION: '$(cat .nvmrc)' -> (cat .nvmrc)
  • CUSTOM_NODE_VERSION: ${cat .nvmrc} -> (empty string)
  • CUSTOM_NODE_VERSION: '${cat .nvmrc}' -> (empty string)
  • CUSTOM_NODE_VERSION: "${cat .nvmrc}" -> (empty string)

It works if I put it in the before_script like the following:

before_script:
  - CUSTOM_NODE_VERSION=$(cat .nvmrc)

But it isn't accessible to the following part of the gitlab-ci.yml file:

lint:
  stage: Test
  image: node:$CUSTOM_NODE_VERSION
like image 607
basickarl Avatar asked Feb 28 '18 10:02

basickarl


People also ask

What is set in bash?

The set command in Bash allows you to control the behavior of your scripts by managing specific flags and properties. These safeguards guarantee that your scripts are on the right track and that Bash's odd behavior does not cause problems.

How many ways we can specify variables in GitLab?

There are two places defined variables can be used.


3 Answers

I also wanted to use a version string in a .gitlab-ci.yml file but for appending it to a Docker image name. I did it like this:

build:
  stage: build_images
  script:
    - API_VERSION=v$(grep -E -o "(version = )(.*)" pyproject.toml | cut -d\" -f2)
    - echo $API_VERSION
    # Build and push new images for staging.
    - docker pull $API_STAGING:latest
    - docker build --cache-from $API_STAGING:latest >-
        -t $API_STAGING:latest >-
        -t $API_STAGING:$CI_COMMIT_SHORT_SHA >-
        -t $API_STAGING:$API_VERSION >-
        -f dockerfiles/Dockerfile.staging .
    - docker push $API_STAGING
  tags:
    - build

The key line here is API_VERSION=v$(grep -E -o "(version = )(.*)" pyproject.toml | cut -d\" -f2).

Explanation: the string I'm trying to read in pyproject.toml is something like version = "0.17.1", and the result I wanted was v0.17.1

  • v is just a string I want to prepend to my version number
  • -E (--extended-regexp) invokes grep as egrep; allows use of special regexp characters
  • -o (--only-matching) doesn't make a difference in my use-case, but might be helpful in other cases (I'm not sure)
  • (version = ) and (.*): the two capture groups; the latter one captures anything after the space after the equal sign
  • Running just $ grep -E -o "(version = )(.*)" pyproject.toml will result in version = "0.1.0", so I'm not using the capture groups; instead, I'm using cut
  • cut "cut[s] out selected portions of each line of a file"
  • -d\" sets the delimiter to a double-quote instead of the default (tab)
  • -f2 specifies the fields to return; a value of 1 would return everything before the first quote, i.e., version = , so 2 returns everything before the second quote and after the first, and 3 returns nothing in this example since there is no third double-quote-separated field
  • echo $API_VERSION just to see that it's working
like image 61
acetone Avatar answered Oct 24 '22 17:10

acetone


There are some parts of .gitlab-ci.yml where variables are usable and some parts where they are not.

The .yml file is parsed in Gitlab itself and then the commands are executed by the runner. So setting a variable that is used in the job config is not possible at this point. You could use a pre-defined Secret Variable although that doesnot seem to fix your need.

There are issues tracking the documentation of what you can and cannot do:

  • Variables in docker service alias not supported
  • Document the list of places where you are allowed to use an env variable in .gitlab-ci.yml
like image 43
Stefan van Gastel Avatar answered Oct 24 '22 19:10

Stefan van Gastel


You might want to try:

before_script:
  - export CUSTOM_NODE_VERSION=$(cat .nvmrc)

In your script to make the variable available to subsequent shells.

like image 31
NIGHTSCROLLER Avatar answered Oct 24 '22 17:10

NIGHTSCROLLER