Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to strip some form of new line character at end of parsed Jenkinsfile variable

I am wanting to pass variables between stages in a Jenkinsfile and am doing it as people suggest, by writing to a file then parsing it.

stage('1') {
  steps {
    sh "echo value > var.txt"
    script {
      VALUE = readFile('var.txt')
    }
  }
}

stage('2') {
  steps {
    sh "echo ${VALUE} && echo 'ok'"
  }
}

However, I am getting some form of new line character at the end of ${VALUE} that means I am trying to execute 2 lines and the && is causing the stage to barf. I've tried piping through tr -d '\r\n'but it doesn't seem to do anything.

like image 463
John Avatar asked May 23 '17 20:05

John


People also ask

How do you pass parameters in Jenkins pipeline?

Any Jenkins job or pipeline can be parameterized. All we need to do is check the box on the General settings tab, “This project is parameterized”: Then we click the Add Parameter button.

What does sh do in Jenkinsfile?

The sh step assumes the system is Unix/Linux-based, for Windows-based systems the bat could be used instead. The sh step invokes the make command and will only continue if a zero exit code is returned by the command. Any non-zero exit code will fail the Pipeline.

What is Bat command in Jenkins Pipeline?

Pipeline: Nodes and Processes. bat : Windows Batch Script. node : Allocate node. powershell : Windows PowerShell Script. pwsh : PowerShell Core Script.

What can you do in a Jenkins pipeline to pause execution and wait for user feedback?

input : Wait for interactive input. This step pauses Pipeline execution and allows the user to interact and control the flow of the build. Only a basic "proceed" or "abort" option is provided in the stage view. You can optionally request information back, hence the name of the step.


1 Answers

I ran into this problem as well for my Jenkins file that is a groovy script, and this is what helped me get rid of the EOL / CR characters at the end of the value I wanted to use:

VALUE = readFile('var.txt').trim()
like image 84
Chris Santen Avatar answered Oct 21 '22 17:10

Chris Santen