KoalatyEngineer
KoalatyEngineer

Reputation: 21

Jenkins Declarative Pipeline: How to use arguments passed into function in Windows bat command?

I have a pipeline in which I am defining a function that takes string arguments and then attempting to use that function/parameters in a step that contains a bat command. I cannot for the life of me figure out how to get those string parameters exposed in the bat command.

Here is the function-

// publish step
def publish(String projectName, String publishProfile) {
    echo "${projectName}"
    echo "${publishProfile}"
    bat ''' sqlpackage.exe /Action:Publish /SourceFile:%WORKSPACE%\\%projectName%\\bin\\Debug\\%projectName%.dacpac /Profile:%WORKSPACE%\\%projectName%\\Publish\\%publishProfile% '''
}

In the console output, I can see the correct projectName and publishProfile that were passed in being echoed. %WORKSPACE% is exposed correctly in the bat command. However, %projectName% and %publishProfile% are just blank. I've tried a lot of combinations of double quotes, single quotes, etc. How do I get these exposed in the bat command?

Upvotes: 1

Views: 1920

Answers (1)

KoalatyEngineer
KoalatyEngineer

Reputation: 21

Finally resolved this. Using """ (double quotes) rather than ''' for the bat command allows for the arguments to be exposed. I then used set in the bat command to set them as variables so that they played nicely with the filepaths/file extensions.

def publish(String projectName, String publishProfile) {
    bat """ set PROJNAME=$projectName
    set PUBPROF=$publishProfile
    \"C:\\Program Files (x86)\\Microsoft SQL Server\\140\\DAC\\bin\\sqlpackage.exe\" /Action:Publish /SourceFile:%WORKSPACE%\\%PROJNAME%\\bin\\Debug\\%PROJNAME%.dacpac /Profile:%WORKSPACE%\\%PROJNAME%\\Publish\\%PUBPROF% """
}

Upvotes: 1

Related Questions