Pipeline由多個步驟組成,允許您構建,測試和部署應用程序。Jenkins Pipeline允許您以簡單的方式組合多個步驟,可以幫助您建模任何類型的自動化過程。
想像一個“一步一步”,就像執(zhí)行單一動作的單一命令一樣。當一個步驟成功時,它移動到下一步。當步驟無法正確執(zhí)行時,Pipeline將失敗。
當Pipeline中的所有步驟成功完成后,Pipeline被認為已成功執(zhí)行。
在Linux,BSD和Mac OS(或其他類Unix)系統(tǒng)上,該sh步驟用于在Pipeline中執(zhí)行shell命令。
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'echo "Hello World"'
sh '''
echo "Multiline shell steps works too"
ls -lah
'''
}
}
}
}
Toggle Scripted Pipeline (Advanced)
Jenkinsfile (Scripted Pipeline)
node {
stage('Build') {
sh 'echo "Hello World"'
sh '''
echo "Multiline shell steps works too"
ls -lah
'''
}
}
基于Windows的系統(tǒng)應該使用bat
執(zhí)行批處理命令的步驟。
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Deploy') {
steps {
retry(3) {
sh './flakey-deploy.sh'
}
timeout(time: 3, unit: 'MINUTES') {
sh './health-check.sh'
}
}
}
}
}
Toggle Scripted Pipeline (Advanced)
Jenkinsfile (Scripted Pipeline)
node {
stage('Build') {
bat 'set'
}
}
有一些強大的步驟,“包裝”其他步驟,可以輕松地解決問題,如重試(retry
)步驟,直到成功或退出,如果步驟太長(timeout
)。
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Deploy') {
steps {
retry(3) {
sh './flakey-deploy.sh'
}
timeout(time: 3, unit: 'MINUTES') {
sh './health-check.sh'
}
}
}
}
}
Toggle Scripted Pipeline (Advanced)
Jenkinsfile (Scripted Pipeline)
node {
stage('Deploy') {
retry(3) {
sh './flakey-deploy.sh'
}
timeout(time: 3, unit: 'MINUTES') {
sh './health-check.sh'
}
}
}
“部署”階段重試flakey-deploy.sh腳本3次,然后等待health-check.sh腳本執(zhí)行最多3分鐘。如果健康檢查腳本在3分鐘內未完成,則Pipeline將在“部署”階段被標記為失敗。
“包裝”的步驟,例如timeout和retry可以包含其它步驟,包括timeout或retry。
我們可以組合這些步驟。例如,如果我們想重新部署我們的部署5次,但是在失敗的階段之前,總是不想花3分鐘以上:
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Deploy') {
steps {
timeout(time: 3, unit: 'MINUTES') {
retry(5) {
sh './flakey-deploy.sh'
}
}
}
}
}
}
Toggle Scripted Pipeline (Advanced)
Jenkinsfile (Scripted Pipeline)
node {
stage('Deploy') {
timeout(time: 3, unit: 'MINUTES') {
retry(5) {
sh './flakey-deploy.sh'
}
}
}
}
當Pipeline完成執(zhí)行后,您可能需要運行清理步驟或根據(jù)Pipeline的結果執(zhí)行某些操作??梢栽诒緋ost節(jié)中執(zhí)行這些操作。
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'echo "Fail!"; exit 1'
}
}
}
post {
always {
echo 'This will always run'
}
success {
echo 'This will run only if successful'
}
failure {
echo 'This will run only if failed'
}
unstable {
echo 'This will run only if the run was marked as unstable'
}
changed {
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'
}
}
}
Toggle Scripted Pipeline (Advanced)
Jenkinsfile (Scripted Pipeline)
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.previousBuild?.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'
}
}
更多建議: