Azure Devops Pipeline YAML 中的 Git 标记名称

标签 git azure-devops yaml azure-pipelines git-tag

总结

如何在 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/

相关文章:

git - 如何在 IntelliJ IDEA 中存储 SSH 主机 key

git - 无法使用 ssh 连接到 Visual Studio Online git repo

azure - 使用 Azure DevOps Server 2019 将 Docker 容器上的 ASPNETAPP 部署到 Windows VM

python - 根据 YAML 文件中的用户输入将自定义字段添加到 JSON 文件 - Python3

git show 修改文件,但我没有更改任何内容 git reset 不起作用

Git 不在提交中显示作者详细信息

git - 标准错误 : fatal: cannot exec '/tmp/ssh**.sh' : Permission denied fatal: unable to fork

typescript - 用于在 Visual Studio 和 Visual Studio Team Services 构建中运行 TypeScript 测试的 Chutzpah 配置

java - 用于运行 JUnit 测试的 YML 文件

syntax - 创建此数组的 Yaml 语法