Last active
February 29, 2020 05:54
-
-
Save tknerr/3b4f21001ed8b5b81f84d6fdcd934694 to your computer and use it in GitHub Desktop.
Debugging Powershell Sequential / Parallel Workflows and Error Reporting in Jenkinsfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def powershell_workflow(String mode, String... scripts) { | |
powershell """ | |
echo "starting workflow in $mode mode" | |
workflow runWorkflow { | |
$mode { | |
${scripts.collect { script -> | |
"InlineScript { ${script} *>&1 | tee -filepath ${script.replace(' ','_')}.log; if(\$LastExitCode -ne 0) { Throw \"ERROR: \'$script\' failed with exit code \$LastExitCode \" } } -ErrorAction Continue" | |
}.join('\n') } | |
} | |
} | |
runWorkflow | |
echo "done with workflow in $mode mode" | |
""" | |
} | |
node('Windows') { | |
stage('create ps1 scripts') { | |
//powershell "echo 'cmd /c \"exit 42\"; cmd /c \"exit 0\";' > shouldfailtoobutwont.ps1" | |
powershell "echo 'sleep 1; echo failtoo.ps1; cmd /c \"exit 42\"' > failtoo.ps1" | |
powershell "echo 'sleep 3; echo oktoo.ps1; cmd /c \"exit 0\"' > oktoo.ps1" | |
powershell "echo 'sleep 1; echo fail.ps1; sleep 1; exit 8' > fail.ps1" | |
powershell "echo 'sleep 6; echo ok.ps1; exit 0' > ok.ps1" | |
powershell "echo 'sleep 2; echo okwithstderr.ps1; write-error doh; exit 0' > okwithstderr.ps1" | |
} | |
stage('test sequential') { | |
powershell_workflow('sequence', | |
"echo exit0; exit 0", | |
//"C:/jenkins/workspace/xxx/fail.ps1", | |
"C:/jenkins/workspace/xxx/ok.ps1", | |
"C:/jenkins/workspace/xxx/oktoo.ps1", | |
"C:/jenkins/workspace/xxx/okwithstderr.ps1" | |
) | |
} | |
stage('test parallel') { | |
powershell_workflow('parallel', | |
"echo exit0; exit 0", | |
//"C:/jenkins/workspace/xxx/fail.ps1", | |
"C:/jenkins/workspace/xxx/ok.ps1", | |
"C:/jenkins/workspace/xxx/oktoo.ps1", | |
"C:/jenkins/workspace/xxx/okwithstderr.ps1" | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment