简体   繁体   中英

How to pass env variables from python script to gitlab ci with requests?

Need to pass env variable from python script to gitlab ci pipeline. Tried

responce = requests.post("https://gitlab.com/api/v4/projects/{project_id}/trigger/pipeline", data={'token': 'token', 'ref': 'branch', 'variables': [{'key': 'MR_ID', 'value': 'VALUE'}])

responce = requests.post("https://gitlab.com/api/v4/projects/{project_id}/trigger/pipeline", data={'token': 'token', 'ref': 'branch', {variables': [{'key': 'MR_ID', 'value': 'VALUE'}]})

Getting {"error":"variables is invalid"} all the time.

Here is what documentation says: https://docs.gitlab.com/ee/api/pipelines.html#create-a-new-pipeline

Could anybody provide real working python example?

Looks like you misunderstand what the data parameter is in a requests.post . If you have a look at the docs you can see, that the information from data will be delivered in the body.

What you want to fulfill the gitlab API is the params parameter of requests.

params will encode the data in the URL as query parameter like this:

requests.post("https://gitlab.example.com/api/v4/projects/YOUR_PROJECT_ID/pipeline", params={"MR_ID": "VALUE"});

So you have to put your variables in the params parameter and the other information you have in data at the right place (like token in the header etc.)

You can send the variable in the following way:

import requests
url = "https://gitlab.com/api/v4/projects/{project_id}/trigger/pipeline"
data = {'token': 'token', 'ref': 'branch', 'variables[MR_ID]': 'VALUE'}
response = requests.posts(url, data=data}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM