如何在gitlab api中提供自定义变量?

问题描述 投票:0回答:1

根据 gitlab 文档,可以在 ci/cd 管道中触发作业,并在 POST 请求中传递一些参数。下面的示例。如何正确读取/解析 gitlab ci/cd 作业中的variables.json?比如说,我想通过下面的curl命令在我的管道中触发test-job1,如何读取.gitlab-ci中@variables.json中传递的参数?

curl --request POST "https://gitlab.example.com/api/v4/projects/1/jobs/1/play" \
     --header "Content-Type: application/json" \
     --header "PRIVATE-TOKEN: <your_access_token>" \
     --data @variables.json

.gitlab-ci.yml

build-job:
  stage: build
  script:
    - echo "Hello, $GITLAB_USER_LOGIN!"

test-job1:
  stage: test
  script:
    - python3 mydumbscript.py $arg1 $arg2 $arg3

test-job2:
  stage: test
  script:
    - echo "This job tests something, but takes more time than test-job1."
    - echo "After the echo commands complete, it runs the sleep command for 20 seconds"
    - echo "which simulates a test that runs 20 seconds longer than test-job1"
    - sleep 20

deploy-prod:
  stage: deploy
  script:
    - echo "This job deploys something from the $CI_COMMIT_BRANCH branch."
  environment: production
gitlab continuous-integration gitlab-ci
1个回答
0
投票

给定一个示例

variables.json
文件,如下所示:

{
  "job_variables_attributes": [
    {
      "key": "MY_VAR_1",
      "value": "foo"
    },
    {
      "key": "MY_VAR_2",
      "value": "bar"
    }
  ]
}

然后在要执行的作业上,您可以执行如下操作:

test-job1:
  stage: test
  script:
    - python3 mydumbscript.py $MY_VAR_1 $MY_VAR_2

--data @variables.json 本质上是导出 JSON 中定义的变量以在作业中可用,因此您可以像在

env
关键字中定义这些变量一样使用它们。

© www.soinside.com 2019 - 2024. All rights reserved.