总结
如何在 Azure Devops Pipeline YAML 文件中获取当前 git 标记的名称?
我想做什么?
我正在 Azure Devops 中设置构建管道。当创建新的 git 标签时,管道会触发。然后我想构建 docker 镜像并用 git 标签的名称标记它们。
我的 YAML 管道看起来像这样:
# 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 文件是这样的:
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
。那么...我如何获得标签的名称?
最佳答案
好的,这比我预期的要棘手一些。这是设置变量所需的步骤:
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 标签的名称。它分为三个步骤:
1: git describe --tags
打印当前/最新标签的名称
2: VERSION_TAG=`...`
将步骤 1 的输出设置为局部变量
3: echo "##vso[task.setvariable variable=VERSION_TAG]$VERSION_TAG"
打印出在 Azure Devops 中设置变量的命令。在步骤 2 中设置的局部变量用作值。
关于Azure Devops Pipeline YAML 中的 Git 标记名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56575840/