0

i am trying to do git checkout in a jenkins pipeline and trying to find out only the addition of the lines in a pariticular file. however it is throing me error at the dollar sign on the dynamic branch name but the same thing worked elsewhere in the pipeline. the only new addition to the code is the git diff line to find only the addition of the new lines in a particular file. Can you help me to acheive that.

working code:

script{
    withCreentials(...){
    
        sh returnStdout: true, script: """
            gwit checkout -B ${env.branch}
            git config user.name 'xxx'
            git config user.email 'xxx@xxx.com'
            git add sample.txt
            git commit -m "updated from pipeline"
            git push origin HEAD:${env.branch}
        
        """
    }

}

not working code:

script{
    withCreentials(...){
        sh returnStdout: true, script: """
            git checkout -B ${env.branch}
            git config user.name 'xxx'
            git config user.email 'xxx@xxx.com'
            git diff HEAD^:sample.txt HEAD:sample.txt --color=always|perl -wine 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/'> output.txt
        
    }

}

error is :

illegal string boy character
solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" 
    git  checkout -B ${env.branch}
                                 ^
Reese
  • 389
  • 2
  • 10
  • 26
  • You could start by separating your multiline command into several single-line commands and see what line is causing the issue. – MaratC Dec 06 '20 at 12:42
  • If i remove the git diff command as mentioned above.. everything running fine.. but when i add it the error points to the checkout line.. which is why it is being difficult to understand the issue – Reese Dec 06 '20 at 13:08
  • Your line has `print $1` and the error says `either escape a literal dollar sign "\$5" or bracket the value expression "${5}"`. Have you tried fixing as the error message is suggesting? – MaratC Dec 06 '20 at 13:50
  • Tried..no luck..but pretty sure the issue is with escaping in the git diff command... which till now i couldn't sort out – Reese Dec 07 '20 at 05:28

1 Answers1

1

This is a really common error, basically when using Triple-Double-Quote you need to scape all the $ except those you want to expand as part of variable interpolation.

However Groovy prints the error in one of the first occurrence of $ instead of the actual place where you have the error

In your case the line with problems is

git diff HEAD^:sample.txt HEAD:sample.txt --color=always|perl -wine 'print $1 if /^\e\[32m\+\e\[m\e\[32m(.*)\e\[m$/'> output.txt

You have $1 and [m$. The solution will be to escape those.

In the other hand you will need to escape any backlash that is going to be required by the script

Official documentation http://docs.groovy-lang.org/latest/html/documentation/index.html#all-strings

jcmendez
  • 81
  • 5