We have published all over gradle/maven artifacts to azure devops artifacts repository. Now, we want to use these artifacts in some of the scripts or python code. When i looked into the options to "Connect to feed", it only shows options specific to maven or gradle.
Can we rest calls or APIs to download specific artifacts from azure devops ?
Apologize for the previous answer.
For rest API, of course it can. Refer to this doc first: Maven - Download Package
GET https://pkgs.dev.azure.com/{org name}/{project name}/_apis/packaging/feeds/{feed name}/maven/{groupId}/{artifactId}/{version}/{fileName}/content?api-version=5.1-preview.1
To download this in script, you can use powershell task to achieve it:
$strURL = "https://pkgs.dev.azure.com/{org name}/{project name}/_apis/packaging/feeds/{feed name}/maven/{groupId}/{artifactId}/{version}/{fileName}/content?api-version=5.1-preview.1"
$filePath="D:\"
$fileName=$filePath+"\build.{type}"
$pipeline = Invoke-RestMethod -Uri $strURL -Headers #{
Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"
} -Method GET -OutFile $fileName
Replace the relevant file type to specify it in the $fileName=$filePath+"\build.{type}". After this script finished successfully, you can check your local file in the corresponding file path.
Related
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
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
I have one Azure DevOps build pipeline which deploy few alerts on Azure Cloud with ARM template. I want to deploy alerts on different azure cloud accounts and prior to deploy the alerts I need to list few things as those information is required to deploy the code.
my question is, to get details from another account I need to execute Azure DevOps pipeline with different user which has additional privileges. When I trigger a job it use my credentials so how can I switch user to execute that privileged user to deploy the alerts. Is there any way where I can configure that user in pipeline?
I suggest you can use REST API to run your builds. You can ask other users to provide you with a Personal Access Token, and then you can use this Personal Access Token as credentials to run the REST API.
Here is the document about queue a build: https://learn.microsoft.com/en-us/rest/api/azure/devops/build/builds/queue?view=azure-devops-rest-6.1 .
Here is a sample I tested in postman:
You only need to change the Personal Access Token here to use a different account to run builds.
You can also run this REST API in a PowerShell task. Here is a sample:
$url = "https://dev.azure.com/{organization name}/{project name}/_apis/pipelines/{build id}/runs?api-version=5.1-preview.1"
$contentType = "application/json"
$user="user"
$token="$(PAT)"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
$body= #'
{
"definition":
{
"id":172
}
}
'#
Invoke-RestMethod -Uri $url -Method Post -ContentType $contentType -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body
You can change the PAT in the variables.
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.
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.