如何通过 gitlab api 触发 gitlab ci/cd 中的作业并将参数传递给作业?

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

我可以通过 REST api 调用 gitlab api 将参数传递给作业,以触发特定作业吗?我有一个作为作业一部分运行的 python 脚本,我想将参数传递给该脚本。 我想通过 gitlab api 调用该作业。请求会是什么样子?

例如,根据文档,触发管道的curl命令如下所示

curl --request POST \ 
  "https://gitlab.example.com/api/v4/projects/project_id/trigger/pipeline?token=**************&ref=main"

问题:所以在下面的示例中,我有一个 test-job1,它运行一个名为 mydumbscript.py 的 python 脚本,并且我传递了几个参数 $arg1 ... 。我可以通过 gitlab api 传递这个参数并触发这个特定的作业吗?

我已经在 gitlab 中建立了一个项目,比如说 myproject,这是一个示例 .gitlab-ci.yml 文件

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

test-job1:
  stage: test
  script:
    - python3 myproject/script/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
python gitlab gitlab-ci gitlab-api
1个回答
0
投票

来自 gitlab 文档:

您可以在触发器 API 调用中传递任意数量的 CI/CD 变量。这些变量具有最高优先级,并覆盖所有同名变量。 参数的形式为变量[key]=value,例如:

curl --request POST \
     --form token=TOKEN \
     --form ref=main \
     --form variables[UPLOAD_TO_S3]="true" \
     "https://gitlab.example.com/api/v4/projects/123456/trigger/pipeline"

触发管道中的 CI/CD 变量显示在每个作业页面上,但只有具有所有者和维护者角色的用户才能查看这些值。

示例中的关键是管道开始运行时您将在管道中定义的变量。您可以添加多行,例如 --vorm 变量行。

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