Declarative script: 'bat' command fails but job succeeds

5,129 views
Skip to first unread message

David Aldrich

unread,
Oct 18, 2017, 5:45:29 AM10/18/17
to jenkins...@googlegroups.com

Hi

 

I have a declarative script that runs a ‘bat’ step. That step builds some VS Studio projects on a Windows slave by calling msbuild:

 

            steps {

                bat '''

                    call "%VSMSBUILDCMD%"

 

                    msbuild %WORKSPACE%\\MSVC\\<proj> /p:Configuration=config_0 /p:Platform="Win32" /flp:logfile=Output.log /verbosity:normal

 

                    msbuild %WORKSPACE%\\MSVC\\<proj> /p:Configuration=config_1 /p:Platform="Win32" /flp:logfile=Output.log /verbosity:normal

 

                    msbuild %WORKSPACE%\\MSVC\\<proj> /p:Configuration=config_2 /p:Platform="Win32" /flp:logfile=Output.log /verbosity:normal

 

'''

}

 

The second msbuild command is failing (due to missing header files) and reports ‘Build Failed’, but the script step succeeds and the post stage executes the ‘success’ code.

 

Any idea why the step is not failing please?

 

Best regards

 

David

 

David Aldrich

unread,
Oct 18, 2017, 7:04:30 AM10/18/17
to jenkins...@googlegroups.com

A follow-up to this:

 

I ran the failing msbuild command from the command line (in the workspace) and checked the errorlevel, it was 1.

 

I then replayed the declarative script with some extra code:

 

msbuild %WORKSPACE%\\MSVC\\<proj> /p:Configuration=config_2 /p:Platform="Win32" /flp:logfile=ZodiacOutput.log /verbosity:normal

if errorlevel 1 (

   echo Failure Reason Given is %errorlevel%

   exit /b %errorlevel%

)

 

The Jenkins output was then:

 

Failure Reason Given is 1

Post stage

[Pipeline] script

[Pipeline] {

<snip>

[Pipeline] End of Pipeline

ERROR: script returned exit code 1

Finished: FAILURE

 

So I had to explicitly check the errorlevel to detect the failure. Does this mean there is this a bug in the declarative script plugin?

 

Best regards

 

David

Click here to report this email as spam.

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/b1500615c71c4a48b48714a3a1aa3706%40EUX13SRV1.EU.NEC.COM.
For more options, visit https://groups.google.com/d/optout.

itchymuzzle

unread,
Oct 18, 2017, 9:24:28 AM10/18/17
to Jenkins Users
Where is "script" in the original example?

itchymuzzle

unread,
Oct 18, 2017, 9:25:30 AM10/18/17
to Jenkins Users
>    bat '''

What type of quote(s) is that?  Three single quotes?

David Aldrich

unread,
Oct 18, 2017, 9:35:03 AM10/18/17
to jenkins...@googlegroups.com

Here’s a better view of my script:

 

pipeline {

 

    agent none

    options { buildDiscarder(logRotator(numToKeepStr: '20')) }

    triggers {

        pollSCM('H/5 * * * *')

    }

 

    stages {

 

        stage('build_gcc') {

            <snip - this stage runs on a Linux node>

        }

 

        stage('build_VisualStudio') {

 

            agent { label "jenkinswin10" }

 

            environment {

                VSMSBUILDCMD = 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Tools\\VsMSBuildCmd.bat'

            }

 

            steps {

                bat '''

                    call "%VSMSBUILDCMD%"

                    msbuild %WORKSPACE%\\MSVC\\myproj.sln /p:Configuration=Release /p:Platform="Win32" /flp:logfile=ZodiacOutput.log /verbosity:normal

                '''

            }

 

            post {

                <snip>

            }

        }

    }

}

 

itchymuzzle

unread,
Oct 18, 2017, 9:40:59 AM10/18/17
to Jenkins Users
So you don't mean "script", you mean using "bat" as you would "sh".   Is that right?

stage('Build') {
steps {
script {

David Aldrich

unread,
Oct 18, 2017, 9:46:05 AM10/18/17
to jenkins...@googlegroups.com

Yes, that’s right. Sorry for using wrong term.

jer...@bodycad.com

unread,
Oct 18, 2017, 2:21:29 PM10/18/17
to Jenkins Users
Take care batch error level only is valid for the last command and not a cumulative flag. So every msbuild command you run should check the error level result and exit batch according to it with proper error code. This will make the bat command failed properly. I personnaly would suggest, you run run each msbuild command into separated bat command so you will known which one failed, but that's totally up to you. Also make sure you give the proper  bat argument returnStatus into your pipeline scripts.

David Aldrich

unread,
Oct 19, 2017, 4:10:41 AM10/19/17
to jenkins...@googlegroups.com
Thanks very much for your reply.

> Take care batch error level only is valid for the last command and not a cumulative flag.

Yes, that was exactly the issue. I followed your advice and the script works correctly now.

> Also make sure you give the proper  bat argument returnStatus into your pipeline scripts.

I partly understand this. Where should I specify the returnStatus argument? (Note this is declarative).

jer...@bodycad.com

unread,
Oct 19, 2017, 10:36:08 AM10/19/17
to Jenkins Users
Not sure about declarative pipeline (I haven't played with them yet). but for normal pipeline I use it this way:
bat returnStatus: false, script: "\"${bcad.msbuild_current}\" ${bcad.msbuild_solution_name} ${bcad.msbuild_default_arg} /t:Build"

You can find the doc here:

returnStatus can be check or not if you want, if you don't want to end the script for warnings like my above example. You can then use msbuild parser for example
step([$class: 'WarningsPublisher', canRunOnFailed: true, consoleParsers: [[parserName: 'MSBuild']]])

David Aldrich

unread,
Oct 19, 2017, 10:57:41 AM10/19/17
to jenkins...@googlegroups.com

Thanks for your answer.

 

--

You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.


For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages