pipeline {
agent { docker 'maven:3.3.3' } #代理,
stages { #階段。
stage('build') { #每一個階段
steps { #每一個步驟
sh 'mvn --version'
}
}
stage('test') {
steps {
sh 'echo success'
}
}
}
}
#每一個流水線工程最終都在
/var/jenkins_home/workspace/流水線工程名
#agent { docker 'maven:3.3.3' }
整個流水線工程
docker run -t -d -u 0:0 -w /var/jenkins_home/workspace/helloworld --volumes-from cd270036b30eada48bb123338864c1451ee8c7d30997a3b856c6d0174b1be2e2 -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** -e ******** maven:3.3.3 cat
整個流水線的文件夾是在maven容器內運行的
#1、多步驟。steps{ sh ``` 多命令 ``` }
#2、agent:jenkins接下來的流水線運行在哪個環境
https://www.jenkins.io/doc/book/pipeline/syntax/#agent
agent any:任意環境(立刻就能運行,不挑環境),
agent none:頂級整個流水線環境,每個階段stage,要定義自己的agent環境
agent { label 'my-defined-label' }
agent { node { label 'labelName' } } ;和agent { label 'labelName' }一個意思,
agent {
docker {
image 'maven:3-alpine'
label 'my-defined-label'
args '-v /tmp:/tmp'
}
} #指定當前流水線或者stage運行在使用docker下載來的這個環境下,用完就刪了。如果這些環境有些數據需要永久保存我們就應該掛載出來。
#3、環境變量,jenkins整個流水線過程中,我可以把經常要用的一些值,抽取為環境變量,在下面方便引用。
environment {
DISABLE_AUTH = 'true'
DB_ENGINE = 'sqlite'
}
#4、后置處理
post {
always {
junit 'build/reports/**/*.xml'
}
aborted: {
sh 'echo 666'
}
}
always:完成
changed:階段發生變化執行
fixed:
regression
Only run the steps in post if the current Pipeline’s or stage’s run’s status is failure, unstable, or aborted and the previous run was successful.
aborted:手工停止
failure
Only run the steps in post if the current Pipeline’s or stage’s run has a "failed" status, typically denoted by red in the web UI.
success
Only run the steps in post if the current Pipeline’s or stage’s run has a "success" status, typically denoted by blue or green in the web UI.
unstable
Only run the steps in post if the current Pipeline’s or stage’s run has an "unstable" status, usually caused by test failures, code violations, etc. This is typically denoted by yellow in the web UI.
unsuccessful
Only run the steps in post if the current Pipeline’s or stage’s run has not a "success" status. This is typically denoted in the web UI depending on the status previously mentioned.
cleanup
Run the steps in this post condition after every other post condition has been evaluated, regardless of the Pipeline or stage’s status.
示例:構建失敗的時候郵件通知
post {
failure {
mail to: 'team@example.com',
subject: "Failed Pipeline: ${currentBuild.fullDisplayName}",
body: "Something is wrong with ${env.BUILD_URL}"
}
}
#5、人工確認
input "Does the staging environment look ok?"
#6、總結:Pipeline語法
pipeline {
agent any
stages {
stage('第一階段') {
//
steps {
sh 'echo 111'
emailext body: '哈哈', subject: '測試', to: '22222@qq.com'
}
}
}
}
#所有不會寫的語法,全部參照 流水線工程里面的 語法生成器。
片段生成器--》可以生產step里面不會寫的內容
Declarative Directive Generator--》生成哪些指令不會用
post {
always {
// One or more steps need to be included within each condition's block.
}
aborted {
// One or more steps need to be included within each condition's block.
}
success {
// One or more steps need to be included within each condition's block.
}
failure {
// One or more steps need to be included within each condition's block.
}
}
agent {
docker {
image 'maven:3-alpine' #docker下載來的maven作為接下來的環境,容器用完就沒了
args '-v /root/.m2:/root/.m2' #mvn從網上下載jar包。下載來的東西都掛載到linux的/root/.m2
}
}
#默認使用docker的maven環境下載很慢。如何下載快的問題
#1、改配置文件。進入linux的/root/.m2。修改settings-docker.xml
#2、指定maven的配置。
2.1:先給項目里面放maven-setting.xml文件,配置好mirror,profile等。
2.2:jenkins流水線,mvn -gs maven-setting.xml
參考語法生成器
