GitLab CI variables in job api?

Viewed 5409

I am using rest API to run manual jobs in GitLab CI. When i start a manual job from UI I am able to define custom variables that i can use during the job. How can i define them when running job through API?

Could not find any documentation on it. Or not even a single question in forums.

This is how i currently run my job

curl -k --request POST --header "PRIVATE-TOKEN: abc" https://mygit.com/api/v4/projects/17/jobs/1956/play

I tried adding:

--form variables[TEST]=hello

But this didnt work.

Edit: A bit more information on what im doing. So my pipeline has two stages. Build and deploy. On each commit I want build to run once and then i want to be able to deploy this result to multiple different servers. Because the server list is dynamic and there are a lot of them I want to have the IP address of the server as an variable I can give to my deploy job.

5 Answers

Instead of starting a job you can start a pipeline and set the variables from there. Here's an example of how to do this from the GitLab documentation:

curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" \
--header "Content-Type: application/json" \
--data '{ "ref": "master", "variables": [ {"key": "VAR1", "value": "hello"}, {"key": "VAR2", "value": "world"} ] }' \
"https://gitlab.example.com/api/v4/projects/169/pipeline"

This is the way how I'm using it, didn't find a way to use API tokens for it though.

curl -X POST \
                -F token=xxxxxxxxxxxxxxxx \
                -F "ref=some_branch" \
                -F "variables[VAR1]=abc" \
                -F "variables[VAR2]=cde" \
                "https://example.gitlab.com/api/v4/projects/312/trigger/pipeline"

Where -F "variables[VAR1]=abc" for example is set in .gitlab-ci.yml.

only:
    variables:
      - $VAR1

The idea was to create some manual CI jobs and tell the devs they can run them via API call, but since I can only use the project token here, it's absolutely not secure.

It would be really handy to run it via curl --request PUT --header "PRIVATE-TOKEN: <your_access_token>"

Passing variables is documented in gitlab-org/gitlab issue 2772, but more about triggering pipeline (not job)

See if that syntax would work, for trigger variables (syntax variables[xxx]=yyy):

# gitlab-ci.yml
build:
  script:
    - curl --request POST --form "variables[PRE_CI_PIPELINE_SOURCE]=$CI_PIPELINE_SOURCE" --form "token=$CI_JOB_TOKEN" --form ref=master http://192.168.10.3:3001/api/v4/projects/13/trigger/pipeline

Or simply for regular variables --form key=value:

curl --request POST --form "token=$CI_JOB_TOKEN" --form ref=master https://gitlab.example.com/api/v4/projects/9/trigger/pipeline

Update 2022-03:

After you create a trigger token, and create trigger_pipeline step in pipeline, like this

trigger_pipeline:
  tags:
  image: alpine:latest
  stage: deploy
  script:
  only:
    variables:
      - $MANUAL

you can use it to trigger pipelines with a tool that can access the API

curl --request POST \
  --form token=TOKEN \
  --form ref=main \
  --form "variables[MANUAL]=true" \
  "https://gitlab.example.com/api/v4/projects/123456/trigger/pipeline"

or a webhook:

https://gitlab.example.com/api/v4/projects/123456/ref/<ref_name>/trigger/pipeline?token=<token>

for example for manual run.

Related