My Code
I am using my personal access token with full access still getting error, tell me other possible ways to achieve this so I can add tasks to multiple azure pipeline through automation.
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()
Following error I am getting:
Failed to update pipeline 42. Status code: 405
{"count":1,"value":{"Message":"The requested resource does not support http method 'PUT'."}}
It should update/add the tasks to multiple azure pipelines, tell me other possible way to automate this.
2
Answers
I tried making some changes to your code and ran the pipeline by updating its tasks, But Only the pipeline ran and the tasks did not get updated as steps, jobs and for classic pipeline phases, steps are not supported in the Python.
In your existing code replace put with post or use the modified code below which runs thee pipeline but fails to create the task as it is not supported as mentioned above.
My modified code:-
Pipeline ran but task failed:-
The recommended way is to upgrade your YAML file content in your Azure Repository and then use the code below to trigger the pipeline with Python like below:-
Python code:-
Output:-
Can you check whether the endpoint method is right, As 405 status code means the "method is Note Allowed" for that particular endpoint (PUT in this case)