Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab CI CD variable are not getting injected while running gitlab pipeline

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

like image 787
simond Avatar asked Sep 07 '25 15:09

simond


2 Answers

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.

like image 170
Keet Sugathadasa Avatar answered Sep 09 '25 11:09

Keet Sugathadasa


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

like image 23
Damith Udayanga Avatar answered Sep 09 '25 11:09

Damith Udayanga