Каков синтаксис "post" в сценарии по сравнению с декларативным конвейером? https://jenkins.io/doc/book/pipeline/syntax/#post
пост-эквивалент в сценарии сценария?
Ответ 1
Для скриптового конвейера все должно быть написано программно, и большая часть работы выполняется в блоке finally
:
Jenkinsfile
(Скриптовый конвейер):
node {
try {
stage('Test') {
sh 'echo "Fail!"; exit 1'
}
echo 'This will run only if successful'
} catch (e) {
echo 'This will run only if failed'
// Since we're catching the exception in order to report on it,
// we need to re-throw it, to ensure that the build is marked as failed
throw e
} finally {
def currentResult = currentBuild.result ?: 'SUCCESS'
if (currentResult == 'UNSTABLE') {
echo 'This will run only if the run was marked as unstable'
}
def previousResult = currentBuild.getPreviousBuild()?.result
if (previousResult != null && previousResult != currentResult) {
echo 'This will run only if the state of the Pipeline has changed'
echo 'For example, if the Pipeline was previously failing but is now successful'
}
echo 'This will always run'
}
}
https://jenkins.io/doc/pipeline/tour/running-multiple-steps/#finishing-up
Ответ 2
Вы можете изменить решение @jf2010, используя замыкания, чтобы оно выглядело немного лучше (на мой взгляд)
pipeline = {
stage('Test') {
sh 'echo "Fail!"; exit 1'
}
echo 'This will run only if successful'
}
postFailure = {
echo 'This will run only if failed'
throw e
}
postAlways = {
echo 'This will always run'
}
node{
try {
pipeline()
} catch (e) {
postFailure()
} finally {
postAlways()
}
}