1

In a pipeline I run the following script to grab the version property from a gradle project, and then store off the major version in a variable:

// grab version property, split on '-' to remove '-SNAPSHOT', and then tokenize
def versionParts = sh(returnStdout: true, script: "./gradlew properties -q | grep 'version:' | awk '{print \$2}'").split("-")[0].tokenize(".")

// sometimes this is null??
def majorVersion = versionParts[0]

Sometimes, the majorVersion property seems to evaluate to null, but most of the time it seems to work. Are there any kind of limitations or caveats to using pipes inside the sh command like that? It's almost as if it's not running the pipes in the correct order. The strange part is that it had been working...

I know there are other ways I could use to achieve loading the version property, but what I'm more interested in is the behavior around pipes... specifically in the context of it should be avoided, or if there are special considerations that need to be made when using them.

chinabuffet
  • 5,278
  • 9
  • 40
  • 64
  • Is it possible, that's a duplicate of this one? https://stackoverflow.com/questions/42568201/jenkins-pipeline-sh-does-not-seem-to-respect-pipe-in-shell-command – hakamairi Feb 08 '19 at 07:40
  • Can you post the data which you get by running ./gradlew properties ? And you can debug it by printing the result ./gradlew propeties to rule out, the problem is not with the command – Fidel Feb 08 '19 at 08:47
  • Instead of doing all that regexp logic in the shell, you could always return the output before the pipe and then perform regexps with Groovy on the output. That would also be cleaner code. – Matthew Schuchard Feb 08 '19 at 12:55
  • @hakamairi I had seen that post, and it certainly could be, but I didn't get the sense that post really had a conclusion on the pipes behavior being the culprit or not. – chinabuffet Feb 08 '19 at 13:59
  • @Fidel If I run the gradle properties + grep command, I see: `version: 0.0.1-SNAPSHOT`, and then after the pipe to awk, I see `0.0.1-SNAPSHOT` which is what I'd expect. In the case where it resulted in null the couple times, I didn't have a println there, and haven't seen it happen again yet, so not sure there. – chinabuffet Feb 08 '19 at 14:01
  • @MattSchuchard yeah that's certainly an option, and may likely end up doing that anyways, but I was hoping to try to get to the bottom of whether or not there are strange behaviors with pipes in jenkins shell commands – chinabuffet Feb 08 '19 at 14:02
  • Understood. I see that there are two separate components here: solving the problem and understanding the behavior. – Matthew Schuchard Feb 08 '19 at 16:36
  • @MattSchuchard Yes, exactly :) I updated my question to reflect the part I'm moreso interested in is the behavior around pipes... because there are other spots in our code base also using pipes that I'm concerned about after seeing this happen. – chinabuffet Feb 08 '19 at 16:56

0 Answers0