DevOps-Jenkins Pipline简介、基于pipline的代码部署及历史构建记录配置
pipeline官网文档:https://www.jenkins.io/zh/doc/book/pipeline/
声明式和脚本化的流水线语法
Jenkinsfile 能使用两种语法进行编写 - 声明式和脚本化。
声明式和脚本化的流水线从根本上是不同的。 声明式流水线的是 Jenkins 流水线更近的特性:
-
相比脚本化的流水线语法,它提供更丰富的语法特性,
-
是为了使编写和读取流水线代码更容易而设计的。
pipline 优势:
可持续性:jenkins 的重启或者中断后不影响已经执行的 Pipline Job;
支持暂停:pipline 可以选择停止并等待人工输入或批准后再继续执行;
可扩展:通过 groovy 的编程更容易的扩展插件;
并行执行:通过 groovy 脚本可以实现 step,stage 间的并行执行,和更复杂的相互依赖关系。
声明式流水线基础
Jenkinsfile (Declarative Pipeline) pipeline { agent any 1 stages { stage('Build') { 2 steps { // 3 } } stage('Test') { 4 steps { // 5 } } stage('Deploy') { 6 steps { // 7 } } } }
1. 在任何可用的代理上,执行流水线或它的任何阶段。
2. 定义 "Build" 阶段。
3. 执行与 "Build" 阶段相关的步骤。
4. 定义"Test" 阶段。
5. 执行与"Test" 阶段相关的步骤。
6. 定义 "Deploy" 阶段。
7. 执行与 "Deploy" 阶段相关的步骤。
流水线示例
Jenkinsfile (Declarative Pipeline) pipeline { 1 agent any 2 stages { stage('Build') { 3 steps { 4 sh 'make' 5 } } stage('Test'){ steps { sh 'make check' 5 junit 'reports/**/*.xml' 6 } } stage('Deploy') { steps { sh 'make publish' } } } }
1. pipeline 是声明式流水线的一种特定语法,他定义了包含执行整个流水线的所有内容和指令的 "block" 。
2. agent是声明式流水线的一种特定语法,它指示 Jenkins 为整个流水线分配一个执行器 (在节点上)和工作区。
3. stage 是一个描述 stage of this Pipeline的语法块。在 Pipeline syntax 页面阅读更多有关声明式流水线语法的`stage`块的信息。如 above所述, 在脚本化流水线语法中,stage 块是可选的。
4. steps 是声明式流水线的一种特定语法,它描述了在这个 stage 中要运行的步骤。
5. sh 是一个执行给定的shell命令的流水线 step (由 Pipeline: Nodes and Processes plugin提供) 。
6. junit 是另一个聚合测试报告的流水线 step (由 JUnit plugin提供)。
7. node 是脚本化流水线的一种特定语法,它指示 Jenkins 在任何可用的代理/节点上执行流水线 (和包含在其中的任何阶段)这实际上等效于 声明式流水线特定语法的`agent`。
脚本化流水线基础
Jenkinsfile (Scripted Pipeline) node { 1 stage('Build') { 2 // 3 } stage('Test') { 4 // 5 } stage('Deploy') { 6 // 7 } }
1. 在任何可用的代理上,执行流水线或它的任何阶段。
2. 定义 "Build" 阶段。 stage 块 在脚本化流水线语法中是可选的。 然而, 在脚本化流水线中实现 stage 块 ,可以清楚的显示Jenkins UI中的每个 stage 的任务子集。
3. 执行与 "Build" 阶段相关的步骤。
4. 定义 "Test" 阶段。
5. 执行与 "Test" 阶段相关的步骤。
6. 定义 "Deploy" 阶段。
7. 执行与 "Deploy" 阶段相关的步骤。
创建 pipline jo
可以使用流水线语法在线生成语法
node('jenkins-slave-2') { //绑定jenkins-slave-2节点构建,需要设置slave节点,只允许绑定到这个节点 stage("clone 代码"){ sh 'cd /var/lib/jenkins/workspace/linux39-web1-develop-pipeline ; rm -rf ./* ' git branch: 'develop', credentialsId: '8e6f2919-6af2-43a0-b719-525cb82c72e7', url: 'git@159.138.5.152:linux39/web1.git' } stage("代码打包"){ sh 'cd /var/lib/jenkins/workspace/linux39-web1-develop-pipeline ; tar -czvf myapp.tar.gz ./*' } stage("代码构建"){ sh 'cd /var/lib/jenkins/workspace/linux39-web1-develop-pipeline ; scp myapp.tar.gz www@web2:/data/tomcat/tomcat_appdir' } stage("停止服务"){ sh 'ssh www@web2 "bash /opt/apps/tomcat.sh stop"' } stage("代码部署"){ sh 'ssh www@web2 "cd /data/tomcat/tomcat_appdir ; rm -rf /data/tomcat/tomcat_webapps/myapp/* ; tar -xvf /data/tomcat/tomcat_appdir/myapp.tar.gz -C /data/tomcat/tomcat_webapps/myapp"' } stage("启动服务"){ sh 'ssh www@web2 "bash /opt/apps/tomcat.sh start"' } }
控制台打印日志
Started by user jenkinsadmin [Pipeline] Start of Pipeline [Pipeline] node Running on jenkins-slave-2 in /var/lib/jenkins/workspace/linux39-web1-develop-pipeline //可以看到被调度到slave-2节点构建 [Pipeline] { [Pipeline] stage [Pipeline] { (clone 代码) [Pipeline] sh + cd /var/lib/jenkins/workspace/linux39-web1-develop-pipeline + rm -rf './*' [Pipeline] git The recommended git tool is: NONE using credential 8e6f2919-6af2-43a0-b719-525cb82c72e7 Fetching changes from the remote Git repository Checking out Revision d2a57fa9f9210b39c73ac471ae5b7153d68c9cec (refs/remotes/origin/develop) Commit message: "Update index.html" > git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/linux39-web1-develop-pipeline/.git # timeout=10 > git config remote.origin.url git@159.138.5.152:linux39/web1.git # timeout=10 Fetching upstream changes from git@159.138.5.152:linux39/web1.git > git --version # timeout=10 > git --version # 'git version 1.8.3.1' using GIT_SSH to set credentials for gitlab rsa > git fetch --tags --progress git@159.138.5.152:linux39/web1.git +refs/heads/*:refs/remotes/origin/* # timeout=10 > git rev-parse refs/remotes/origin/develop^{commit} # timeout=10 > git config core.sparsecheckout # timeout=10 > git checkout -f d2a57fa9f9210b39c73ac471ae5b7153d68c9cec # timeout=10 > git branch -a -v --no-abbrev # timeout=10 > git branch -D develop # timeout=10 > git checkout -b develop d2a57fa9f9210b39c73ac471ae5b7153d68c9cec # timeout=10 > git rev-list --no-walk d2a57fa9f9210b39c73ac471ae5b7153d68c9cec # timeout=10 [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (代码打包) [Pipeline] sh + cd /var/lib/jenkins/workspace/linux39-web1-develop-pipeline + tar -czvf myapp.tar.gz ./index.html ./index.html [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (代码构建) [Pipeline] sh + cd /var/lib/jenkins/workspace/linux39-web1-develop-pipeline + scp myapp.tar.gz www@web2:/data/tomcat/tomcat_appdir [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (停止服务) [Pipeline] sh + ssh www@web2 'bash /opt/apps/tomcat.sh stop' 正在判断服务状态,请稍等3秒钟! 3 2 1 Tomcat 没有运行 1 [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (代码部署) [Pipeline] sh + ssh www@web2 'cd /data/tomcat/tomcat_appdir ; rm -rf /data/tomcat/tomcat_webapps/myapp/* ; tar -xvf /data/tomcat/tomcat_appdir/myapp.tar.gz -C /data/tomcat/tomcat_webapps/myapp' ./index.html [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (启动服务) [Pipeline] sh + ssh www@web2 'bash /opt/apps/tomcat.sh start' 正在判断服务状态,请稍等! 请稍等3秒钟 3 2 1 Tomcat没有运行,1秒后启动! 1 Tomcat started. Tomcat 已经成功启动完成,5秒后判断是否启动成功 5 4 3 2 1 Tomcat 已经成功启动1 个Tomcat进程!,PID为1851 [Pipeline] } [Pipeline] // stage [Pipeline] } [Pipeline] // node [Pipeline] End of Pipeline Finished: SUCCESS
测试访问
[root@jenkins-slave-1 linux39-web1-develop-pipeline]# curl http://web2:8080/myapp/index.htmlthis is linux39 test page v1
this is linux39 test page v2
this is linux39 test page v3
//可以看到已经升级到v3版本
后续更新k8s环境运行jenkins