Warm tip: This article is reproduced from serverfault.com, please click

How to increment tags in Azure DevOps Pipeline (Salesforce)

发布于 2020-12-03 06:49:14

Sorry I am kinda new to Azure DevOps (I usually use Github Actions and unfortunately, I was tasked to do it on AzureDevOps) Anyway, I was wondering if it's possible to automatically increment the tags I have on my branch after a successful deployment? Here's my current setup on AzureDevOps.

  - script: |
    LastStableTag=`git tag -l testTag_v* --sort=creatordate | tail -n1`
    echo $LastStableTag
    CurrentCommitID=`git rev-parse --verify HEAD`
    echo $CurrentCommitID 
    npx sfdx sfpowerkit:project:diff -t $CurrentCommitID -r $LastStableTag -d delta
  displayName: 'Generate Delta'

Basically, what I do is get the difference of Salesforce Components between the Feature Branch and the Dev Branch (which is based on the latest tag). Validate it first then once it has passed and has been merged, another pipeline will run and do the actual deployment.

On GitHub Actions, this is the code that I used to increment the tags, bump and push the next version. I did try to do this on Azure DevOps, but I'm stuck and not sure what to do next. Is there any way I can do it on Azure DevOps? Or should I go with a different approach? Any help would be really much appreciated! Thank you.

- name: Get next version
  run: |
       CurrentVersion=`git tag -l testtag_v* --sort=creatordate | tail -n1 | awk -F'v' '{print $2}'`
       echo CURRENT VERSION: $CurrentVersion
       major=`echo $current_version | cut -d'.' -f 1`
       minor=`echo $current_version | cut -d'.' -f 2`
       minor=`expr $minor + 1`
       patch=`echo $current_version | cut -d'.' -f 3`
       NextVersion=`echo $major.$minor.$patch`
       echo NEXT VERSION: $NextVersion
       echo "::set-output name=next_version::$(echo $NextVersion)"
  id: NextVersion
  
- name: Zip delta folder
  run: |
       zip -r delta.zip delta/
       
- name: Bump version and push tag
  uses: "marvinpinto/action-automatic-releases@latest"
  with:
      repo_token: "secrets"
      prerelease: true
      automatic_release_tag: "testtag_v${{ steps.next_version.outputs.NextVersion }}"
      files: |
        delta.zip
Questioner
Percy Jackson
Viewed
0
Vito Liu-MSFT 2020-12-04 13:43:28

We could create the tag via this REST API

Steps:

We could refer to this doc to create PAT token.

Open build pipeline definition->click the tab variable->add variable pat and set the value to secret.

enter image description here

Add task power shell and enter the below script

$connectionToken="$(pat)"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$URL = "$($env:SYSTEM_TEAMFOUNDATIONSERVERURI)$env:BUILD_REPOSITORY_NAME/_apis/git/repositories/$($env:BUILD_REPOSITORY_ID)/annotatedtags?api-version=5.1-preview.1" 

Write-Host "Hello World" $URL

Write-Host "Hello World" $($env:BUILD_SOURCEVERSION)


$Body = @"
{
  "message": "test",
  "name": "Just",
  "taggedObject": {
    "objectId": "$($env:BUILD_SOURCEVERSION)"
  }
}
"@

Write-Host "Hello World" $Body


$Response = Invoke-RestMethod -Uri $URL -ContentType "application/json" -Body $Body  -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST

Note: objectID is commit ID

By the way, we could add task bash and enter to script printenv to list all env variable and then replace the field with env variable.

Result:

enter image description here

Update1

We could get the existing tag via this REST API, your tag format is Testtag-v1.0.0, we could add for each in the power shell script to increment the tag and then set the value to request body name. Or if you add tag commit the tag via pull request, we could get the tag and then set it. You could refer to this answer for more details.