4

I have a Jenkinsfile in which I need a property from my .csproj file (dotnet core project)

I have tried this:

def versionPrefix = sh(script: 'cat ./src/project.csproj | grep (?<=<VersionPrefix>).*(?=</VersionPrefix>)')

I know I'm probably far off, so I'm not necessarily looking for something close to this, but I am looking for a solution not requiring plugins (preferably).

arifCee
  • 509
  • 2
  • 12
kodeaben
  • 1,829
  • 3
  • 24
  • 33

3 Answers3

2

Have a look at the Groovy library called "XmlSlurper" which could be helfpul for you.

It was not allowed in Jenkins pipelines but that issue was resolved some time ago! Someone tried to do similar things here.

Hope this helps!

arifCee
  • 509
  • 2
  • 12
2

Solution without pipeline

First install the Environment Script Plugin

Then configure as the image bellow

enter image description here

Enviroment Script Plugin

In the image I get the version from the build.xml file, the "-o" arg in egrep (also works with grep) is to return only the part I want and not the entire line, the rest is pure regex to match the number version with . (dot), something like

1.3.13

Solution WITH pipeline

Just adjust the grep in the image for yout need's

def versionPrefix =  sh(script: 'grep -o (?<=<VersionPrefix>).*(?=</VersionPrefix>) ./src/project.csproj')
Joao Vitorino
  • 2,976
  • 3
  • 26
  • 55
2

I know this question has been answered, but if anyone else requires this behaviour and grep -o doesn't work, then the following command can work:

def versionPrefix = sh([returnStdout: true, script: 'sed -n "s/.*<VersionPrefix>\\([^<]*\\)<\\/VersionPrefix>.*/\\1/p" ./src/project.csproj'])

This is because - at least some - grep variants don't support lookaheads/lookbehinds.

Simon Laing
  • 1,194
  • 7
  • 18