Milan
Milan

Reputation: 1760

Jenkins function definition: call a batch file using function arguments

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

Upvotes: 0

Views: 2118

Answers (2)

Vitalii Vitrenko
Vitalii Vitrenko

Reputation: 10435

You can use string interpolation

bat """cd $folder
     call $script $scriptArgument"""

Upvotes: 2

Milan
Milan

Reputation: 1760

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"""
    }
}

Upvotes: 1

Related Questions