MW.*_*MW. 7 git yaml git-tag azure-devops azure-pipelines
概括
如何在 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'
Run Code Online (Sandbox Code Playgroud)
我引用的 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 ]
Run Code Online (Sandbox Code Playgroud)
如您所见,docker-compose 文件中的标签名称取自环境变量VERSION_TAG。在 YAML 管道中,我试图VERSION_TAG根据当前的 GIT 标记设置环境变量。那么......我如何获得标签的名称?
好吧,这比我预期的要棘手一些。这是设置变量所需的步骤:
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
Run Code Online (Sandbox Code Playgroud)
此脚本将变量 VERSION_TAG 设置为最新 git 标签的名称。它分为三个步骤:
1: git describe --tags
打印当前/最新标签的名称
2: VERSION_TAG=`...`
将步骤 1 的输出设置为局部变量
3: echo "##vso[task.setvariable variable=VERSION_TAG]$VERSION_TAG"
打印出在 Azure Devops 中设置变量的命令。使用在步骤 2 中设置的局部变量作为值。