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
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.
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:
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.
I get the concept, but I am not sure if this is the answer that I am looking for. Can this also append or increment the existing tag? For example, the current tag on our repository is Testtag-v1.0.0. A developer is done on his user story and merged a pull request. The script will automatically run and once the deployment is done, it will also update the tag to Testtag-v1.1.0. So the next time a pull request is merged, the script will now retrieve the latest tag, which is Testtag-v1.1.0 and contains the latest changes/components.
Basically, my script validate/deploy the delta between two branch using the SHA of the Source Branch, and the latest tag of the Main branch (or Master). So every time a pull request is merged to the main branch (or master), it should automatically update the tag, or increment/append it. Depends if major, minor, or patch.
Hi @PercyJackson, I have updated the answer, you could check it. If you have any issues, you could feel free to share it here.
Hi @PercyJackson, Just checking in to see whether this issue is still blocking you now? Any update for this issue? If this answer is helpful, would you please accept it as the answer? So it could help other community members who get the same issues and we could archive this thread. Thanks. Have a nice day. :)
Hi, yes was able to do it. Thanks!