从 azure-pipelines 中的 pom.xml 读取 yaml 文件中的项目版本

Read version of project in yaml file from pom.xml in azure-pipelines

我有一个 pom.xml 文件,其中包含我这样的项目版本

 <version> 1.14.0 </version>

我还有一个 YAML 文件,它会在测试通过后自动生成一个 GitHub 标签,就像这样

- job: createTag
    dependsOn: ifBranchIsMaster
    condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/master')
    steps:
      - task: GitHubRelease@0
        displayName: ‘Create GitHub Release’
        inputs:
          gitHubConnection: $(GITHUB_CONNECTION)
          repositoryName: $(GITHUB_REPO)
          action: create
          tag: 1.14.0

我想从我的 YAML 文件中删除硬编码版本标签并立即从 pom.xml 中读取它有什么方法可以发生我尝试将硬编码版本最小化为 1。我想一处改,到处改。

您可以创建从 pom.xml 文件读取变量并设置管道变量的 PowerShell 脚本。在 tag: 中使用此变量。

例如:

$filePath = "path/to/pom.xml"
$version = (Select-String -Path $filePath -Pattern "<version>").Line
$version = $version.Split(" ")[1]
Write-Host "##vso[task.setvariable variable=version]$version"

读取版本的另一个选项是:

[xml]$pomXml = Get-Content $filePath
$version = $pomXml.project.version

GitHubRelease@ 任务中使用变量:

tag: $(version)

所以我想出了一个脚本来解决我的问题并读取所有 pom.xml

中的 <version>1.14.1</version>

那是一个 powershell 脚本

[xml]$pomXml = Get-Content .\pom.xml
# version
Write-Host $pomXml.project.version
$version=$pomXml.project.version
Write-Host "##vso[task.setvariable variable=version]$version"

而且我会提供 bash 脚本以防有人需要它

#!/usr/bin/env bash
version=$(grep version pom.xml | grep -v '<?xml' | grep '<version>'|head -n 1|awk '{print }'| cut -d'>' -f 2 | cut -d'<' -f 1)
echo "##vso[task.setvariable variable=version]$version"

这就是我发现可以从 pom.xml

获取版本的方法