How to promote the package with UniversalPackages#0 task in AzureDevops - azure

Is there an option to promote universal package view with azure devops yaml task?

There is no option to promote universal package view in UniversalPackages #0 task. As a workaround, besides using extension, you can also promote universal package view through script in powershell task.
$token = "Enter your Pat here"
$url = "https://pkgs.dev.azure.com/OrgName/ProjectName/_apis/packaging/feeds/FeedID/upack/packagesbatch?api-version=5.1-preview.1"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = #'
{
"data": {
"viewId": "Release"
},
"operation": 0,
"packages": [{
"id": "YourPackageName",
"version": "PackageVersion",
"protocolType": "upack"
}]
}
'#
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON
Enter your own PAT in $token, and replace the OrgName, ProjectName, FeedID, YourPackageName, PackageVersion with your own ones, you can set PreRelease/Release in viewID to promote universal packages in Azure DevOps feed view from #local to #pre-release to #release.
Test result:

Check out this extension:
https://marketplace.visualstudio.com/items?itemName=rvo.vsts-promotepackage-task
It allows you to promote packages in a feed.

Related

Integrate CI and CD together Azure Devops

we need your support on enabling continues deployment on our release pipeline .
Environment :
CI or Build Pipeline is on Azure Devops Services
CD or Release pipeline is on Azure Devops Server
We want to Integrate CI and CD together right now after Build release is not kicking automatically.(I have to manually execute the release )
[![enter image description here][1]][1]
[![enter image description here][2]][2]
[![enter image description here][3]][3]
Service connection between azure devops service and azure devops server
[![enter image description here][4]][4]
# Trigger Release pipeline
- task: PowerShell#2
displayName: 'Trigger Release pipeline'
inputs:
targetType: 'inline'
powershell: |
$url = "https://vsrm.dev.azure.com/{OrganizationName}/{ProjectName}/_apis/release/releases?api-version=6.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($env:TOKEN)"))
$JSON = #'
{
"definitionId": 38,
"variables": {
"Version": {
"value": "$(build.buildnumber)"
}
}
}
'#
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON
displayName: 'PowerShell Script'
env:
TOKEN: $(token)```
[1]: https://i.stack.imgur.com/g4J8I.png
[2]: https://i.stack.imgur.com/njsVU.png
[3]: https://i.stack.imgur.com/MIaJJ.png
[4]: https://i.stack.imgur.com/20wk9.png
We want to Integrate CI and CD together right now after Build release is not kicking automatically.
Since the azure devops service is on the cloud side and the azure devops server is local, there is no out-of-the-box feature that can Integrate CI/CD.
But you could use PowerShell task to run the Rest API in Azure Devops Service to trigger the Release on Azure Devops Server . Releases - Create
Here is an example:
You can add the Powershell Task to the end of the build, then you could add the following script in the powershell task:
$token = "PAT"
$url = "https://{instance}/{collection}/{project}/_apis/release/releases?api-version=5.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = #'
{
"definitionId": DefinitionID(e.g. 15),
"description": "Creating Sample release",
"artifacts": [],
"isDraft": false,
"reason": "none",
"manualEnvironments": null
}
'#
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON
If your CI/Build pipeline is running on self-hosted agent, you can directly add the powershell task at the same agent job.
If your build pipeline is running on Microsoft-hosted agent, you need to create a self-hosted agent and add additional agent job to run powershell script.
In this case, you also need to set the Dependencies.
Note: When running the rest api to trigger the azure devops server release, you need to ensure that they are in the same network range. So it needs self-hosted agent.
Update:
To define a stage, you could refer to the following doc and sample:
stages:
- stage: A
jobs:
- job: A1
pool:
name: Default
steps:
- script: echo
- stage: B
pool:
name: Default
jobs:
- job: B1
steps:
- task: PowerShell#2
inputs:
targetType: 'inline'
script: |
$token = "PAT"
$url = "https://{instance}/{collection}/{project}/_apis/release/releases?api-version=5.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = #'
{
"definitionId": ID,
"variables": {
"Version": {
"value": "$(Build.buildnumber)"
}
}
}
'#
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON
Update2:
In order to achieve a function similar to the system.accesstoken variable, you can try the following settings.
Step1: Create a variable in Azure Devops Service Build Pipeline and set it as variable:
Step2: PowerShell Task script:
- powershell: |
$url = "https://{instance}/{collection}/{project}/_apis/release/releases?api-version=5.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($env:TOKEN)"))
$JSON = #'
{
"definitionId": 38,
"variables": {
"Version": {
"value": "$(build.buildnumber)"
}
}
}
'#
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON
displayName: 'PowerShell Script'
env:
TOKEN: $(token)

How to import/upload multiple json files to Azure Devops Pipeline Release and Builds?

I have multiple Azure DevOps release and build (.json) files saved on my desktop. The script below is to add one json file. I am trying to import multiple build or release json files from my desktop to Azure DevOps?
Can someone please help?
$token = "PAT token"
$url = "https://dev.azure.com/{organization}/{Project}/_apis/build/definitions?api-version=6.0-preview.2"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = #'
request body
'#
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON
How to import/upload multiple json files to Azure Devops Pipeline Release and Builds?
We could use powershell scripts to parse the names of these json files from your desktop folder:
$JsonNames = Get-ChildItem C:\Users\<UserName>\Desktop\*.json | Select-Object -ExpandProperty Name
Then we could loop this JsonNames to import to Azure Devops Pipeline Release and Builds:
ForEach ($JN in $JsonNames)
{
$token = "PAT token"
$url = "https://dev.azure.com/{organization}/{Project}/_apis/build/definitions?api-version=6.0-preview.2"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON= Get-Content "C:\Users\<UserName>\Desktop\$($JN).json"
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON
}

Change variable group value of Azure Pipeline

I have a Variable Group in Azure Pipeline(below attached),
I want to change the value of isPOS to False through below mentioned conditions,
$angular = (Get-Content "$(system.defaultworkingdirectory)/amaze-commerce/angular.json" -Raw) | ConvertFrom-Json
if ($angular.defaultProject -ne "ecomm-ac-ecomm"){
// Please add your code here
}
Can you please guide me to change the Variable group value and i want to use the same in Release Pipeline.
In order to change the value of a variable group use Azure DevOps Api.
An example of how to update a variable group:
Variablegroups - Get Variable Groups - filtered by {groupName}
$pat ="<your-tfs-security-token>"
$read = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$pat"))
$variableGroupName = "Check for angular.json"
$variableGroupId = ((Invoke-RestMethod -Method Get -Headers #{'Authorization' = "Basic $read" } -Uri "https://{instance}/{collection}/{project}/_apis/distributedtask/variablegroups?groupName=${variableGroupName}").Value).id
Variablegroups - Update
$body =#"
{
"variables": {
"isPOS": {
"value": $false
}
},
"type": "Vsts",
"name": "${variableGroupName}",
"description": "Updated variable group"
}
"#
$response= (Invoke-RestMethod -Uri "https://{instance}/{collection}/{project}/_apis/distributedtask/variablegroups/${variableGroupId}?api-version=5.0-preview.1" -Method Put -Headers #{'Authorization' = "Basic $read" } -ContentType "application/json" -Body $body)

Promote npm packages in AzureDevops feed view from #local to #pre-release to #release, through shell/pyhthon/powershell script?

How to Promote npm packages in Azure DevOps feed view from #local to #pre-release to #release, through shell/python/PowerShell script?
You can try to write a script that does it with Azure DevOps Rest API, but there are two extensions that already did it:
1) Promote package to Release View
2) Promote
How to Promote npm packages in Azure DevOps feed view from #local to
#pre-release to #release, through shell/python/PowerShell script?
As Shayki Abramczyk said above, there already exists extensions to do that job. But if you do want a working PS script, here's mine:
$token = "Enter your Pat here"
$url = "https://pkgs.dev.azure.com/OrgName/ProjectName/_apis/packaging/feeds/FeedID/npm/packagesbatch?api-version=5.1-preview.1"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = #'
{
"data": {
"viewId": "Release"
},
"operation": 0,
"packages": [{
"id": "YourPackageName",
"version": "PackageVersion",
"protocolType": "Npm"
}]
}
'#
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Post -ContentType application/json -body $JSON
Note:
Enter your own PAT in $token, and replace the OrgName, ProjectName, FeedID, YourPackageName, PackageVersion with your own ones, you can set PreRelease/Release in viewID to promote npm packages in Azure DevOps feed view from #local to #pre-release to #release.
Update 1
If you want to run it in Linux environment using bash, try using this script:
curl --header 'Content-Type: application/json' -X Post --user PAT:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
https://pkgs.dev.azure.com/YourOrgName/YourProjectName/_apis/packaging/feeds/YourFeedName/npm/packagesbatch?api-version=5.1-preview.1 \
--data '{ "data": {"viewId":"Release"},"operation": 0,"packages": [{"id": "YourPackageName","version": "YourPackageVersion","protocolType": "Npm"}]}'
To Generate PAT: https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page#create-a-pat

Azure DevOps Rest API to create a branch from a specific branch

I'm looking for an Azure DevOps Rest API to create a new branch from an existing branch.
Azure DevOps Rest API to create a branch from a specific branch
Konteks pointed out the correct REST API.
We could use the Initial commit (Create a new branch) to create a branch, but if you want to create a branch from a specific branch, we need modify the Request Body.
POST https://dev.azure.com/fabrikam/_apis/git/repositories/{repositoryId}/pushes?api-version=5.1
First, we need use the REST API Repositories - List to get the repositoryId.
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=4.1
Then, use the REST API Refs - List with filter=<BranchName> to get the oldObjectId for your specific branch:
GET https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/refs?filter=heads/master&api-version=5.1
Now, we could use the REST API Repositories - List with following request body to create a new branch from a specific branch:
{
"refUpdates": [
{
"name": "refs/heads/<DefineYourNewBranchName>",
"oldObjectId": "<oldObjectId we get from above REST API>"
}
],
"commits": [
{
"comment": "Initial commit.",
"changes": [
{
"changeType": "add",
"item": {
"path": "/readme111.md"
},
"newContent": {
"content": "My first file!",
"contentType": "rawtext"
}
}
]
}
]
}
Result from postman:
This is my test powershell scripts:
$connectionToken="<PAT Here>"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$headers = #{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN" }
$url= "https://dev.azure.com/<Organizationname>/<ProjectName>/_apis/git/repositories/<repositoryId>/pushes?api-version=5.1"
$body =#"
{
"refUpdates": [
{
"name": "refs/heads/Test228",
"oldObjectId": "a57f0c34f8ec7330bdc37e7631f7be3cc3ea3956"
}
],
"commits": [
{
"comment": "Initial commit.",
"changes": [
{
"changeType": "add",
"item": {
"path": "/readme111.md"
},
"newContent": {
"content": "My first file!",
"contentType": "rawtext"
}
}
]
}
]
}
"#
Write-Host "$url"
$response= Invoke-RestMethod -Uri $url -ContentType "application/json-patch+json" -Body $body -headers #{authorization = "Basic $base64AuthInfo"} -Method POST
Hope this helps.
I captured the requests created by the AzureDevOps web UI and found out that creating a branch from a specific branch is quite simple task for the REST api.
$repository = "repositoryName"
$newBranch = "newBranchName"
$baseBranch = "baseBranchName"
$organization = "organizationName"
$project = "projectName"
$pat = "personalAccessToken"
$base64AuthInfo = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$pat"))
$headers = #{ Authorization = "Basic $base64AuthInfo" }
# Get ID of the base branch
$url = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/refs?filter=heads/$baseBranch&api-version=5.1"
$baseBranchResponse = Invoke-RestMethod -Uri $url -ContentType "application/json" -headers $headers -Method GET
# Create a new branch
$url = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repository/refs?api-version=5.1"
$body = ConvertTo-Json #(
#{
name = "refs/heads/$newBranch"
newObjectId = $baseBranchResponse.value.objectId
oldObjectId = "0000000000000000000000000000000000000000"
})
$response = Invoke-RestMethod -Uri $url -ContentType "application/json" -Body $body -headers $headers -Method POST
This worked for me:
To create/update/delete:
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/refs/update%20refs?view=azure-devops-rest-5.0#create/update/delete-a-ref-by-repositoryid
To get objectId:
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/refs/list?view=azure-devops-rest-5.0#refs
https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pushes/create?view=azure-devops-rest-5.1#initial-commit-(create-a-new-branch)
See section "initial commit"
Sparrow plugin does what you need ( using Rest API ):
s6 --plg-run ado-git-branch-create#project=Backends,repo=Catalog,branch_from=dev,branch=feature
Please pay attention, it does not create any dummy files for that, it only references for an existing branch through it's internal object ID.
Feel free to still the code ;-)).
PS Disclosure. I am the tool author, more details could be found here - https://github.com/melezhik/Spazure

Resources