How to automaticly change Work Item State - azure

I want to create rule like:
When custom field value is set to X then change state od Task to New. Can it be done?
It's hard to belive this isn't possible but I can't find a way to do it.

Default custom rule does not support changing state. There are two workarounds you can refer to.
Workaround1:
You can customize a field to decide whether to change the state, trigger a webhook when the field changes, and then trigger a pipeline through this webhook. In the pipeline, the state of the current work item is changed by running the REST API Work Items – Update.
There are the detailed steps.
Customize a field named "ChangeState" in my sample. Its definition is as follows:
Follow this doc Define a webhooks resource to create a webhook and a "Incoming Webhook" service connection.
The setting of the webhook:
Set Trigger on this type of event to Work item updated, set Field to the customed field created at the first step.
Set the webhook created at the second step as the pipeline resource and run the REST API Work Items – Updateto update the state of the work item. There is the YAML for your reference:
resources:
webhooks:
- webhook: ChangeWIState ### Webhook alias
connection: ChangeNameSC ### Incoming webhook service connection
pool:
vmImage: ubuntu-latest
steps:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
Write-Host ${{ parameters.ChangeWIState.resource.workItemId}}
$token = "<your PAT>"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$url="https://dev.azure.com/<org name>/_apis/wit/workitems/${{ parameters.ChangeWIState.resource.workItemId}}?api-version=7.0"
$body = #'
[
{
"op": "add",
"path": "/fields/System.State",
"value": "New"
}
]
'#
$head = #{ Authorization =" Basic $token" }
$response = Invoke-RestMethod -Uri $url -Method PATCH -Headers $head -Body $body -ContentType application/json-patch+json
"ChangeWIState" is the name of my webhook and "ChangeNameSC" is the name of my servie connection.
Every time the field "ChangeState" changes, the pipeline will be triggered to run the REST API to change the state to new.
Workaround2:
You can use this extension "Work item form one click actions.
Set Actions as following:
Set Triggers as following:
When the value of the field "Test" changes to "X", the state will change to "New".

Related

Update Release Description in Release Pipeline [duplicate]

In Azure DevOps I'm trying to set the release description via PowerShell / CMD in order to have a dynamic description of my releases based on the input of the artifacts from the build step.
I've tried setting the release variables via powershell like:
Write-Host "##vso[task.setvariable variable=release.releasedescription;]bar"
Write-Host "##vso[task.setvariable variable=RELEASE_RELEASEDESCRIPTION;]bar"
But that didn't work and the description field remains always empty.
Is there any tweak / setting that would help achieve this behavior?
What you tried to do is just to set the environment variable that contains the release description data and not set the "real" release description, so after the release finished the description not changed.
If you want to set the release description during the release you can try to do it with Azure DevOps Rest API - Update Release.
So add a PowerShell task that executes the Rest API with Invoke-RestMethod, get the current release with GET method and then update the release with PUT, in the body change the description to the new one.
You could do this too:
- bash: |
echo "Updating pipeline job Run description"
echo "##vso[build.updatebuildnumber]$(Build.BuildNumber) $(App_Name)"
displayName: "Set pipeline job Run description for Azure DevOps console"
For anyone who simply wants to set the name of an Azure Pipelines run, you can find the description here.
TLDR: Set the top-level name: attribute.
Here is the PowerShell script to set release description in DevOps. Before the stage that need approval, add a PowerShell task to invoke REST API to change the release description. Please use your own orgName, projectName, PAT and modify the release description.
$url = https://vsrm.dev.azure.com/<orgName>/<ProjectName>/_apis/release/releases/$(Release.ReleaseId)?api-version=6.0
$token="PAT"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$head = #{ Authorization =" Basic $base64AuthInfo" }
$pipeline = Invoke-RestMethod -Uri $url -Headers $head -Method Get
$Pipeline.description = "<Modify the release description>"
$body = $Pipeline | ConvertTo-Json -Depth 100
$url2=https://vsrm.dev.azure.com/<orgName>/<ProjectName>/_apis/release/releases/$(Release.ReleaseId)?api-version=6.0
$resp = Invoke-RestMethod -Uri $url2 -Method Put -Headers $head -Body $body -ContentType application/json

