从Python API调用调用GITLAB作业

问题描述 投票:0回答:1

我有一个 Python 函数,它使用两个变量触发 GitLab 作业:标记和发布。使用管道触发令牌时,该函数按预期工作,但是当我切换到私有令牌时,遇到以下错误:

Response Status Code: 400
Response Body: {"error":"variables is invalid"}

这是我的Python代码:

import requests
import json

# Function to trigger GitLab pipeline
def trigger_gitlab_pipeline(project_id, tag, release, branch, private_token):
    url = f"https://gitlab.com/api/v4/projects/{project_id}/pipeline"
    
    headers = {
        'PRIVATE-TOKEN': private_token,
        'Content-Type': 'application/json'
    }
    
    # Data payload, ensuring release is a string
    data = {
        'ref': branch,
        'variables': {
            'TAG_NAME': tag,
            'RELEASE_NUM': release
        }
    }
    
    print("Request Data:", json.dumps(data, indent=4))
    
    # Send the request
    response = requests.post(url, headers=headers, json=data)
    
    # Output the result
    print("Response Status Code:", response.status_code)
    print("Response Body:", response.text)

# Example usage
project_id = <project_id>
tag = "AGT_v1.2.0-b25_and_AWB_v1.2.0-b08"
release = 88  # This will be converted to string
branch = "pipe_modi"
private_token = <token>

trigger_gitlab_pipeline(project_id, tag, release, branch, private_token)

我是否忽略了打电话的方式或任何错误?

python api gitlab gitlab-ci
1个回答
0
投票

尝试:

# Data payload, with variables as a list of dictionaries
data = {
    'ref': branch,
    'variables': [
        {'key': 'TAG_NAME', 'value': tag},
        {'key': 'RELEASE_NUM', 'value': str(release)}  # Ensure release is a string
    ]
}
© www.soinside.com 2019 - 2024. All rights reserved.