27

I am running the below code section in gitlab-ci.yml file:

  script:
- pip install --upgrade pip
- cd ./TestAutomation
- pip install -r ./requirements.txt

Below are the keys and values. So I have to pass any values to the pipeline with key as a variable ENV : dev

I have added all the above three variables in the GitLab CI CD variables sections by expanding them. just added a single value along with key

I also found like we can add variables in the .yml file itself as below. I am not sure how we can add multiple values for one key

variables:
 TEST:
   value: "some value" # this would be the default value
   description: "This variable makes cakes delicious"

When I run the pipeline I am getting errors as looks like these variables and values are not injected properly.

More details:

And the same error I am getting while running the pipeline. Hence my suspect is like Category variable is not injected properly when I am running through the pipeline

If needed I will show it on the share screen

What I have observed is --the values associated with keys which I am passing as parameter or variables , those are not injected or replaced instead of key. So ideally ${Category} should be replaced with value smoke etc

5
  • I'm a bit confused about what you're trying to do. Are you trying to run the same job multiple times with slightly different variables? If so, look at parallel: matrix in gitlab's CI reference. Are you trying to get a dropdown to show up when manually running a CI/CD pipeline and entering variable values? That's not possible right now. Commented Nov 22, 2021 at 23:29
  • @Patrick, I am trying to run just pipeline by passing any single values I want to pass ENV, BROWSER and Category as key value When I am passing all 3 key value and running pipeline , I am getting error and the same error I get locally when I am not passing Category key value hence it looks like that is not injected properly. I want to run job once not multiple time Commented Nov 23, 2021 at 2:19
  • Can you please update your post to include a full job and variable definition instead of just snippets? We need to be able to reproduce your issue, which is difficult with partial code Commented Nov 23, 2021 at 3:49
  • could you please post the entire yml file Commented Nov 23, 2021 at 4:00
  • Patrick, Damith Udayanga, I have pasted complete file Commented Nov 23, 2021 at 4:03

6 Answers 6

48

When Gitlab CI CD variables are not getting injected into your pipelines as environment variables, please follow the following steps to verify.

  1. Check whether the variable is defined. You need to have at least the Maintainer role setup for your user. Go to Settings --> CI/CD --> Variables. You can see all project variables, and group variables (inherited).

  2. Next, check whether these variables are defined as Protected variables. If they are marked as Protected, then they are only exposed to protected branches or protected tags. I would suggest to uncheck this, if your current branch is not a protected branch. If not you can always make your current branch a protected one. enter image description here

  3. Next, check whether your code is accessing the environment variables correctly. Based on your scripting language, just access as if you are accessing a regular environment variable.

  4. You don't really need to define these variables in the .gitlab-ci.yaml file. (Even though their documentation says so)

Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

Branch was not protected; variables were marked as protected. This solved my problem.
I have solved my problem with the same approach, thanks!
2 is a live saver
This anwser definitely solved it for me too. Thank you very much !
21

Variables set in the GitLab UI are not passed down to service containers. To set them, assign them to variables in the UI, then re-assign them in your .gitlab-ci.yml:

stages:
  - Test
# Added this to your yml file
variables:
  ENV: $ENV
  BROWSER: $BROWSER
  Category: $Category

ui_tests:
  stage: Test
  image: 
    name: joyzourky/python-chromedriver:3.8
    entrypoint: [""]
  tags:
  - micro
  only:
  - develop
  when: manual
  script:
    - pip install --upgrade pip 
    - cd ./src/Tests/UIAutomation
    - pip install -r ./requirements.txt
    - pytest -s -v --env=${ENV} --browser=${BROWSER} --alluredir=./reports ./tests -m ${Category}
  artifacts:
    when: always
    path:
    - ./src/Tests/UIAutomation/reports/
    - ./src/Tests/UIAutomation/logs/
    expire_in: 1 day

Please refer attachment it's working with any issue. enter image description here

10 Comments

It is a typo in my .gitlab-ci.yml file. the variable must use always with $. Please read the comment on the .gitlab-ci.yml file.
Adding more points: - variables key is not necessary at least in my case, variables were available in my jobs. - if the variables are protected, they won't be available in your merge request event.
Unfortunately this answer is not correct. Project variables are indeed passed into containers. Self assignment is not needed. It is more that ssi-anik is correct (i.e. it is more likely a 'protected' issue).
If "Protect variable" is checked it can only be used by protected branches and tags. Please read this docs.gitlab.com/ee/ci/variables/…
This answer is in fact correct. As per the docs, variables set in the UI are not available to service containers (but variables defined in the .gitlab-ci.yml file itself are ¯_(ツ)_/¯). They may need to have a different name, too. FOO: $FOO may not work, but FOO: $UI_FOO will work.
|
8

As @Keet Sugathadasa mentioned, the branch that triggers the CI must be protected; this was my case so I have to protect it by going to Settings > Repository > Protected branch and then protect the branch from there

Comments

3

For users that run the pipeline based on tags, eg.

rules:
  - if: $CI_COMMIT_TAG

Make sure to use protect your tags under Settings > Repository > Protected Tags.

Comments

2

I had this problem, and I was using a protected variable in a protected branch, but the variable was still not being injected. I believe that my issue was this one: https://gitlab.com/gitlab-org/gitlab/-/issues/353447

That is, protected variables are not available in manual jobs.

In my case, I was able to unprotect the variable and it worked (indeed, in my case, the variable was needed on all branches, so it was better to unprotect it), but of course this is not always an option.

Comments

0

In my case, I had to remove the GitLab CI/CD variable defined for the development environment from the variables section in the YAML. That is, this didn't work:

temp_test_env_vars:
  stage: init
  variables:
    SUB_NAME: $SUB_NAME
  script:
    - echo "SUB_NAME -> $SUB_NAME" # The output was the literal "SUB_NAME -> $SUB_NAME"
  environment:
    name: development

But this worked:

temp_test_env_vars:
  stage: init
  script:
    - echo "SUB_NAME -> $SUB_NAME" # The output contained the value defined in the project's CI/CD settings
  environment:
    name: development

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.