Richard Szalay
Richard Szalay

Reputation: 84744

Determine the previous commit during a VSTS release

When performing a deployment, I'm looking to perform some optimisations depending on whether certain file paths have been changed since the previous release.

Is there a way to find out what the previous commit hash on the target environment was? I'm guessing it would need to go "Previous Release" -> "Build Artifact" -> "Commit", but I'm stuck on the first step.

To clarify, I want to be able to list the commits displayed in the "Deploy" modal:

enter image description here

Upvotes: 0

Views: 848

Answers (1)

Marina Liu
Marina Liu

Reputation: 38106

If the build definition builds for multiple branches

Then you need to get the last commit sha-1 value by the previous release/build and then get the last commit. The steps to achieve as below:

  1. Get last release

    use the REST API:

    GET https://{account}.vsrm.visualstudio.com/{project}/_apis/release/releases?$top=2&definitionId={id}api-version=4.1-preview.6
    

    And you will get last two releases for a certain release definition. And for the two releases, one is the current release you are deploying, the other is last release.

  2. Get last buildId from last release

    In the first step, you can get the last release logs from the parameter logsContainerUrl. And you can download the last release log to zip and unzip the logs. Then you can get the buildId from Download artifact step log.

  3. Get last commit sha-1 from buildId

    Use the REST API to get the last build, and you can get the source version (last commit) from response.

If the build definition builds for a certain branch

Then the last commit sha-1 value can be got easier by git commands.

You can use a PowerShell task with below script:

git clone <URL for git repo> repofolder
cd repofolder
git checkout $(Build.SourceBranchName)
$lastcommit=$(git rev-parse HEAD~)
echo "last commit sha-1 value is $lastcommit"

Note:

  • For the git repo URL, you need to procider credential (such as PAT) inside the URL like:

    https://Personal%20Access%20Token:[email protected]/project/_git/repo

  • If the build artifacts is not the Primary artifact for release, you should use the variable $(Release.Artifacts.{alias}.SourceBranchName) instead of $(Build.SourceBranchName) in powershell script.

  • The Fail on Standard Error option in PowerShell task should be deselected.

    enter image description here

Upvotes: 1

Related Questions