简体   繁体   English

Gitlab 运行 gitlab 管道时未注入 CI CD 变量

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

I am running the below code section in gitlab-ci.yml file:我在 gitlab-ci.yml 文件中运行以下代码部分:

  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所以我必须将任何值作为变量 ENV 传递给管道:dev

I have added all the above three variables in the GitLab CI CD variables sections by expanding them.我已经通过扩展在 GitLab CI CD 变量部分添加了所有上述三个变量。 just added a single value along with key只是添加了一个值和键

I also found like we can add variables in the.yml file itself as below.我还发现我们可以在 .yml 文件本身中添加变量,如下所示。 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]请找到我的 gitlab-ci.yml 文件的图像片段 - [![在此处输入图像描述][1]][1]

I am passing the below parameter while running pipeline - [![enter image description here][2]][2]我在运行管道时传递了以下参数 - [![在此处输入图像描述][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所以理想情况下 ${Category} 应该替换为 value smoke 等

When Gitlab CI CD variables are not getting injected into your pipelines as environment variables, please follow the following steps to verify.当 Gitlab CI CD 变量没有作为环境变量注入到您的管道中时,请按照以下步骤进行验证。

  1. Check whether the variable is defined.检查变量是否已定义。 You need to have at least the Maintainer role setup for your user.您至少需要为您的用户设置Maintainer角色。 Go to Settings --> CI/CD --> Variables. Go 到设置 --> CI/CD --> 变量。 You can see all project variables, and group variables (inherited).您可以查看所有项目变量和组变量(继承)。

  2. Next, check whether these variables are defined as Protected variables.接下来,检查这些变量是否被定义为Protected的变量。 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 .如果您当前的分支不是protected branch ,我建议取消选中此项。 If not you can always make your current branch a protected one.如果不是,您始终可以使当前分支成为受保护的分支。 在此处输入图像描述

  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.您实际上不需要在.gitlab-ci.yaml文件中定义这些变量。 (Even though their documentation says so) (即使他们的文档是这样说的)

Hope this helps.希望这可以帮助。

Variables set in the GitLab UI are not passed down to service containers. GitLab UI 中设置的变量不会传递给服务容器。 To set them, assign them to variables in the UI, then re-assign them in your.gitlab-ci.yml:要设置它们,请将它们分配给 UI 中的变量,然后在 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

As @Keet Sugathadasa mentioned, the branch that triggers the CI must be protected;正如@Keet Sugathadasa 提到的,触发 CI 的分支必须受到保护; this was my case so I have to protect it by going to Settings > Repository > Protected branch and then protect the branch from there这是我的情况,所以我必须通过转到Settings > Repository > Protected branch来保护它,然后从那里保护分支

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM