0

Here is a chunk of code I have in a pipeline:

def doBuild(folder, script, scriptArguments = '') {
   bat '''cd folder
          call script scriptArgument'''
}

So basically, it is a windows command saying: move to this directory, and call this script.

Of course, during execution, the command will be executed as 'cd folder' and will fail.

How can I make sure the 'folder' is replaced by the value passed in argument ?

Edit:

Following the suggestion of Vitalii, I tried the following code:

/* Environment Properties */
repository_name = 'toto'
extra_repository_branch = 'master'
branch_to_build = "${env.BRANCH_NAME}"
version = "${branch_to_build}_v1.5.4.${env.BUILD_NUMBER}"

/* Methods definitions properties */
def doTag() {
    dir(repository_name) {
        String tagCommand = """git tag -m "Release tag ${env.BUILD_URL}" ${version}"""
        String pushCommand = "git push --tags origin ${branch_to_build}"

        bat '''
            call $tagCommand
            call $pushCommand'''
    }
}

Here is the output:

C:\Jenkins\toto>call $tagCommand '$tagCommand' is not recognized as an internal or external command, operable program or batch file.

C:\Jenkins\toto>call $pushCommand '$pushCommand' is not recognized as an internal or external command, operable program or batch file.

Thanks very much for your time

Milan
  • 1,547
  • 1
  • 24
  • 47
  • Possible duplicate of [What's the difference of strings within single or double quotes in groovy?](https://stackoverflow.com/questions/6761498/whats-the-difference-of-strings-within-single-or-double-quotes-in-groovy) – mkobit Apr 17 '18 at 00:07

2 Answers2

2

You can use string interpolation

bat """cd $folder
     call $script $scriptArgument"""
Vitalii Vitrenko
  • 9,763
  • 4
  • 43
  • 62
  • Thank you, now maybe you can answer a question I had for a moment, what is the clear difference between $foo and ${foo} ? Thanks – Milan Apr 17 '18 at 12:49
  • They are almost the same except second one allows dotted expressions. For example `"${foo.split(',')}"` – Vitalii Vitrenko Apr 17 '18 at 14:56
  • In the case of my script, ${folder} was read literraly and the command sent to windows was 'cd ${folder}' Is there a reason why $folder will work while ${folder} won't ? – Milan Apr 17 '18 at 15:43
  • Hi Vitalii, unfortunately it is not working, I have edited my original post, may you have a look ? – Milan Apr 17 '18 at 15:56
1

So I did not think it was the case first but it was really had its answer in What's the difference of strings within single or double quotes in groovy?

Using single quotes, the text is considered as literrate. Using double code, it will interpret correctly the $tagCommand. A working version is:

/* Methods definitions properties */

def doTag() {
    dir(repository_name) {
        String tagCommand = """git tag -m "Release tag ${env.BUILD_URL}" ${version}"""
        String pushCommand = "git push --tags origin ${branch_to_build}"

        bat """call $tagCommand
            call $pushCommand"""
    }
}
Milan
  • 1,547
  • 1
  • 24
  • 47