Azure Devops Pipeline YAML中的Git标记名称 [英] Git tag name in Azure Devops Pipeline YAML
问题描述
摘要
如何在Azure Devops Pipeline YAML文件中获取当前git标记的名称?
How do I get the name of the current git tag in an Azure Devops Pipeline YAML-file?
我要做什么?
我正在Azure Azure中建立构建管道.当创建新的git标签时,管道触发.然后,我想构建docker映像并用git标记的名称标记它们.
I am setting up a build pipeline in Azure Devops. The pipeline triggers when a new git tag is created. I then want to build docker images and tag them with the git tag's name.
我的YAML管道如下所示:
My YAML pipeline looks something like this:
# Trigger on new tags.
trigger:
tags:
include:
- '*'
stages:
- stage: Build
jobs:
- job: Build
pool:
vmImage: 'ubuntu-latest'
steps:
- script: export VERSION_TAG={{ SOMEHOW GET THE VERSION TAG HERE?? }}
displayName: Set the git tag name as environment variable
- script: docker-compose -f k8s/docker-compose.yml build
displayName: 'Build docker containers'
- script: docker-compose -f k8s/docker-compose.yml push
displayName: 'Push docker containers'
我所引用的docker-compose文件是这样的:
And the docker-compose file I am referencing something like this:
version: '3'
services:
service1:
image: my.privaterepo.example/app/service1:${VERSION_TAG}
build:
[ ... REDACTED ]
service2:
image: my.privaterepo.example/app/service2:${VERSION_TAG}
build:
[ ... REDACTED ]
如您所见,docker-compose文件中的标签名称取自环境变量VERSION_TAG
.在YAML管道中,我试图根据当前的GIT标签设置环境变量VERSION_TAG
.那...我怎么得到标签的名字?
As you can see, the tag name in the docker-compose file is taken from the environment variable VERSION_TAG
. In the YAML pipeline, I am trying to set the environment variable VERSION_TAG
based on the current GIT tag. So... how do I get the name of the tag?
推荐答案
好的,这比我预期的要复杂一些.这是设置变量所需的步骤:
Ok, this was a bit trickier than I expected. Here's the step required to set the variable:
steps:
- script: VERSION_TAG=`git describe --tags` && echo "##vso[task.setvariable variable=VERSION_TAG]$VERSION_TAG"
displayName: Set the tag name as an environment variable
此脚本将变量VERSION_TAG设置为最新git标签的名称.它分三个步骤完成:
This script sets the variable VERSION_TAG to the name of the latest git tag. It does so in three steps:
1:git describe --tags
打印当前/最新标签的名称
Prints the name of the current/latest tag
2:VERSION_TAG=`...`
将步骤1的输出设置为局部变量
Sets the output of step 1 to a local variable
3:echo "##vso[task.setvariable variable=VERSION_TAG]$VERSION_TAG"
打印出一个在Azure Devops中设置变量的命令.将在步骤2中设置的局部变量用作值.
Prints out a command that sets the variable in Azure Devops. The local variable set in step 2 is used as the value.
这篇关于Azure Devops Pipeline YAML中的Git标记名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!