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

3

6

I have a pom.xml file that includes my project version like this

 <version> 1.14.0 </version>

and I also have a YAML file that autogenerates a GitHub tag when the tests have passed and it's like this

- 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

and I want to remove from my YAML file the hard-coded version tag and read it from pom.xml immediately is there any way that can happen I try to minimize the hard-coded version to 1. I want to change it in 1 place and change everywhere.

Psia answered 6/5, 2019 at 8:42 Comment(1)
There is no reference in the YAML specification to autogeneration. It might be that azure-pipelines implements something like autogeneration based on the data it loads from the YAML document, by interpreting its scalars, but describing that as "a YAML file that autogenerates" is as incorrect as writing "an ASCII file that generates"Hymnist
P
6

so I figured out a script that solves my problem and reads the <version>1.14.1</version> inside all the pom.xml

thats a powershell script

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

and also i will provide and bash script in case of anyone want it

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

that's the ways I find that I can get the version from the pom.xml

Psia answered 8/5, 2019 at 11:4 Comment(0)
M
2

You can create a PowerShell script that read the variable from the pom.xml file and set a pipeline variable. in the tag: use this variable.

For example:

$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"

Another option to read the version is:

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

In the GitHubRelease@ task use the variable:

tag: $(version)
Muricate answered 6/5, 2019 at 11:52 Comment(7)
hi again I don't seem to get any good result with this I always get an error on Select-String and never my version any help? i added them to a file and read it afterwordsPsia
the $filePath is correct? can you run Write-Host $filePath and check the result?Muricate
okay thank you very much but the problem is i get back nothing from the script also i am in ubuntu machine and cant try it live :/ and the filePath is also correct cause i managed to print the 1st line of my pom.xml filePsia
what is the result of the first line? can you share it?Muricate
it just pom.xml i found a better script to do it ` [xml]$pomXml = Get-Content .\pom.xml # version Write-Host $pomXml.project.version $version=$pomXml.project.version Write-Host "##vso[task.setvariable variable=version]$version"` that worked preety wellPsia
Great! you are right, because it's xml you can search the version in this way. so it works good? you can assign the github tag with the version?Muricate
yes it works like charm i will post an answer so people can see if they come herePsia
M
0

This gets the version without -SNAPSHOT and saves it in the variable versionFromPom:

steps:
  - bash: echo "##vso[task.setvariable variable=versionFromPom]$(cat ./pom.xml | grep -m 1 "<version>" | grep -Po '<version>\K\d{1,}\.\d{1,}\.\d{1,}')" 
Mohandis answered 11/10, 2024 at 9:22 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.