Is there any azure DevOps rest API that will fetch the list if releases along with their environment status by providing the build-id?

I have a problem where if I provide a build id and use 'LIST Release Azure DevOps API' it is fetching all the releases present in that build. But here the problem is that the 'LIST API' is not providing details of the environments present in the list of releases. I need to make another request with the release id to fetch the environment details for every release. Is there any option that will combine both these operations?
You can provide the $expand=environments parameter in the List release API to include the details of environments in the response result. See here.
https://vsrm.dev.azure.com/{org}/{proj}/_apis/release/releases?sourceId={projectGuid}:{BuildDefinitionId}&$expand=environments&api-version=6.1-preview.8
See below example in powershell scripts:
$url = "https://vsrm.dev.azure.com/{org}/{proj}/_apis/release/releases?sourceId={projectGuid}:{BuildDefinitionId}&`$expand=environments&api-version=6.1-preview.8"
$PAT = "Personal access token"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
Invoke-RestMethod -Uri $reurl -Headers #{authorization = "Basic $base64AuthInfo"} -Method get

How to execute next stage in Azure Devops Release pipeline?

In Azure devops, Release pipeline, I am trying to run a stage after two different stage as below. I am facing issue in running the API-test stage.
Dev stage is auto triggered.
QA stage is manual triggered.
API-test is needs to run either Dev/QA is successful.
Expected:
API-test needs to run if either Dev or QA stage is successful.
Actual:
API-test stage is not triggered when Dev stage is successful.
Kindly let me know the required configuration.
Besides of duplicating the API-Test stage, another workaround is to use Update Release Environment rest api. See below steps:
1, Set API-Test stage only be auto triggered after Dev stage.
2, Go the security page of your release edit page.
Set the Manage deployments to allow for account yourProjectname Build Service(Your Organization). This persmission will allow you to update the release environment in the release pipeline.
3, Go to QA stage-->In the Agent job section-->Check Allow scripts to access the OAuth token. This setting will allow you to use the accesstoken in the release pipeline.
4, After above preparation, you can now add a script task at the end of QA stage to call the release rest api. See below example in powershell task:
#Get releaseresponse
$Releaseurl= "https://vsrm.dev.azure.com/{yourOrg}/$(System.TeamProject)/_apis/Release/releases/$(Release.ReleaseId)?api-version=6.0-preview.8"
$releaseresponse = Invoke-RestMethod -Method Get -Headers #{Authorization = "Bearer $(system.accesstoken)"} -ContentType application/json -Uri $Releaseurl
#Get the environment ID of API-Test stage from the release response:
$id = $releaseresponse.environments | Where-Object{$_.name -match "API-Test"} | select id
#Create the JSON body for the deployment:
$deploymentbody = #"
{"status": "inprogress"}
"#
#Invoke the REST method to trigger the deployment to API-Test stage:
$DeployUrl = "https://vsrm.dev.azure.com/{yourOrg}/$(System.TeamProject)/_apis/release/releases/$(Release.ReleaseId)/environments/$($id.id)?api-version=6.0-preview.7"
$DeployRelease = Invoke-RestMethod -Method Patch -ContentType application/json -Uri $DeployUrl -Headers #{Authorization = "Bearer $(system.accesstoken)"} -Body $deploymentbody
Above scripts first call get Release rest api to get the environment id of API-Test stage.
Then call the update release environment rest api to trigger the deployment to API-Test.
So that above script can achieve the API-Test stage be triggered after manually Deployment to QA stage is successfully.

Handling multiple azure devops pipelines

