9

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 5

6

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"
Sign up to request clarification or add additional context in comments.

Comments

2

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>"

Comments

0

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

4 Comments

Yeah, I also found documentation on how i can trigger a new pipeline with variables. But if my goal is to deploy the build artifact to different servers then this is no good for me. Each pipeline would create a new artifact in that case. Thats why I want to replay jobs with different arguments to deploy the same artifact to different servers.
@user1985273 OK, but would --form TEST=hello work better than --form variables[TEST]=hello?
"replay jobs with different arguments to deploy the same artifact to different servers" - is that sensible? Would it not make more sense to retrieve the artefact and then distribute it to the desired servers in whatever environment you're currently thinking about using to re-trigger the job?... or including a list of target servers in your CI/CD scripts that are run as part of the job in the first place.
I would prefer to keep deployment as part of gitlab pipeline. The environment im currently using to retrigger jobs is just a basic ui that has links to gitlab because gitlabs ui is not very user friendly. I need to option to manually deploy the artifact to any server. If they are all part of one job then I cant just say that "deploy artifat x to server y".
0

It looks like that as of Jan 25, 2021 this feature not yet supported. There is a feature request I found here: https://gitlab.com/gitlab-org/gitlab/-/issues/37267

Comments

0

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.

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.