gitlab-cicicdgitlab-api

is it possible to invoke a gitlab ci/cd step via rest call?


I'm new to gitlab ci/cd. Looking at the documentation, it suggests that we can define/set up ci/cd steps in our gilab pipeline . also , set up webhook integrations. is it possible to set up a step and invoke that by post request by a web call? in example below, I am aware that , i can automate the steps mention or invoke it manually from the browser. however, is it possible to invoke each step, like test-job1 and test-job-2 via a web call ?

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

test-job1:
  stage: test
  script:
    - echo "This job tests something"

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

I have set up a gitlab repo and added ci/cd steps in my gitlab ci file .


Solution

  • You can run a manual job using Gitlab’s API.

    curl --request POST "https://gitlab.example.com/api/v4/projects/1/jobs/1/play" \
     --header "Content-Type: application/json" \
     --header "PRIVATE-TOKEN: <your_access_token>"
    

    Your job would need “when: manual” added, or a rule that makes it be a manual job.

    test-job1:
      stage: test
      when: manual
      script:
        - echo "This job tests something"
    

    In order to get the job ID you might need to perform additional API calls, like list pipeline jobs. Be aware that you might need to paginate as it returns the last 20 pipelines. Here you get all manual jobs:

    curl --globoff --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/jobs?scope[]=manual"