I have several azure devops pipeline files in one project. The files are all in a subdirectory and called azure-pipelines.yml.
Renaming: I can rename the pipelines in the UI to distinguish them... but I would like to skip that manual step and perform that in the yml. Is there a parameter for that - I cannot find it in the docs.
Workdirs: the pipelines start in the main directory. I can adjust the working directory of the script steps with workingDirectory thanks to the answer here. But can we also adjust that for the entire pipeline?
There is not a parameter for renaming the pipelines. There are two ways to rename the pipelines. One is to manually rename them from the UI. Another way is through build definition update rest api.
Below is an example in powershell scripts to rename the pipeline through rest api.
the scripts first get the build definition by build definition get api. Then assign a new name for the build definition, and update the definition with the new name.
$create = "https://dev.azure.com/{ORG}/{PROJ}/_apis/build/definitions/{DefinitionId}?api-version=5.1"
$PAT="{Person access token}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
$result = Invoke-RestMethod -Uri $create -Headers #{authorization = "Basic $base64AuthInfo"} -Method get
$result.name = "YamlPipeline-newName"
$updateBody= $result | ConvertTo-Json -Depth 100
$result7 = Invoke-RestMethod -Uri $create -Headers #{authorization = "Basic $base64AuthInfo"} -Method put -ContentType application/json -Body $updateBody
You cannot change the workingdirectory for the entire pipeline. You can change the workingdirectory inside the tasks.
And there are predefined variables you can use to refer to the places in the agents.
For below example:
$(Agent.BuildDirectory) is mapped to c:\agent_work\1
%(Build.ArtifactStagingDirectory) is mapped to c:\agent_work\1\a
$(Build.BinariesDirectory) is mapped to c:\agent_work\1\b
$(Build.SourcesDirectory) is mapped to c:\agent_work\1\s
You can also submit a feature request for above renaming pipeline and adjust workingdirectory for the entire pipeline(click Suggest a feature and choose Azure Devops) to Microsoft Development team. Hope they will consider supporting these feature in the future.

Azure Devops : Updating Variable in VariableGroup [duplicate]

