How to pass values to a Gitlab CI Job
Asked Answered
L

3

10

I have a GitLab ci job to download and build few files and then publish to another server. I have done a POC and was successful. The problem is that I would like to customize the job when triggered manually or via API. How can i pass the dynamic values(user given) to reflect in CI job (like a filename, format etc)?

I am using Windows runner.

I saw trigger variables and environment variables but can be retrieved while execution once the value is set in UI. I want a bit different where I will get the values from user.

Is it not possible in GitLab?

Edit:

A bit more info on how I'm using the pipeline. My pipeline has two stages - build and deploy. I want the build to run once, but I want to be able to call deploy multiple to deploy to different server. I can use the variable to determine to which server I want to deploy. Through GitLab UI this is possible - i can set variables for pipeline and also for job that overrides the pipeline variable. I just want to do the same thing through API.

Leila answered 28/3, 2019 at 6:12 Comment(0)
H
5

You can pass variables when starting a pipeline: https://docs.gitlab.com/ee/ci/pipelines/#run-a-pipeline-manually

Pipelines can be manually executed, with predefined or manually-specified variables.

These variables can be also specified on the API call: https://docs.gitlab.com/ee/api/pipelines.html#create-a-new-pipeline

An array containing the variables available in the pipeline, matching the structure [{ ‘key’ => ‘UPLOAD_TO_S3’, ‘value’ => ‘true’ }]

Highstrung answered 28/3, 2019 at 11:9 Comment(0)
R
4

The problem is that I would like to customize the job when triggered manually or via API

This should be possible with GitLab 14.9 (March 2022)

Specify variables when running manual jobs via the API

When running a manual job, it can be useful to input CI/CD variables to overwrite the existing variables, or to add new ones.

Previously, the only way to do this, was to do it through the UI.

In this release we’ve added the ability to specify variables when running a job by using the REST API, which will give you more options for automating your CI/CD pipelines.

See Documentation and Issue.

Example:

curl --request POST "https://gitlab.example.com/api/v4/projects/1/jobs/1/play
     --header "PRIVATE-TOKEN: <your_access_token>"
     --data @variables.json

@variables.json is structured like:

{
  "job_variables_attributes": [
    {
      "key": "TEST_VAR_1",
      "value": "test1"
    },
    {
      "key": "TEST_VAR_2",
      "value": "test2"
    }
  ]
}
Ronnaronnholm answered 22/3, 2022 at 18:15 Comment(3)
@sgm What GitLab version are you using? variables.json is a file to be created and passed as parameter to your curl command. It is not a file "to be received", but "to be sent". See also "What does the @ (at) symbol before a filename mean in a curl command?"Ronnaronnholm
im using 14.09.1-ee version. I send variables.json with @ but not appears the TEST_VAR_! in the job. I used the example codeSimper
@Simper Strange. Can you illustrate that in a new separate question?Ronnaronnholm
I
0

JavaScript example of passing variables to a job via GitLab API

If you're not using curl, but want to trigger a GitLab job with variables via API programmatically from your language of choice, the important part to make job_variables_attributes work is setting correct Content-Type:

Example in JavaScript / nodejs:


const https = request('https')

const postData = JSON.stringify({
    job_variables_attributes: [
       {
            key: 'CUSTOM_VAR1',
            value: 'test1',
        },
        {
            key: 'CUSTOM_VAR2',
            value: 'test2',
        },
    ],
});


const req = https.request({    
    method: 'POST', 
    hostname: 'gitlab.example.com',
    path: `/api/v4/projects/${projectId}/jobs/${jobId}/play?private_token=${token}`,
    headers: { 'Content-Type': 'application/json' } 
}, (res) => ...)

req.write(postData);
req.end();

Then in the job you should have $CUSTOM_VAR1 and $CUSTOM_VAR2.

Intramundane answered 26/1, 2023 at 11:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.