jdylanmc
jdylanmc

Reputation: 859

Reading Git Tags in Azure Devops Pipelines?

I have an Azure Devops pipeline running as an Azure Hosted Windows 2019 VM.

I am attempting to fetch the latest tag name from my git repository, as this is used for semantic versioning in my application.

In my local machine, I can open a powershell window and use a command similar to this: git describe --abbrev=0 --tags. This fetches the latest tag applied to the repository.

Within Azure Devops, I receive the error: ##[error]PowerShell exited with code '1'. when executing the same command.

How can I interrogate my git repository from within an Azure Devops pipeline?

EDIT:

Here's an example:

This powershell step (inline on an Azure windows-2019 cloud agent):

Write-Host "Hello World"
$last_tag = & 'git' 'describe' '--tags' '--abbrev=0'
Write-Host "Version is $($last_tag)"

Results in this output:

2020-07-07T12:54:06.1632792Z ##[section]Starting: PowerShell Script
2020-07-07T12:54:06.1774384Z ==============================================================================
2020-07-07T12:54:06.1775151Z Task         : PowerShell
2020-07-07T12:54:06.1775845Z Description  : Run a PowerShell script on Linux, macOS, or Windows
2020-07-07T12:54:06.1776215Z Version      : 2.170.1
2020-07-07T12:54:06.1776715Z Author       : Microsoft Corporation
2020-07-07T12:54:06.1777139Z Help         : https://learn.microsoft.com/azure/devops/pipelines/tasks/utility/powershell
2020-07-07T12:54:06.1777971Z ==============================================================================
2020-07-07T12:54:10.5356566Z Generating script.
2020-07-07T12:54:10.6190142Z ========================== Starting Command Output ===========================
2020-07-07T12:54:10.6706776Z ##[command]"C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'd:\a\_temp\efb4a469-a426-480e-b0c6-7a2b2c47b0f8.ps1'"
2020-07-07T12:54:14.0840142Z Hello World
2020-07-07T12:54:14.1208411Z fatal: No names found, cannot describe anything.
2020-07-07T12:54:14.2053268Z Version is 
2020-07-07T12:54:14.4822185Z ##[error]PowerShell exited with code '1'.
2020-07-07T12:54:14.5315567Z ##[section]Finishing: PowerShell Script

Upvotes: 0

Views: 2244

Answers (2)

Leo Liu
Leo Liu

Reputation: 76928

Reading Git Tags in Azure Devops Pipelines?

I could reproduce this issue on my side. That because we are using git feature, but we are not in the folder managed by git.

When we use powershell task directly, the default work folder for it is:

PS C:\Users\<Username>

Obviously, there are no files managed by our git in this directory. That the reason why you get the error No names found, cannot describe anything..

So, to resolve this issue, we just need to switch the working folder to the repo folder by the command cd $(System.DefaultWorkingDirectory):

Write-Host "Hello World"

cd $(System.DefaultWorkingDirectory)

$last_tag =  & 'git' 'describe' '--tags' '--abbrev=0'

Write-Host "Version is $($last_tag)"

Now, the result is:

enter image description here

Hope this helps.

Upvotes: 2

Shamrai Aleksander
Shamrai Aleksander

Reputation: 16163

This is my working example of getting last tag with powershell step in azure devops pipeline:

$last_tag = & 'git' 'describe' '--tags' '--abbrev=0' '--match' '"$(BUILDNAME)_[0-9]*_[0-9]*"'

In you case will be something like that:

$last_tag = & 'git' 'describe' '--tags' '--abbrev=0'

Update:

This is issue of you repository. Check this answer : git describe fails with "fatal: No names found, cannot describe anything."

Git can not find tags and returns fail.

Upvotes: 0

Related Questions