How to add task in Azure DevOps release pipeline using rest API - azure-devops

I want to add task in Azure DevOps release pipeline using rest API -
Here is rest API for updating release pipeline -
PUT https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/definitions?api-version=6.0
for example if I want to add powershell task in my existing release pipeline in Dev stage, What request body I need to put ?

After testing(clicking F12 to capture corresponding browser network requests), this request body for this API: Definitions - Update is the same content when you call this API: Definitions - Get to get its definition.
Therefore, you could get the release pipeline definition firstly, and then add below PowerShell task code segment to workflowTasks array, which locates in the result's environments array >>dev stage>>deployPhases array>>workflowTasks array
{
"environment": {},
"taskId": "e213ff0f-5d5c-4791-802d-52ea3e7be1f1",
"version": "2.*",
"name": "PowerShell Script test",
"refName": "",
"enabled": true,
"alwaysRun": false,
"continueOnError": false,
"timeoutInMinutes": 0,
"definitionType": "task",
"overrideInputs": {},
"condition": "succeeded()",
"inputs": {
"targetType": "inline",
"filePath": "",
"arguments": "",
"script": "# Write your PowerShell commands here.\n\nWrite-Host \"Hello World\"\n",
"errorActionPreference": "stop",
"failOnStderr": "false",
"showWarnings": "false",
"ignoreLASTEXITCODE": "false",
"pwsh": "false",
"workingDirectory": ""
}
}
And then there will an extra PowerShell task in Dev stage.
BTW, each updates will generate a new revision, which is the latest version of release definition, and you need to use the latest version to update release pipeline the next time.

Related

Azure DevOps API get status of previous release task

Does anyone know the api to get the status of the individual release task in a release?
I can get the status of the release itself but not the individual release tasks (PowerShell tasks in my case). https://learn.microsoft.com/en-us/rest/api/azure/devops/release/releases/get%20release?view=azure-devops-rest-6.0
Also if anyone knows where the source code is for ADO built in tasks condition. can you please share the link?
Does anyone know the api to get the status of the individual release task in a release?
Method 1: You can use a REST API that is not documented:
GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/releases/{releaseId}/environments/{environmentId}/attempts/{attemptId}/timelines/{timelineId}/tasks
It returns a brief overview and status(succeeded, inProgress, pending, failed...) of all tasks in the release.
The response body looks like the following example:
{
"count": 9,
"value": [
{
"id": 4,
"timelineRecordId": "d81751cc-e42f-407a-9091-f611d37df33b",
"name": "Initialize job",
"dateStarted": "2021-04-27T06:01:21.5233333Z",
"dateEnded": "2021-04-27T06:01:23.32Z",
"startTime": "2021-04-27T06:01:21.5233333Z",
"finishTime": "2021-04-27T06:01:23.32Z",
"status": "succeeded",
"rank": 1,
"lineCount": 121,
"issues": [],
"agentName": "Hosted Agent",
"logUrl": "https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/releases/6/environments/6/deployPhases/6/tasks/4/logs"
},
{
"id": 5,
"timelineRecordId": "68716c38-638d-551b-6964-2da9e273edef",
"name": "Command Line Script",
"dateStarted": "2021-04-27T06:01:23.33Z",
"dateEnded": "2021-04-27T06:01:28.5833333Z",
"startTime": "2021-04-27T06:01:23.33Z",
"finishTime": "2021-04-27T06:01:28.5833333Z",
"status": "succeeded",
"rank": 2,
"lineCount": 15,
"issues": [],
"task": {
"id": "d9bafed4-0b18-4f58-968d-86655b4d2ce9",
"name": "CmdLine",
"version": "2.182.0"
},
"agentName": "Hosted Agent",
"logUrl": "https://vsrm.dev.azure.com/{organization}/{project}/_apis/Release/releases/6/environments/6/deployPhases/6/tasks/5/logs"
},
{
"id": 6,
"timelineRecordId": "fced85f1-31cf-59f2-d2e0-ccd1645b2427",
"name": "Command Line Script",
"dateStarted": "2021-04-27T06:01:28.5866667Z",
"startTime": "2021-04-27T06:01:28.5866667Z",
"status": "inProgress",
"percentComplete": 0,
"rank": 3,
"issues": [],
"task": {
"id": "d9bafed4-0b18-4f58-968d-86655b4d2ce9",
"name": "CmdLine",
"version": "2.182.0"
},
"agentName": "Hosted Agent",
"logUrl": ""
}
]
}
Method 2: You can use the REST API Releases - Get Task Log:
GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/{releaseId}/environments/{environmentId}/deployPhases/{releaseDeployPhaseId}/tasks/{taskId}/logs?api-version=6.0-preview.2
It returns the running log of a specific task.
The response body looks like the following example:
2021-04-27T05:50:50.3443852Z ##[section]Starting: Command Line Script
2021-04-27T05:50:50.4313144Z ==============================================================================
2021-04-27T05:50:50.4313918Z Task : Command line
2021-04-27T05:50:50.4314555Z Description : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
2021-04-27T05:50:50.4314878Z Version : 2.182.0
2021-04-27T05:50:50.4315342Z Author : Microsoft Corporation
2021-04-27T05:50:50.4315927Z Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/command-line
2021-04-27T05:50:50.4316336Z ==============================================================================
2021-04-27T05:50:53.2219858Z Generating script.
2021-04-27T05:50:53.2572235Z ========================== Starting Command Output ===========================
2021-04-27T05:50:53.2972287Z ##[command]"C:\Windows\system32\cmd.exe" /D /E:ON /V:OFF /S /C "CALL "D:\a\_temp\fda7641b-b8a2-4d04-8f83-94f5eda9c3b6.cmd""
2021-04-27T05:50:53.3085688Z Write your commands here
2021-04-27T05:50:53.3086888Z Hello world
2021-04-27T05:50:53.3788822Z ##[section]Finishing: Command Line Script
If anyone knows where the source code is for ADO built in tasks condition.
I don't think Azure DevOps built-in tasks have conditions source code, because conditions can be used not only for built-in tasks, but for other tasks, jobs, stages, etc... It is supported by YAML Schema and Azure DevOps classic UI pipeline, but it has nothing to do with specific tasks.
Click this github link for Azure DevOps built-in task source code.
Each of your PowerShell tasks is an environment; therefore, you can access through the environments that is returned by "Get Release" API. See https://learn.microsoft.com/en-us/rest/api/azure/devops/release/releases/get%20release?view=azure-devops-rest-6.0#releaseenvironment

