如何通过GitLab API在GitLab CI/CD中运行特定任务?

-1 投票
1 回答
37 浏览
提问于 2025-04-12 12:28

我有一个curl命令(见下文),目前我的流水线里只有一个任务,当我执行下面的curl命令时,它会运行我的流水线并执行我唯一的任务,也就是test-job1。现在我添加了test-job2。将来我会有更多的任务,但我想配置这些任务,让我可以选择性地运行某些任务(其实就是在任务中创建规则),这样我就可以通过curl命令只运行那些特定的任务。

有没有人能告诉我所有的步骤?

curl --request POST "https://gitlab.example.com/api/v4/projects/1412/pipeline" \
     --header "Content-Type: application/json" \
     --header "PRIVATE-TOKEN: <your_access_token>" \
     --data @variables.json
build-job:
  stage: build
  script:
    - echo "Hello, $GITLAB_USER_LOGIN!"

test-job1:
  stage: test
  script:
    - python3 mydumbscript.py $arg1 $arg2 $arg3
how to run particular jobs in gitlab ci/cd via gitlab api? 
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

1 个回答

暂无回答

撰写回答