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.

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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 to get the ID of the pipeline
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.