Azure Devops rest API for create pipeline with source provider bitbucket not working

I am new on Azure and trying to create pipeline through rest api with below URL
https://dev.azure.com/my-org/my_project/_apis/pipelines?api-version=6.1-preview.1
When Source Provider is Azure Git Repo then it working perfectly fine with below request body
{
"folder": "\\",
"name": "Test-Pipeline-1",
"configuration": {
"type": "yaml",
"path":"/azure-pipelines.yml",
"repository":{
"id": "1f13f61c-eade-b36bc515bb5e",
"name": "TestAzure123",
"type":"azureReposGit"
}
}
}
But when Source Provider is Bitbucket Cloud then its not working
{
"folder": "\\",
"name": "Bitbucket-Pipeline",
"configuration": {
"type": "yaml",
"path":"/master-pipeline.yaml",
"repository": {
"id": "sid_07/Bitbucket-repository",
"name": "Bitbucket-repository",
"type": "Bitbucket"
}
}
}
I am getting below exception
{
"$id": "1",
"innerException": null,
"message": "This API does not support creating pipelines with repositories of type Unknown.",
"typeName": "Microsoft.Azure.Pipelines.WebApi.UnsupportedRepositoryTypeException, Microsoft.Azure.Pipelines.WebApi",
"typeKey": "UnsupportedRepositoryTypeException",
"errorCode": 0,
"eventId": 3000
}
Is pipeline creation supported through rest api for bitbucket? or am I missing something? Please help
I have already taken reference from similar issue
https://developercommunity.visualstudio.com/content/problem/1101376/create-pipeline-rest-api-does-not-work.html
Azure Devops rest API for create pipeline with source provider bitbucket not working
I am afraid the REST API Pipelines - Create does not support the creation of a pipeline whose source type is bitbucket at this moment.
When I use the request body below, which I used to create a pipeline for the resource type of github:
{
"folder": "\\",
"name": "Test-Pipeline-2",
"configuration": {
"path": "TestDemo.yml",
"repository": {
"fullName": "xxx/leotest",
"connection": {
"id": "e11d299a-0bfd-4a38-a77c-xxxxxxxx"
},
"type": "bitbucket"
},
"type": "yaml"
}
I got the same error as you:
Then I created that pipeline manually, and use the REST API Pipelines - Get to get the detailed info about this pipeline, I got following info:
"configuration": {
"path": "BitbucketRepo.yml",
"repository": null,
"type": "yaml"
},
"url": "https://dev.azure.com/xxx/xxxxx/_apis/pipelines/146?revision=2",
"id": 146,
"revision": 2,
"name": "BitbucketRepo",
"folder": "\\YAML\\Resources"
We could to know the part of repository: is null.
So, the REST API Pipelines - Create does not support create pipeline with source provider bitbucket at this moment.
For this request, you could add it for this feature on our UserVoice site (https://developercommunity.visualstudio.com/content/idea/post.html?space=21 ), which is our main forum for product suggestions. Thank you for helping us build a better Azure DevOps.

Webhook Payload explanation

Looking at both the documentation and example service hooks there is little to no explanation about what the contents of the payload is.
For example, I create a Release Started and a Release Completed Service hook, and when I look at the payload none of the included guids actually represent the id of the release pipeline that triggered the alert.
Here is an obfuscated payload I receive:
{
"subscriptionId": "000-000-000-000-000",
"notificationId": 12,
"id": "000-000-000-000-000",
"eventType": "ms.vss-release.deployment-started-event",
"publisherId": "rm",
"message": {
"text": "Deployment of release Release-430 to stage Test started.",
"html": "Deployment on stage <a href='https://TESTAcentral.visualstudio.com/TESTA/_release?_a=environment-summary&definitionId=34&definitionEnvironmentId=12'>Test</a> started.",
"markdown": "Deployment on stage [Test](https://TESTAcentral.visualstudio.com/TESTA/_release?_a=environment-summary&definitionId=34&definitionEnvironmentId=12) started."
},
"detailedMessage": {
"text": "Deployment of release Release-430 on stage Test started.\r\nTrigger: After successful deployment of Dev",
"html": "Deployment on stage <a href='https://TESTAcentral.visualstudio.com/TESTA/_release?_a=environment-summary&definitionId=34&definitionEnvironmentId=12'>Test</a> started.<br>Trigger: After successful deployment of Dev",
"markdown": "Deployment on stage [Test](https://TESTAcentral.visualstudio.com/TESTA/_release?_a=environment-summary&definitionId=34&definitionEnvironmentId=12) started.\r\nTrigger: After successful deployment of Dev"
},
"resourceVersion": null,
"resourceContainers": {
"collection": {
"id": "000-000-000-000-000",
"baseUrl": "https://TESTAcentral.vsrm.visualstudio.com/"
},
"account": {
"id": "000-000-000-000-000",
"baseUrl": "https://TESTAcentral.vsrm.visualstudio.com/"
},
"project": {
"id": "000-000-000-000-000",
"baseUrl": "https://TESTAcentral.vsrm.visualstudio.com/"
}
},
"createdDate": "2020-01-29T13:49:01.1293269Z",
"resource": null
}
How should I identify the pipeline that actually triggered this webhook?
Please dont tell me that the intention is to parse the markdown or html fields and break apart that url. The utility of this entire process is compromised if you dont include the triggering identifier!
Update
This is a issue which may caused by new UI in Service Hook.
The temporarily workaround is turn off "Service Hooks setting page improvements preview" in Preview Feature

How to execute a PowerShell Command from within Azure Data Factory custom activity?

I have a custom activity in Azure Data Factory, which attempts to execute the following command:
PowerShell.exe -Command "Write-Host 'Hello, world!'"
However, when I debug (run) this command from within Azure Data Factory, it runs for a long time, and finally fails.
I guess it fails because perhaps it could not locate "PowerShell.exe". How can I ensure that the ADF Custom Activity has access to PowerShell.exe?
Some sites say about specifying a package (.zip file) that contains everything needed for the exe to execute. However, since PowerShell is from Microsoft, I think it would be inappropriate to ZIP the PowerShell directory, and specify it as a package to the Custom Activity.
Please suggest as to how I can execute PowerShell command from Custom Activity of an Azure Data Factory. Thanks!
Whenever I search "Execute PowerShell from Custom Activity in Azure Data Factory", the search results are talking more about which Az PowerShell command to use to trigger start an ADF pipeline.
I saw two threads in Stackoverflow.com, where the answer just specifies to use a Custom Activity, and the answer is not specific to PowerShell command call from ADF
Here is the JSON for the task:
{
"name": "ExecutePs1CustomActivity",
"properties": {
"activities": [
{
"name": "ExecutePSScriptCustomActivity",
"type": "Custom",
"dependsOn": [],
"policy": {
"timeout": "7.00:00:00",
"retry": 0,
"retryIntervalInSeconds": 30,
"secureOutput": false,
"secureInput": false
},
"userProperties": [],
"typeProperties": {
"command": "PowerShell.exe -Command \"Write-Host 'Hello, world!'\"",
"referenceObjects": {
"linkedServices": [],
"datasets": []
}
},
"linkedServiceName": {
"referenceName": "Ps1CustomActivityAzureBatch",
"type": "LinkedServiceReference"
}
}
],
"annotations": []
}
}
I see "In Progress" for 3 minutes (180 seconds), and then it shows as "Failed."
I would suggest you to move all you scripting task in a powershell file and copy it to a storage account linked with your custom activity. . Once done try to call it like below:
powershell .\script.ps1
You can also provide the path of the script in json like below:
{
"name": "MyCustomActivityPipeline",
"properties": {
"description": "Custom activity sample",
"activities": [{
"type": "Custom",
"name": "MyCustomActivity",
"linkedServiceName": {
"referenceName": "AzureBatchLinkedService",
"type": "LinkedServiceReference"
},
"typeProperties": {
"command": "helloworld.exe",
"folderPath": "customactv2/helloworld",
"resourceLinkedService": {
"referenceName": "StorageLinkedService",
"type": "LinkedServiceReference"
}
}
}]
}
}
Please try it and see if it helps. Also i would suggest you to troubleshoot the pipeline steps to look for detailed error.
Also to your second point "Some sites say about specifying a package (.zip file) that contains everything needed for the exe to execute." This is required when you are building a custom activity using dot net then it is must copy all the Dll's and Exe's for execution.
Hope it helps.

How to run a PowerShell script during Azure VM deployment with ARM template?

I want to deploy a VM in azure using Azure Resource Manager (ARM), and then run a PowerShell script inside the VM post deployment to configure it.
I can do this fine with something like this: https://github.com/Azure/azure-quickstart-templates/tree/master/201-vm-vsts-agent
However that template grabs the PowerShell script from GitHub. As part of my deployment I want to upload the script to Azure Storage, and then have the VM get the script from Azure storage and run it. How can I do that part with regards to dependencies on the PowerShell script, because it has to exist in Azure Storage somewhere before being executed.
I currently have this to install a VSTS Agent as part of a deployment, but the script is downloaded from GitHub, I don't want to do that, I want the installation script of the VSTS Agent to be part of my ARM Project.
{
"name": "vsts-build-agents",
"type": "extensions",
"location": "[parameters('location')]",
"apiVersion": "2017-12-01",
"dependsOn": [
"vsts-build-vm"
],
"tags": {
"displayName": "VstsInstallScript"
},
"properties": {
"publisher": "Microsoft.Compute",
"type": "CustomScriptExtension",
"typeHandlerVersion": "1.9",
"settings": {
"fileUris": [
"[concat(parameters('_artifactsLocation'), '/', variables('powerShell').folder, '/', variables('powerShell').script, parameters('_artifactsLocationSasToken'))]"
]
},
"protectedSettings": {
"commandToExecute": "[concat('powershell.exe -ExecutionPolicy Unrestricted -Command \"& {', './', variables('powerShell').script, ' ', variables('powerShell').buildParameters, '}\"')]"
}
}
}
I guess my question is really about how to set _azurestoragelocation to an azure storage location where the script has just been uploaded as part of the deployment.
chicken\egg problem. you cannot upload to azure storage with arm template, you need to use script to upload to azure storage, but if you have that script on vm to upload it you dont really need to upload it.
that being said, why dont you use VSTS agent extension?
{
"name": "xxx",
"apiVersion": "2015-01-01",
"type": "Microsoft.Resources/deployments",
"properties": {
"mode": "Incremental",
"templateLink": {
"uri": "https://gallery.azure.com/artifact/20161101/microsoft.vsts-agent-windows-arm.1.0.0/Artifacts/MainTemplate.json"
},
"parameters": {
"vmName": {
"value": "xxx"
},
"location": {
"value": "xxx"
},
"VSTSAccountName": {
"value": "xxx"
},
"TeamProject": {
"value": "xxx"
},
"DeploymentGroup": {
"value": "Default"
},
"AgentName": {
"value": "xxx"
},
"PATToken": {
"value": "xxx"
}
}
}
},
Do you mean how to set _artifactsLocation as in the quickstart sample? If so you have 2 options (or 3 depending)
1) use the script in the QS repo, the defaultValue for the _artifactsLocation param will set that for you...
2) if you want to customize, from your local copy of the sample, just use the Deploy-AzureResourceGroup.ps1 in the repo and it will stage and set the value for you accordingly (when you use the -UploadArtifacts switch)
3) stage the PS1 somewhere yourself and manually set the values of _artifactsLocation and _artifactsLocationSasToken
You can also deploy from gallery.azure.com, but that will force you to use the script that is stored in the galley (same as using the defaults in GitHub)
That help?