【问题标题】:How to trigger only specific stage of pipeline with gitlab API?如何使用 gitlab API 仅触发管道的特定阶段?
【发布时间】:2020-07-21 12:55:09
【问题描述】:

我有带有 ci 文件的 gitlab 项目:

stages:
 - build
 - run

build:
  stage: build
  script:
    - (some stuff)
  tags:
    - my_runner_tag
  except:
    - triggers
  when: manual

run:
  stage: run
  script:
    - (some stuff)
  tags:
    - my_runner_tag
  except:
    - triggers
  when: manual

每次源代码更改都会创建作业,并且只能使用 gitlab 界面手动运行它们。 现在,我希望有可能使用 Gitlab API 触发阶段run。尝试:

curl -X POST \
>      -F token=xxxxxxxxxxxxxxx \
>      -F ref=master \
>      https://gitlab.xxxxx.com/api/v4/projects/5/trigger/pipeline

返回:

{"message":{"base":["No stages / jobs for this pipeline."]}}

似乎我必须定义要触发的阶段,但我找不到通过 api 调用传递它的方法。

【问题讨论】:

    标签: gitlab gitlab-ci pipeline jobs stage


    【解决方案1】:

    您使用了错误的端点,要做到这一点,您需要按照以下路径进行操作

    1. 列出您的所有管道并获取最新的 GET /projects/:id/pipelines

    2. 列出此管道中的作业 GET /projects/:id/pipelines/:pipeline_id/jobs

    3. 之后你就可以触发你的工作了 POST /projects/:id/jobs/:job_id/play

    【讨论】:

    • 谢谢,但是如果我的管道不是最新的,例如有一个较新的管道,是从另一个分支创建的,所以我无法使用 :id 获取管道,我只知道 ref姓名?
    • 当您使用 api 时,大部分时间您将使用 ID。另一种方法是从API获取你的MR来获取管道的ID
    【解决方案2】:

    您是在告诉您的构建在被触发时始终运行除了(API 调用也被视为触发器)。

    将您的工作定义更改为以下内容:

    run:
      stage: run
      script:
        - (some stuff)
      tags:
        - my_runner_tag
      when: manual
    

    【讨论】:

      猜你喜欢
      • 2022-12-11
      • 2020-02-23
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 2018-09-26
      • 2016-11-19
      • 2022-11-10
      • 2021-11-10
      相关资源
      最近更新 更多