4

I have gitlab project with ci file:

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

Jobs are created on every source code change, and they can be run only manually, using gitlab interface. Now, i want to have possibility to trigger stage run with Gitlab API. Trying:

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

Returns:

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

Seems, like i have to define stage to trigger, but i can't find a way to pass it via api call.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
coolsv
  • 268
  • 3
  • 17

2 Answers2

2

you are using the wrong endpoint, to do it, you need to follow the path below

  1. list all of your pipelines and get the newest one GET /projects/:id/pipelines

  2. list the jobs from this pipeline GET /projects/:id/pipelines/:pipeline_id/jobs

  3. After that you can trigger your job POST /projects/:id/jobs/:job_id/play

Sergio Tanaka
  • 1,325
  • 1
  • 6
  • 18
  • Thanks, but what if my pipeline is not a newest one, for example there is a newer pipe, created from another branch, so i can't get pipeline with :id, i know only the ref name? – coolsv Jul 21 '20 at 13:36
  • When you are working with api's, most of the time you will be using IDs. Another approach is to get your MR from the [API](https://docs.gitlab.com/ee/api/merge_requests.html) to get the ID of the pipeline – Sergio Tanaka Jul 21 '20 at 13:41
1

you are telling your build to run at all times except for the time they are being triggered (api call is also considered as a trigger).

change your job definition to the following:

run:
  stage: run
  script:
    - (some stuff)
  tags:
    - my_runner_tag
  when: manual
yogev
  • 189
  • 5