我的密码
我正在使用我的个人访问令牌,完全访问仍然出错,请告诉我其他可能的方法来实现这一点,以便我可以通过自动化将任务添加到多个Azure管道。
import requests
import base64
# Azure DevOps organization URL
org_url = "https://dev.azure.com/my-org" # Replace with your organization URL
# Personal access token (PAT) with necessary permissions (Read and Write) for pipelines
pat = "my-pat"
# Encode the PAT to Base64
credentials = base64.b64encode(f":{pat}".encode()).decode()
# Project name and pipeline ID where you want to add the tasks
project_name = "My Project"
pipeline_id = 12
# Tasks to be added
tasks = [
{
"taskReference": {
"id": "mspremier.PostBuildCleanup.PostBuildCleanup-task.PostBuildCleanup@3",
"name": "Clean Agent Directories"
},
"enabled": True
},
{
"taskReference": {
"id": "NodeTool@0",
"name": "Use Node 6.x"
},
"enabled": True
}
]
def update_pipeline_definition():
url = f"{org_url}/{project_name}/_apis/pipelines/{pipeline_id}?api-version=6.0-preview.1"
headers = {"Authorization": f"Basic {credentials}", "Content-Type": "application/json"}
# Get the current pipeline definition
response = requests.get(url, headers=headers)
if response.status_code == 200:
pipeline_data = response.json()
# Modify the pipeline definition to include the tasks
if "phases" in pipeline_data["configuration"]:
for phase in pipeline_data["configuration"]["phases"]:
if "steps" in phase:
for task in tasks:
phase["steps"].append(task)
# Update the pipeline definition
response = requests.put(url, headers=headers, json=pipeline_data)
if response.status_code == 200:
print(f"Tasks added to pipeline {pipeline_id} successfully.")
else:
print(f"Failed to update pipeline {pipeline_id}. Status code: {response.status_code}")
print(response.text)
else:
print(f"Failed to get pipeline {pipeline_id}. Status code: {response.status_code}")
print(response.text)
if __name__ == "__main__":
update_pipeline_definition()
字符串
下面的错误我得到:
Failed to update pipeline 42. Status code: 405
{"count":1,"value":{"Message":"The requested resource does not support http method 'PUT'."}}
型
它应该更新/添加任务到多个Azure管道,告诉我其他可能的方法来自动化。
2条答案
按热度按时间dgsult0t1#
我试着对你的代码做了一些修改,并通过更新它的任务来运行管道,但是只有管道运行,任务没有被更新为步骤,作业和经典的管道阶段,Python中不支持步骤。
在现有代码中,将put替换为post,或者使用以下修改后的代码,该代码运行管道,但无法创建任务,因为它不受支持,如上所述。
我修改的编码:-
字符串
管道运行但任务失败:-
x1c 0d1x的数据
推荐的方法是升级Azure存储库中的YAML文件内容,然后使用下面的代码触发Python管道,如下所示:-
Python代码:-
型
输出:-
的
的
eoxn13cs2#
你能检查端点方法是否正确吗,因为405状态码意味着“方法是允许注解的”(在这种情况下是PUT)