How can I turn the output of a git diff into a string and assign it to a variable in Jenkinsfile?

My Jenkinsfile pipeline currently looks like this:


    stage('Testing') {
        sh '$(git diff HEAD^ requirements.txt) > requirements_change.txt' 
        sh 'cat requirements_change.txt' 

       script { 
           some_variable = 'requirements_change.txt'

When I build the above pipeline in Jenkins, the line sh 'cat requirements_change.txt' outputs all of the git diffs of the requirements.txt file as expected.

Is it possible to turn all of these git diffs into one long string?
My intention is to assign this string to a variable that then gets passed on.

The issue with the code written above is that some_variable points to the path/file of requirements_change.txt, but what I need instead is the contents of the text file.

no need to use cat you can use the readFile step
some_variable = readFile encoding: 'UTF-8', file: 'requirements_change.txt'

Thanks for the quick response! I’ll give it a try. Been stuck on this for the longest time

@mawinter69 is there a way to verify after the line above that some_variable is a string? Because I suspect it is not due to the error I get when passing along the varaible.

try echo some_variable
The echo step accepts strings only. It might be a GString though. What error do you get?

Thanks I’ll try echoing it.

It’s a complex error, I am passing some_variable into a python argparse, which only accepts strings as a parameter, otherwise it fails.

The error will be quite meaningless I suspect, but for reference this is what I get:

WORKSPACE=/home/ec2-user/jenkins/workspace/xxx_PR-4321 -e GITHUB_TOKEN=**** -e JIRA_SERVER= -e JIRA_USERNAME=**** -e JIRA_PASSWORD=**** -e BLACKDUCK_TOKEN=**** --workdir=/home/ec2-user/jenkins/workspace/xxx_PR-4321 --entrypoint=/home/ec2-user/jenkins/workspace/

[2024-02-05T09:42:04.796Z] standard_init_linux.go:228: exec user process caused: exec format error

script returned exit code 1

so you will probably pass the output as an env variable I assume.
Consider that the string is multiline most likely

Ahh I see - any suggestion on how to do that?

Here is the actual code:

script {

    def jira_raise_project = 'TEST'
    def jira_raise_title = 'Updates requirements for Js deployment'  // DO NOT change title, Jira ticket status relies on this
    def jira_raise_packages = 'js,bmll-dbutils,js-meta'
    def entrypoint = "${env.WORKSPACE}/"
    def some_variable = readFile encoding: 'UTF-8', file: 'requirements_change.txt'

    writeFile file: entrypoint, text: """\
      #!/usr/bin/env bash
      set -ex
      #pip install bl-release-tools  
      pushd $WORKSPACE/bl-release-tools
      pip install -e .
      bl create-jira-ticket '$jira_raise_project' '$jira_raise_title' '$jira_raise_packages' '$some_variable'

You are running into shell-quoting issues most probably.
The bl script needs adoption to read such complex input from file or stdin, not as an commandline arg.
If you insist on command line arg, (but remember that the lenght there is limited), bl could expect the diff to be passed as base64-encoded.

Thanks @bpedersen2 is it possible to turn the contents of the file into a string? If so I could assign it to a variable and pass a string along instead.

I’ve tried converting it into a string, but with no luck. Any suggestions on how I could do this?

It is not argparse that is failing, it is the shell parser that read it differently from what you expect. You would need to fully quote the contents of your string ( all shell meta chars, newlines and especially quote characters) , but that will soon become very tricky.