17

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

please find attached an image snippet of my gitlab-ci.yml file- [![enter image description here][1]][1]

I am passing the below parameter while running pipeline - [![enter image description here][2]][2]

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
simond
  • 684
  • 1
  • 10
  • 36
  • 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. – Patrick Nov 22 '21 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 – simond Nov 23 '21 at 02: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 – Patrick Nov 23 '21 at 03:49
  • could you please post the entire yml file – Damith Udayanga Nov 23 '21 at 04:00
  • Patrick, Damith Udayanga, I have pasted complete file – simond Nov 23 '21 at 04:03

3 Answers3

16

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.

Keet Sugathadasa
  • 11,595
  • 6
  • 65
  • 80
13

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

Damith Udayanga
  • 726
  • 7
  • 18
  • 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. – Damith Udayanga Nov 23 '21 at 08:42
  • I used exact same code given by you and I am getting error -ERROR: Wrong expression passed to '-m': $Category: at column 1: unexpected character "$" – simond Nov 23 '21 at 10:05
  • let me check and confirm you – Damith Udayanga Nov 24 '21 at 00:10
  • Hi simond, it is working with any issue. – Damith Udayanga Nov 24 '21 at 04:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239520/discussion-between-simond-and-damith-udayanga). – simond Nov 24 '21 at 04:31
  • 3
    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. – ssi-anik Apr 20 '22 at 17:20
  • 6
    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). – Andreas H. May 05 '22 at 11:53
  • 4
    If "Protect variable" is checked it can only be used by protected branches and tags. Please read this https://docs.gitlab.com/ee/ci/variables/index.html#protected-cicd-variables – Ishtiyaq Husain Aug 09 '22 at 13:51
5

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

sareno
  • 576
  • 8
  • 10