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.

Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
Theodosis
  • 792
  • 2
  • 7
  • 22
  • 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" – Anthon May 06 '19 at 10:19

2 Answers2

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

Theodosis
  • 792
  • 2
  • 7
  • 22
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)
Shayki Abramczyk
  • 36,824
  • 16
  • 89
  • 114
  • 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 afterwords – Theodosis May 07 '19 at 07:54
  • the `$filePath` is correct? can you run `Write-Host $filePath` and check the result? – Shayki Abramczyk May 07 '19 at 08:28
  • 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 file – Theodosis May 07 '19 at 09:11
  • what is the result of the first line? can you share it? – Shayki Abramczyk May 07 '19 at 09:43
  • 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 well – Theodosis May 07 '19 at 10:35
  • 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? – Shayki Abramczyk May 07 '19 at 10:45
  • yes it works like charm i will post an answer so people can see if they come here – Theodosis May 08 '19 at 11:02