Declarative Pipeline
jenkinsfile(declarative pipeline)
pipeline{
agent any
stages{
stage('Build'){
steps{
}
}
stage('Test'){
steps{
}
}
stage('Deploy'){
steps{
}
}
}
}
Scripted Pipeline
腳本化的pipeline是基于groovy的一種DSL語言,可去此處學習:https://www.w3cschool.cn/groovy/index.html
node('node_name'){
stage('stage_name'){
//清除工作空間
cleanWs()
//拉取代碼
checkout([$class: 'GitSCM',
branches: [[name: '*/master']],
userRemoteConfigs: [[url: 'http://git-server/user/repository.git']]])
//進入指定路徑完成后續(xù)的操作
dir('path'){
//執(zhí)行單條shell指令
sh 'xxxxxx'
//執(zhí)行多條shell指令
sh ```#!/bin/bash
echo "hello"
rm -f xxx
```
}
}
}
腳本化pipeline是串行執(zhí)行的
Jenkinsfile (Scripted Pipeline)
node {
stage('Example') {
if (env.BRANCH_NAME == 'master') {
echo 'I only execute on the master branch'
} else {
echo 'I execute elsewhere'
}
}
}
異常捕捉
Jenkinsfile (Scripted Pipeline)
node {
stage('Example') {
try {
sh 'exit 1'
}
catch (exc) {
echo 'Something failed, I should sound the klaxons!'
throw
}
}
}
二、如何快速編寫pipeline
在創(chuàng)建的pipeline job中,選擇Pipeline Syntax

image.png