Git Azure Devops Pipeline YAML 中的标记名称

Git tag name in Azure Devops Pipeline YAML

总结

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

打印 current/latest 标签的名称

2: VERSION_TAG=`...`

将步骤 1 的输出设置为局部变量

3: echo "##vso[task.setvariable variable=VERSION_TAG]$VERSION_TAG"

打印出一条在 Azure Devops 中设置变量的命令。在步骤 2 中设置的局部变量用作值。

对于windows vm,您可以使用下面的脚本获取标签:

steps:
- powershell: |
   $CI_BUILD_TAG = git describe --tags
   Write-Host "##vso[task.setvariable variable=CI_BUILD_TAG]$CI_BUILD_TAG"
  displayName: 'Set the tag name as an environment variable'

我的用例:在新的 git 提交标记上触发构建管道,生成与 git 提交标记同名的 IoTHub 部署清单。

经过一段时间的摆弄,我得出结论,从 build.sourceBranch 获取提交标记更简单,结果类似于:refs/tags/xxx。 可以使用以下脚本提取标签本身:

trigger:
  branches:
    include:
      - "*"
  tags:
    include:
      - "*"
steps:
- bash: |
    export RELEASE_TAG=$(echo $(build.sourceBranch) | sed -e "s/^refs\/tags\///")
    echo "##vso[task.setvariable variable=RELEASE_TAG;]$RELEASE_TAG"
  1. 重要的是在通配符标签上包含触发器以触发在新标签上构建。根据https://docs.microsoft.com/en-us/azure/devops/pipelines/repos/github?view=azure-devops&tabs=yaml#ci-triggers
  2. echo $(build.sourceBranch) -> refs/tags/2.0.3(触发构建的标签)
  3. | sed -e "s/^refs\/tags\///" -> 将 echo 的输出通过管道传输到 sed 并删除前缀 refs/tags/
  4. echo "##vso[task.setvariable variable=RELEASE_TAG;]$RELEASE_TAG" -> 根据https://docs.microsoft.com/en-us/azure/devops/pipelines/process/set-variables-scripts?view=azure-devops&tabs=bash#set-a-variable-as-secret
  5. 将标签保存到管道变量

对于那些有兴趣查看我在哪里使用 RELEASE_TAG 变量的人来说,我的其余管道:

- download: current
  artifact: deployment
  patterns: "**/deployment.json"
- task: AzureIoTEdge@2
  displayName: "Deploy to 'tags.release=commit-tag'"
  inputs:
    action: "Deploy to IoT Edge devices"
    deploymentFilePath: "$(Pipeline.Workspace)/deployment/deployment.json"
    azureSubscription: "Azure-Service-Connection"
    iothubname: "iothub"
    deploymentid: "$(System.TeamProject)-devops-$(RELEASE_TAG)-$(Build.BuildId)"
    priority: "$(Build.BuildId)"
    deviceOption: "Multiple Devices"
    targetcondition: "tags.release='$(RELEASE_TAG)'"