Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AzureDevOps Build/Release Pipeline - get git commit message (Build title)?

Tags:

azure-devops

I have a webhook task that runs after a deploy on my release pipeline, currently it just pushes a message to say a new version has been deployed, but I'd like to include the git commit message in this as well to show exactly what has been deployed. I'm not quite sure how to retrieve this, but I'm hoping it's possible as the build is initially triggered from a git push and moreover the Build title is named with this message as per this screenshot: enter image description here

like image 851
Detail Avatar asked Aug 26 '19 18:08

Detail


3 Answers

This is somewhat tricky since the Release Pipeline typically does not have Git access like the Build Pipeline does. To add to this, it is not possible to share variables across pipelines, which would have been an easy solution.

This is how I've done it:

First, Add a Bash script task to your Build Pipeline that exports your last commit message to a file:

CHANGES=`git log -1 --pretty=%B`
mkdir -p $(Build.artifactStagingDirectory)/exported
echo "$CHANGES" > $(Build.artifactStagingDirectory)/exported/CHANGES

Make sure this folder is published as an artifact in your build using the Publish build artifacts task:

Path to publish: $(Build.ArtifactStagingDirectory)/exported

Artifact name: exported

Second, add a Bash script task to your Release Pipeline that picks up these changes from the file and stores them in a variable:

CHANGES=$(cat $(System.DefaultWorkingDirectory)/Development/exported/CHANGES)
echo "##vso[task.setvariable variable=commitComment]$CHANGES"

The last Git change is now available for use in your Build Pipeline as the variable $commitComment.

Note: your exact paths above may vary.

like image 171
Terje Avatar answered Oct 19 '22 07:10

Terje


Ok, I've worked out a way, not sure if it's the best way so let me know if it can be done differently...

I've used a Powershell Task in the Release pipeline which queries the Azure REST API and calls into Git, as follows:

$webClient = New-Object Net.WebClient
$token = "Bearer $env:SYSTEM_ACCESSTOKEN"
$headers = @{ Authorization = $token }

$baseUrl = "https://dev.azure.com/<your company>/<your project>/_apis/git/repositories/<your repo Id>/commits"
$request = "$baseUrl/$env:RELEASE_ARTIFACTS_<your repo name>_SOURCEVERSION"
Write-Host "Request: $request"

$response = Invoke-WebRequest -Uri $request -Headers $headers
$json = ($response | ConvertFrom-Json)
$comment = $json.comment
Write-Host "Response: $comment"

Write-Host "##vso[task.setvariable variable=commitComment;]$comment"

To find out the values for the parameters I've tokenised above, follow this guide: https://learn.microsoft.com/en-us/azure/devops/pipelines/release/variables?view=azure-devops&tabs=powershell#view-the-current-values-of-all-variables

This writes to a custom pipeline variable I've created called "commitComment", which I then use in a marketplace task I found to generate a discord webhook request.

Alternatively, you could just call that directly from Powershell. Hope this helps anyone wanting to do the same..

enter image description here

enter image description here

like image 26
Detail Avatar answered Oct 19 '22 08:10

Detail


Updated version: The json response contains not only the last commit, but a collection of the last 100 commits. You would need to access the first commit in the collection (ordered by date DESC). Also, all <your_custom_values> can be retrieved dynamically, to improve reusability:

$webClient = New-Object Net.WebClient

$token = "Bearer $env:SYSTEM_ACCESSTOKEN"
$headers = @{ Authorization = $token }

$devOpsUrl = "$(System.TeamFoundationCollectionUri)"
$projectName = "$(System.TeamProject)"
$repoId = "$(Build.Repository.Id)"
$repoName = "$(Build.Repository.Name)"

$baseUrl = "$devOpsUrl$projectName/_apis/git/repositories/$repoId/commits"
$request = "$baseUrl/$env:RELEASE_ARTIFACTS_$repoName_SOURCEVERSION"

Write-Host "Request: $request"

$response = Invoke-WebRequest -Uri $request -Headers $headers

$json = ($response | ConvertFrom-Json)
$lastCommit = $json.value[0]

Write-Host "Last Commit: $lastCommit"

$comment = $lastCommit.comment
Write-Host "Comment: $comment"
Write-Host "##vso[task.setvariable variable=CommitMessage;]$comment"
like image 1
Jack B Avatar answered Oct 19 '22 08:10

Jack B