代碼上線流程

image.png
Pipeline
Jenkins 從根本上說是一個(gè)支持多種自動化模式的自動化引擎。Pipeline 在 Jenkins 上增加了一套強(qiáng)大的自動化工具,支持從簡單的持續(xù)集成到全面的 CD 流水線的用例
Node
node 是一個(gè)機(jī)器,它是 Jenkins 環(huán)境的一部分,并且能夠執(zhí)行 Pipeline。
同時(shí),node 代碼塊也是腳本式 Pipeline 語法的關(guān)鍵特性。
Stage
Stage 塊定義了在整個(gè) Pipeline 中執(zhí)行的概念上不同的任務(wù)子集(例如“構(gòu)建”,“測試”和“部署”階段),許多插件使用它來可視化或呈現(xiàn) Jenkins 管道狀態(tài)/進(jìn)度。
Step
一項(xiàng)任務(wù)。從根本上講,一個(gè)步驟告訴 Jenkins 在特定時(shí)間點(diǎn)(或過程中的“步驟”)要做什么。例如,使用 sh step:sh 'make' 可以執(zhí)行 make 這個(gè) shell 命令。 當(dāng)一個(gè)插件擴(kuò)展了 Pipeline DSL 時(shí),通常意味著該插件已經(jīng)實(shí)現(xiàn)了一個(gè)新的步驟。
Jenkinsfile (Declarative Pipeline)
pipeline {
agent any //在任何可用的代理上執(zhí)行這個(gè) Pipeline 或其任意 stage
stages {
stage("get code") { //定義 "get code" stage
steps {
// 執(zhí)行和 "get code" stage 相關(guān)的 step
echo "get code from scm"
}
}
stage('package') { // 定義 "package" stage
steps {
// 執(zhí)行和 "package" stage 相關(guān)的 step
echo "package code"
}
}
stage('deploy') {
steps {
echo "deploy packge to node1"
}
}
}
}
pipeline測試
1.新建item

image.png
2.輸入一個(gè)job名字

image.png
3.填寫相關(guān)參數(shù)

image.png
4.寫入代碼,點(diǎn)擊保存

image.png
5.點(diǎn)擊立即構(gòu)建

image.png
6.構(gòu)建結(jié)果

image.png
pipeline 也可以配合gitlab代碼倉庫使用
1.jenkins配置

image.png
2.gitlab配置(需配置相應(yīng)的jenkinsfie)
找到對應(yīng)的項(xiàng)目 new file
image.png
填寫代碼

image.png
Jenkinsfile
pipeline{
agent any
stages{
stage("get code"){
steps{
echo "get code"
}
}
stage("unit test"){
steps{
sh '/usr/local/sonar-scanner/bin/sonar-scanner -Dsonar.projectKey=html -Dsonar.projectName=${JOB_NAME} -Dsonar.sources=.'
}
}
stage("package"){
steps{
sh 'tar zcf /opt/web-${BUILD_ID}.tar.gz ./* --exclude=./git --exclude=jenkinsfile'
}
}
stage("deploy"){
steps{
sh 'ssh 10.0.1.7 "cd /code/www/ && mkdir web-${BUILD_ID}"'
sh 'scp /opt/web-${BUILD_ID}.tar.gz 10.0.1.7:/code/www/web-${BUILD_ID}'
sh 'ssh 10.0.1.7 "cd /code/www/web-${BUILD_ID} && tar xf web-${BUILD_ID}.tar.gz && rm -rf web-${BUILD_ID}.tar.gz"'
sh 'ssh 10.0.1.7 "cd /code/www/ && rm -rf html && ln -s web-${BUILD_ID} /code/www/html"'
}
}
}
}
3.立即構(gòu)建

image.png
構(gòu)建結(jié)果

image.png