Skip to content

Instantly share code, notes, and snippets.

@tknerr
Last active February 29, 2020 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tknerr/3b4f21001ed8b5b81f84d6fdcd934694 to your computer and use it in GitHub Desktop.
Save tknerr/3b4f21001ed8b5b81f84d6fdcd934694 to your computer and use it in GitHub Desktop.
Debugging Powershell Sequential / Parallel Workflows and Error Reporting in Jenkinsfile
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