Am working on Azure Devops CI&CD. Here, my release name must be in Version number using tags. Am getting this with the help of Variable Groups, by adding tags and value to it. Here am getting the tags value as constant like a static for every release like 1.1,1.2,1.3 etc.
Now am trying to increase/update my tag value dynamically for every new release triggered after completion of my Build Definition successfully which looks like 1.1,1.2,2.1,2.2,3.1,3.2 etc.It is possible with the help of statically by variable group, but manually we need to update it.
Is it possible to Increase/Update the tags value in Variable Group with the Build Definition tasks or other process.If possible, please suggest me to "How to done this?"
You can overwrite/update the value of the variables by using the logging command to set the variables again in Azure Devops Build pipleline:
Write-Host "##vso[task.setvariable variable=testvar;]testvalue"
To increase the value dynamically, you need to use the token $(Rev:.r). You can custom the variables based on the $(Build.BuildNumber) or $(Release.ReleaseName)as they will increase the value dynamically...
Just reference this thread to custom the variables:https://github.com/MicrosoftDocs/vsts-docs/issues/666#issuecomment-386769445
UPDATE:
If you just want to update the value of the variables which defined in a specific Variable Group, then you can call REST API in build pipeline to achieve that:
PUT https://{account}.visualstudio.com/{ProjectName or ID}/_apis/distributedtask/variablegroups/{Variable Group ID}?api-version=5.0-preview.1
Content-Type: application/json
Request Body:
{"id":2,"type":"Vsts","name":"VG0926","variables":{"TEST0926":{"isSecret":false,"value":"0930"}}}
UPDATE2:
You can write a PowerShell script to call the REST API, then add a PowerShell task to run the script in your build pipeline: (Use the OAuth token to access the REST API)
Below sample for your reference:
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/distributedtask/variablegroups/{Variable Group ID}?api-version=5.0-preview.1"
Write-Host $url
function CreateJsonBody
{
$value = #"
{"id":2,"type":"Vsts","name":"VG0926","variables":{"TEST0926":{"isSecret":false,"value":"0930"}}}
"#
return $value
}
$json = CreateJsonBody
$pipeline = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers #{
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
}
Write-Host "New Variable Value:" $pipeline.variables.TEST0926.value
UPDATE3:
Well, tested again, below scripts works for me as well. You can try it, just replace the parameters accordingly:
# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "","PAT here")))
$url = "https://dev.azure.com/xxx/Test0924/_apis/distributedtask/variablegroups/1?api-version=5.0-preview.1"
$json = '{"id":1,"type":"Vsts","name":"VG0928","variables":{"TEST0928":{"isSecret":false,"value":"0931"}}}'
$pipeline = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)}
Write-Host "New Variable Value:" $pipeline.variables.TEST0928.value
You can simply update any number of variables in an Azure Devops variable group using its built-in az pipelines variable-group variable update command. You can use this command in a script task in the pipeline definition as shown below.
ps: Replace all the upper case values with corresponding values except the SYSTEM_ACCESSTOKEN.
variables:
- group: VARIABLE_GROUP_NAME
jobs:
- job: UpdateVarGroup
steps:
- script: |
newValue="This is the updated value"
echo $SYSTEM_ACCESSTOKEN | az devops login
az pipelines variable-group variable update --group-id $(group_id) \
--name NAME_OF_THE_VARIABLE \
--value "${newValue}" \
--org https://dev.azure.com/YOUR_ORGANIZATION_NAME \
--project AZURE_DEVOPS_PROJECT
displayName: 'Update variable inside a variable group'
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
In order for this example to work, you need to have a variable inside your variable group called group_id. The value of that needs to be set to the group id of the variable group, which can be obtained by simply looking at the url of your variable group. (The group id is the value for variableGroupId in the url of your browser when you are inside the variable group)
System.AccessToken is required for az devops login
I used this task to update the value of my variables inside my group.
Shared variable updater (preview)
Dont forget to set those settings :
Requires 'Allow scripts to access the OAuth token' in agent job additional options
Set administrator role to 'Project Collection Build Service' in the variable group.
In case of using a YAML pipeline
When using a YAML pipeline, the OAuth token is automatically added (no need for step 1 above), but requires a bit of work to make accessible for the powershell script. Use the guidance here to be able to use the token.
If you want to update a variable group's value, use the REST.API methods.
Rather than constructing the PUT request body manually, use a GET first to get the original JSON, update only what you need, then replay it as a PUT. I used to keep the variable group id also as a variable in that group to avoid hard-coding.
variables:
- group: MyVarGroup
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
$url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/distributedtask/variablegroups/$(VariableGroupId)?api-version=6.0-preview.1"
$header = #{
"Authorization" = "Bearer $(System.AccessToken)"
}
$def = Invoke-RestMethod -Uri $url -Headers $header
$def.variables.MyTestVar.value = "NewValue"
$body = #($def) | ConvertTo-Json -Depth 100 -Compress
$def = Invoke-RestMethod -Method 'Put' -Uri $url -ContentType 'application/json' -Headers $header -Body $body
You can overwrite variables using the REST API with a PowerShell task without needing to create a PAT.
You'll need to first going into the agent job and set "Allow scripts to access OAuth token".
You'll need too go to your variable group and add the 'Project Collection Build Service' as an administrator.
Now you can call the Rest API using the OAuth Bearer token. Code for reference:
$id = <variable group id>
# This is using some environment variables provided by the pipeline to build the URL
$url = ("$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/distributedtask/variablegroups/{0}?api-version=5.0-preview" -f $id)
# You might find it useful to us a GET method to grab the variable group, update it and then convert it to this json string rather than doing this here
$json = '{"id":$id,"type":"Vsts","name":"<Variable Group Name>","<Variable Name":{"ThisIsMyVariable":{"isSecret":false,"value":"20"}}}'
$pipeline = Invoke-RestMethod -Uri $url -Method Put -Body $json -ContentType "application/json" -Headers #{Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
I made this task to manage variable groups from Pipelines:
ManageVariableGroupTask
Using this you can Create/Update/Delete variable groups and do the same operations on variables contained in them.
Hope that's helpfull!

Resources