Jenkins-Credentials

如果要是把它理解为“钥匙”,那就不需要我进行过多的解释了,我们以git(版本控制器)为例来进行说明,当我们在访问gitlab时,其实gitlab是需要我们提供相应的账号与密码进行登录的,假如说我们把要访问的URL地址理解为锁,那么所提供的账号与密码就对应于开这把锁的钥匙,所以说“Credentials”中所记录的就是各种各样的这种钥匙,而这里的钥匙对应的锁是有多种可能的,可能是git,也可能是SVN等,而“Credentials”就是对于这些钥匙所进行的统一管理
image.png

1.添加Credentials

image.png

image.png

image.png
[root@jenkins-server1 ~]# cat /root/.ssh/id_rsa

image.png

Jenkins-Pipeline流水线

jenkins的pipeline功能,如今被一线企业热衷,受用率飙升迅速;
Jenkins Pipeline 的核心概念
Pipeline,简而言之,就是一套运行于Jenkins上的工作流框架,将原本独立运行于单个或者多个节点的任务连接起来,实现单个任务难以完成的复杂流程编排与可视化。
Pipeline是Jenkins2.X的最核心的特性,帮助Jenkins实现从CI到CD与DevOps的转变
Pipeline是一组插件,让Jenkins可以实现持续交付管道的落地和实施。
持续交付管道(CD Pipeline)是将软件从版本控制阶段到交付给用户或客户的完整过程的自动化表现。软件的每一次更改(提交到源代码管理系统)都要经过一个复杂的过程才能被发布。
Pipeline提供了一组可扩展的工具,通过Pipeline Domain Specific Language(DSL)syntax可以达到Pipeline as Code(Jenkinsfile存储在项目的源代码库)的目的。

Jenlins Pipeline的基础语法

Pipeline脚本是由Groovy语言实现(无需专门学习)
支持两种语法
Declarative 声明式(在Pipeline plugin 2.5中引入)
Scripted Pipeline 脚本式

一、声明式(jenkins2.5新加入的语法)

image.png

特点:
1.最外层必须由pipline{ //do something }来进行包裹
2.不需要分号作为分隔符,每个语句必须在一行内
3.不能直接使用groovy语句(例如循环判断等),需要被script {}包裹

声明式pipeline案例:

image.png


  1. pipeline {
  2. agent any //没有指定agent
  3. stages{ //步骤
  4. stage('checkout code'){ //第一个步骤
  5. steps{
  6. sh 'echo "youngfit!!!"'
  7. }
  8. }
  9. }
  10. }

image.png

  1. pipeline{
  2. agent any
  3. stages{
  4. stage('mvn-version'){
  5. steps{
  6. sh 'mvn -v'
  7. }
  8. }
  9. stage('jdk-version'){
  10. steps{
  11. sh 'java -version'
  12. }
  13. }
  14. }
  15. }

image.png

1.参数详解

agent:该部分指定整个Pipeline或特定阶段将在Jenkins环境中执行的位置,具体取决于该agent 部分的放置位置。该部分必须在pipeline块内的顶层定义 ,也可以使用在stage级。
stage:表示这个Pipeline的某一个执行阶段(使用stage使得逻辑变得更加简单明了)
steps: 包含一个或者多个在stage块中执行的step序列(在这里执行操作:运行maven或者部署等等)
environment:指定键值对,可用于step中,主要是为常量或者变量赋值,根据所在的位置来决定其作用范围(类似于java中全局和局部的概念)
options:允许执行pipeline内置的专用选项,也可以使用由插件提供的
parameters:提供触发pipeline时的参数列表
trigger:定义了触发pipeline的方式(jenkins1.x中的pollscm定时构建)
tools:自动安装工具,注意这里使用的一定是在jenkins全局配置中已经定义好了的
when:可以用来执行一些代码逻辑
post:可以根据pipeline的状态来执行一些操作
agent的一些参数如下:
any : 在任何可用的机器上执行pipeline
none : 当在pipeline顶层使用none时,每个stage需要指定相应的agent
agent{ label ‘slave1’}

2.agent指定

2.1 labels
  1. pipeline{
  2. agent { label 'master'} //指定在哪个节点运行,后面的步骤都在指定的节点运行
  3. stages{
  4. stage('mvn-version'){
  5. steps{
  6. sh 'mvn -v'
  7. }
  8. }
  9. stage('jdk-version'){
  10. steps{
  11. sh 'java -version'
  12. }
  13. }
  14. }
  15. }
###### 2.2 agent none
  1. pipeline{
  2. agent none //如果这里是none,后面每一步,都要指定运行节点
  3. stages{
  4. stage('mvn-version'){
  5. agent { label 'master'}
  6. steps{
  7. sh 'mvn -v'
  8. }
  9. }
  10. stage('jdk-version'){
  11. agent { label 'node'}
  12. steps{
  13. sh 'java -version'
  14. }
  15. }
  16. }
  17. }
###### 2.3 agent docker
  1. pipeline {
  2. agent {
  3. docker { image 'maven:latest' }
  4. }
  5. stages {
  6. stage('Test') {
  7. steps {
  8. sh 'mvn -v'
  9. }
  10. }
  11. }
  12. }

![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831533141-51bdf609-2c9f-4779-8571-f83c247a4141.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u434d41fe&margin=%5Bobject%20Object%5D&name=image.png&originHeight=776&originWidth=1325&originalType=url&ratio=1&rotation=0&showTitle=false&size=86403&status=done&style=none&taskId=u796ca80f-dd3b-4bbe-b5db-462a8319326&title=) ###### 2.4 options ## 设置保存最近的记录
`options { buildDiscarder(logRotator(numToKeepStr: '1')) }`

## 禁止并行构建,因为用的worksapce还是同一个
`options { disableConcurrentBuilds() }`

## 跳过默认的代码检出
`options { skipDefaultCheckout() }`

## 设定流水线的超时时间(可用于阶段级别)
`options { timeout(time: 1, unit: 'HOURS') }`

## 设定流水线的重试次数(可用于阶段级别)
`options { retry(3) }`
## 设置日志时间输出(可用于阶段级别)
`options { timestamps() }` #示例:
  1. pipeline {
  2. agent any
  3. options {
  4. buildDiscarder(logRotator(numToKeepStr: '1'))
  5. disableConcurrentBuilds()
  6. skipDefaultCheckout()
  7. timeout(time: 1, unit: 'HOURS')
  8. timestamps()
  9. retry(3)
  10. }
  11. stages {
  12. stage ('test1') {
  13. environment {
  14. name = '飞哥'
  15. }
  16. steps {
  17. sh 'echo "The username is ${name}"'
  18. }
  19. }
  20. }
  21. }

![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831533616-a345dfa1-b059-4998-8714-e99bcc900c3c.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u5bb42750&margin=%5Bobject%20Object%5D&name=image.png&originHeight=648&originWidth=993&originalType=url&ratio=1&rotation=0&showTitle=false&size=75319&status=done&style=none&taskId=u8aeb24b5-5901-4e07-bec8-8dca32243ca&title=) ###### 2.5 environment
  1. pipeline {
  2. agent any
  3. environment {
  4. unit_test = '云计算工程师'
  5. username = 'youngfit'
  6. company = '云科技有限公司'
  7. }
  8. stages {
  9. stage('Example') {
  10. steps {
  11. sh 'echo ${username} 在 ${company} 工作是 ${unit_test}'
  12. }
  13. }
  14. }
  15. }


image.png

2.6 parameters
  1. pipeline {
  2. agent any
  3. environment {
  4. unit_test = 'true'
  5. username = 'youngfit'
  6. company = '云科技有限公司'
  7. }
  8. parameters {
  9. string defaultValue: 'jenkins:1.1.1', description: '版本号', name: 'Version', trim: true
  10. }
  11. stages {
  12. stage('Example') {
  13. steps {
  14. sh 'echo ${username} 在 ${company} 工作是 ${unit_test}的'
  15. echo "${params.Version}"
  16. }
  17. }
  18. }
  19. }

![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831534196-a91f547e-3ecc-454f-9166-55795f40788d.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u531b55b9&margin=%5Bobject%20Object%5D&name=image.png&originHeight=640&originWidth=829&originalType=url&ratio=1&rotation=0&showTitle=false&size=65696&status=done&style=none&taskId=u6ab0ca13-ca98-4f49-a989-da0430113a0&title=)
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831534343-33766d66-b65f-4299-8ef9-02978a8c5d54.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u0d89250a&margin=%5Bobject%20Object%5D&name=image.png&originHeight=732&originWidth=1140&originalType=url&ratio=1&rotation=0&showTitle=false&size=89287&status=done&style=none&taskId=u7ebbd771-8d67-4fcf-ace5-6e0509202f8&title=) ###### 2.7 tools
  1. // String description = "版本号"
  2. pipeline {
  3. agent any
  4. environment {
  5. unit_test = 'true'
  6. username = 'youngfit'
  7. company = '云科技有限公司'
  8. }
  9. parameters {
  10. string defaultValue: 'jenkins:1.1.1', description: '版本号', name: 'Version', trim: true
  11. }
  12. tools {
  13. maven 'maven-jdk8'
  14. jdk 'java1.8'
  15. }
  16. stages {
  17. stage('Example') {
  18. steps {
  19. sh 'echo ${username} 在 ${company} 工作是 ${unit_test}的'
  20. echo "${params.Version}"
  21. sh 'java -version'
  22. sh 'mvn -v'
  23. }
  24. }
  25. }
  26. }

![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831534521-a8f705e8-4d84-4c86-8445-341ea2d28a45.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u8f6a5229&margin=%5Bobject%20Object%5D&name=image.png&originHeight=643&originWidth=1336&originalType=url&ratio=1&rotation=0&showTitle=false&size=55554&status=done&style=none&taskId=uaee6c64b-151c-495a-acc1-3081c7788ea&title=) ###### 2.8 Parallel并串行化
  1. // String description = "版本号"
  2. pipeline {
  3. agent any
  4. stages {
  5. stage('Stage1') {
  6. steps {
  7. sh 'echo "步骤1"'
  8. }
  9. }
  10. stage('并行执行的 Stage') {
  11. parallel {
  12. stage('Stage2.1') { //Stage2.1、2.2和2.3是并行的,但是Stage2.3中的2步仍是串行的
  13. // agent { label "test1" }
  14. steps {
  15. echo "在 agent test1 上执行的并行任务 1."
  16. }
  17. }
  18. stage('Stage2.2') {
  19. // agent { label "test2" }
  20. steps {
  21. echo "在 agent test2 上执行的并行任务 2."
  22. }
  23. }
  24. stage('Stage2.3') {
  25. stages {
  26. stage ('串行1') {
  27. steps {
  28. echo "在 agent test2 上执行的串行任务 1."
  29. }
  30. }
  31. stage ('串行2') {
  32. steps {
  33. echo "在 agent test2 上执行的串行任务 2."
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. }

![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831534974-f226a2a4-16fa-4386-9c10-4222b264ea65.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u93f7cd16&margin=%5Bobject%20Object%5D&name=image.png&originHeight=561&originWidth=671&originalType=url&ratio=1&rotation=0&showTitle=false&size=36489&status=done&style=none&taskId=u2ecf7cc4-d8a3-4b5e-a33b-c8056f754b7&title=) ###### 2.9 post钩子 post 步骤在Jenkins pipeline语法中是可选的,包含的是整个pipeline或阶段完成后一些附加的步骤。 比如我们希望整个pipeline执行完成之后或pipeline的某个stage执行成功后发生一封邮件,就可以使用post,可以理解为”钩子“。 根据pipeline或阶段的完成状态,post部分分成多种条件块,包括:
• always:不论当前完成状态是什么,都执行。
• changed:只要当前完成状态与上一次完成状态不同就执行。
• fixed:上一次完成状态为失败或不稳定(unstable),当前完成状态为成功时执行。
• regression:上一次完成状态为成功,当前完成状态为失败、不稳定或中止(aborted)时执行。
• aborted:当前执行结果是中止状态时(一般为人为中止)执行。
• failure:当前完成状态为失败时执行。
• success:当前完成状态为成功时执行。
• unstable:当前完成状态为不稳定时执行。
• cleanup:清理条件块。不论当前完成状态是什么,在其他所有条件块执行完成后都执行。post部分可以同时包含多种条件块。 以下是 post 部分的完整示例
  1. pipeline {
  2. agent { label 'master' }
  3. stages {
  4. stage('buildCode') {
  5. steps {
  6. echo "building all codes1"
  7. }
  8. post {
  9. always {
  10. echo 'scp code.tar to apps'
  11. }
  12. }
  13. }
  14. }
  15.  
  16. post {
  17. changed {
  18. echo 'pipeline post changed'
  19. }
  20. always {
  21. echo 'pipeline post always'
  22. }
  23. success {
  24. echo 'pipeline post success'
  25. }
  26. // 省略其他条件块
  27. }
  28. }

![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831535602-a49ceaaa-e8f0-4938-a8f1-cceb3f64faf2.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u0531b238&margin=%5Bobject%20Object%5D&name=image.png&originHeight=638&originWidth=766&originalType=url&ratio=1&rotation=0&showTitle=false&size=59616&status=done&style=none&taskId=ufea82c86-4221-4a12-a667-d4f51d310b5&title=)
  1. //failure测试
  2. pipeline {
  3. agent { label 'master' }
  4. stages {
  5. stage('buildCode') {
  6. steps {
  7. echo "building all codes1"
  8. }
  9. post {
  10. always {
  11. sh 'mkdir /opt/a/ab' //这一步是会失败的
  12. }
  13. }
  14. }
  15. }
  16.  
  17. post {
  18. failure {
  19. echo 'pipeline post failure' //会执行这一步
  20. }
  21. }
  22. }


image.png
其他的,兄弟们就自己玩着测试吧!!!

2.10 when

指令2: when

该when指令允许Pipeline根据给定的条件确定是否执行该阶段。该when指令必须至少包含一个条件。如果when指令包含多个条件,则所有子条件必须为舞台执行返回true。这与子条件嵌套在一个allOf条件中相同(见下面的例子)。

更复杂的条件结构可使用嵌套条件建:not,allOf或anyOf。嵌套条件可以嵌套到任意深度。When{…}是写在stage{…}里面一层条件控制,下面来看看when{…}支持的一些内置条件命令。

branch


当正在构建的分支与给出的分支模式匹配时执行阶段,例如:when { branch ‘master’ }。请注意,这仅适用于多分支Pipeline。

environment

当指定的环境变量设置为给定值时执行阶段,例如: when { environment name: ‘DEPLOY_TO’, value: ‘production’ }

expression

当指定的Groovy表达式求值为true时执行阶段,例如: when { expression { return params.DEBUG_BUILD } }

not

当嵌套条件为false时执行阶段。必须包含一个条件。例如:when { not { branch ‘master’ } }

allOf

当所有嵌套条件都为真时,执行舞台。必须至少包含一个条件。例如:when { allOf { branch ‘master’; environment name: ‘DEPLOY_TO’, value: ‘production’ } }

anyOf

当至少一个嵌套条件为真时执行舞台。必须至少包含一个条件。例如:when { anyOf { branch ‘master’; branch ‘staging’ } }
举例一个 使用expression条件的when的代码示例。

  1. pipeline {
  2. agent any
  3. stages {
  4. stage('Example Build') {
  5. steps {
  6. script {
  7. echo 'Hello World'
  8. }
  9. }
  10. }
  11. stage('Example Deploy') {
  12. when {
  13. expression {
  14. return (1 == 1) //返回值为True
  15. }
  16. }
  17. steps {
  18. echo 'Deploying'
  19. }
  20. }
  21. }
  22. }


image.png

environment测试

  1. pipeline {
  2. agent any
  3. environment {
  4. username = 'youngfit'
  5. }
  6. stages {
  7. stage('Example Build') {
  8. steps {
  9. script {
  10. echo 'Hello World'
  11. }
  12. }
  13. }
  14. stage('Example Deploy') {
  15. when {
  16. environment name: 'username', value: 'youngfit'
  17. }
  18. steps {
  19. echo 'youngfit'
  20. }
  21. }
  22. }
  23. }


image.png

2.11 pipeline scm

image.png
image.png
image.png
image.png
image.png

2.12 pipeline scm项目实战

本次构建基于二.12之后
将代码写入仓库的Jenkinsfile中;进行构建:

  1. node {
  2. def gitUrl = "git@gitee.com:youngfit/easy-springmvc-maven.git"
  3. def git_Branch = 'master'
  4. value = "=========================================================="
  5. stage ('拉取代码') {
  6. print "${value}"
  7. git branch: "${git_Branch}", url: "${gitUrl}", credentialsId: "1"
  8. }
  9.  
  10. stage ('编译打包') {
  11. print "${value}"
  12. sh 'mvn clean package'
  13. }
  14.  
  15. stage ('scp发布到tomcat后端服务') {
  16. print '${values}'
  17. sh 'ansible-playbook /etc/ansible/jenknis_scp.yaml'
  18. // sh 'sleep 40'
  19. sh 'ansible java-server1 -m shell -a "nohup /opt/script/app-jenkins.sh &"'
  20. }
  21. }

![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831538137-ee091691-3d91-49e0-bcef-c5d7b290a108.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ud25fb95f&margin=%5Bobject%20Object%5D&name=image.png&originHeight=454&originWidth=1179&originalType=url&ratio=1&rotation=0&showTitle=false&size=49239&status=done&style=none&taskId=u394118ac-8185-4d81-a78b-05b23a18930&title=)
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831538654-c139da0e-3a12-492d-9996-94ae538acac8.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uda9b7f6f&margin=%5Bobject%20Object%5D&name=image.png&originHeight=830&originWidth=1256&originalType=url&ratio=1&rotation=0&showTitle=false&size=47975&status=done&style=none&taskId=ub7b2fc2b-6e2e-4f26-93e5-b7da7406132&title=)
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831538648-039b2103-6054-4ae7-9480-ab4d655facbf.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u2cb97f76&margin=%5Bobject%20Object%5D&name=image.png&originHeight=275&originWidth=859&originalType=url&ratio=1&rotation=0&showTitle=false&size=31569&status=done&style=none&taskId=ud423a13f-25ff-43a3-818a-764c3f23ee6&title=) ###### 2.13 上线企业k8s容器化应用 描述:利用pipeline代码,上线公司企业级容器应用
1.Jenkins服务器拉取代码
2.Jenkins服务器编译打包,生成war包
3.jenkins服务器利用Dockerfile将新war包拷入镜像,生成新的镜像,镜像标签随着tag而变化(Dockerfile是在代码仓库中的,也就是开发人员每次推送代码,也会夹带着一个Dockerfile推送过去)
4.将镜像推送到远程镜像仓库中,推送成功删除镜像
5.jenkins服务器利用ansible对k8s-master节点进行更新镜像 ###### 2.13.1 Jenkins服务器配置 配置解析
[root@jenkins-server1 ~]# cat /etc/hosts 将K8s主节点的解析
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831538906-d3de9ed1-01cb-4b7f-9d7b-d79426090e19.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uf810b578&margin=%5Bobject%20Object%5D&name=image.png&originHeight=248&originWidth=1118&originalType=url&ratio=1&rotation=0&showTitle=false&size=25833&status=done&style=none&taskId=ua789ccd3-ed73-407b-acee-1e09d385c21&title=)
下载ansbile
[root@jenkins-server1 ~]# yum -y install epel-release ; yum -y install ansible
配置ansible主机清单
[root@jenkins-server1 ~]# cat /etc/ansible/hosts #在最后面添加
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831538867-eef9a548-483f-4ea2-903b-e612313da0b1.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uf4f45f4b&margin=%5Bobject%20Object%5D&name=image.png&originHeight=505&originWidth=792&originalType=url&ratio=1&rotation=0&showTitle=false&size=48904&status=done&style=none&taskId=u5eca5f23-f774-4046-8ac5-90f2abfe32f&title=)
发送公钥到k8s-master节点
[root@jenkins-server1 ~]# ssh-copy-id -i k8s-master
测试连通性
[root@jenkins-server1 ~]# ansible k8s-master -m ping
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831538993-5bab5424-e092-4074-8485-204156178b96.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ue7d1c58e&margin=%5Bobject%20Object%5D&name=image.png&originHeight=328&originWidth=1373&originalType=url&ratio=1&rotation=0&showTitle=false&size=34388&status=done&style=none&taskId=u68673efd-0e00-4044-a8c1-a361f876b01&title=) ###### 2.13.2 k8s主节点配置 #已有的deployment。是一个tomcat容器多副本的deployment
[root@k8s-master tomcat]# pwd
/root/tomcat
[root@k8s-master tomcat]# vim tomcat.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: tomcat-deployment
spec:
selector:
matchLabels:
app: tomcat
replicas: 1
template:
metadata:
labels:
app: tomcat
spec:
nodeName: k8s-node1
containers:
- name: tomcat
image: daocloud.io/library/tomcat:8.0.45
ports:
- containerPort: 8080
\#为了看效果,创建1个servcie,让tomcat能够访问
[root@k8s-master tomcat]# cat tomcat-service.yaml
apiVersion: v1
kind: Service
metadata:
name: tomcat-service
spec:
type: NodePort #类型
ports:
- port: 8080
nodePort: 30002
targetPort: 8080
selector: #选择器
app: tomcat #对应上deployment给设置的标签
[root@k8s-master tomcat]# kubectl apply -f tomcat.yaml
[root@k8s-master tomcat]# kubectl apply -f tomcat-service.yaml
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831539588-e7372c41-62ab-4d9a-929e-a67fa00745f7.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ua01b4712&margin=%5Bobject%20Object%5D&name=image.png&originHeight=595&originWidth=1524&originalType=url&ratio=1&rotation=0&showTitle=false&size=179343&status=done&style=none&taskId=u68d24b26-c89b-4ee5-aae5-5f6c51401dd&title=)
上面是公司已有的k8s中tomcat容器,现在的需求是:
要实现持续集成/持续交付的容器更新,更新的代码,是要更新到容器中,并且重启,才能生效 ###### 2.13.3 更新仓库 我这里用的是gitee的仓库,同步的github上一个开源的项目代码
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831540182-cbcf8278-b033-4edf-8865-f67526a1720c.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u7cc6c290&margin=%5Bobject%20Object%5D&name=image.png&originHeight=577&originWidth=1148&originalType=url&ratio=1&rotation=0&showTitle=false&size=70316&status=done&style=none&taskId=u60d15a34-71b7-40e5-bc97-06d6ac0239a&title=)
模拟开发人员更新代码,开发人员肯定使用自己电脑上的git客户端工具进行提交,我这里用虚拟机
[root@git-client tmp]# git clone git@gitee.com:youngfit/easy-springmvc-maven.git
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831540171-cd3e2532-366c-4546-9910-b00f34304a2f.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u1cd68963&margin=%5Bobject%20Object%5D&name=image.png&originHeight=316&originWidth=1235&originalType=url&ratio=1&rotation=0&showTitle=false&size=47161&status=done&style=none&taskId=u1bb2556d-0be2-456a-8a4c-f78067c9ab0&title=)
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831540440-5e03c32e-86ef-4e28-8281-3ed75cda1e96.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u71c38b22&margin=%5Bobject%20Object%5D&name=image.png&originHeight=142&originWidth=763&originalType=url&ratio=1&rotation=0&showTitle=false&size=16305&status=done&style=none&taskId=u69e8d3c4-cac5-4c08-8e2a-ffa3590377b&title=)
[root@git-client tmp]# cd easy-springmvc-maven/
[root@git-client easy-springmvc-maven]# vim Dockerfile
From daocloud.io/library/tomcat:8.0.45 #这里基于tomcat的一个镜像,从daocloud上找的拉取链接
RUN rm -rf /usr/local/tomcat/webapps/* #删除镜像中tomcat默认发布目录下的内容,方便后续放更新的war包
ADD ./target/easy-springmvc-maven.war /usr/local/tomcat/webapps/ #将编译打包好的war包,拷贝进来
ENTRYPOINT ["/usr/local/tomcat/bin/catalina.sh","run"] #启动tomcat
当然,我这里没有对代码进行更新;只添加了1个Dockerfile,下面提交代码到gitee仓库中,提交到仓库之前打tag;
[root@jenkins-server1 easy-springmvc-maven]# git add *
[root@jenkins-server1 easy-springmvc-maven]# git commit -m "v2.0"
[root@jenkins-server1 easy-springmvc-maven]# git tag -a "v2.0" -m "v2.0"
[root@jenkins-server1 easy-springmvc-maven]# git checkout v2.0
[root@jenkins-server1 easy-springmvc-maven]# git push origin v2.0
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831540681-30f492f5-420d-4a3c-a48c-bc4871b4346a.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u95747157&margin=%5Bobject%20Object%5D&name=image.png&originHeight=350&originWidth=984&originalType=url&ratio=1&rotation=0&showTitle=false&size=45767&status=done&style=none&taskId=ufd2843f8-4576-4ca2-8465-e5790e195f9&title=)
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831540878-ec111591-57e7-4547-8e12-a05fecd27d01.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ue579ca86&margin=%5Bobject%20Object%5D&name=image.png&originHeight=641&originWidth=1515&originalType=url&ratio=1&rotation=0&showTitle=false&size=86900&status=done&style=none&taskId=u3b710f6c-ad94-4208-be10-072759bc86d&title=)
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831541392-b981487d-a1ec-46bd-8f62-929c1a06ace9.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u298c7358&margin=%5Bobject%20Object%5D&name=image.png&originHeight=623&originWidth=1163&originalType=url&ratio=1&rotation=0&showTitle=false&size=75389&status=done&style=none&taskId=ufd1e4405-bc2f-4952-866f-bfa2a90ab1e&title=) ###### 2.13.3 pipeline代码
  1. pipeline {
  2. agent {label 'master'}
  3. //设置环境变量,自定义即可
  4. environment {
  5. gitee_url = "git@gitee.com:youngfit/easy-springmvc-maven.git"
  6. gitee_branch = "master"
  7. image_url = "registry.cn-beijing.aliyuncs.com/youngfit/tomcat:${tag}"
  8. k8s_master = "k8s-master"
  9. NS = "default"
  10. }
  11. //拉取代码
  12. stages {
  13. stage('拉取代码') {
  14. steps {
  15. script{
  16. git branch: "${gitee_branch}", url: "${gitee_url}"
  17. sh "git checkout ${tag}; git branch"
  18. }
  19. }
  20. }
  21. //编译打包
  22. stage('编译打包') {
  23. steps {
  24. sh "cd /root/.jenkins/workspace/test-p1"
  25. sh "mvn clean package"
  26. }
  27. }
  28. //生成新的docker镜像
  29. stage('构建镜像') {
  30. steps {
  31. sh "docker build -t tomcat:${tag} ." //上一步已经切换到了Dockerfile所在的目录
  32. }
  33. }
  34. //将镜像上传至仓库,我这里用的是阿里云仓库,你也可以换成harbor都行;
  35. stage ("上传镜像到aliyun仓库") {
  36. steps {
  37. sh "docker login -u youngfit -p QianFeng@123 registry.cn-beijing.aliyuncs.com"
  38. sh "docker tag tomcat:${tag} registry.cn-beijing.aliyuncs.com/youngfit/tomcat:${tag}"
  39. sh "docker push registry.cn-beijing.aliyuncs.com/youngfit/tomcat:${tag} && docker rmi registry.cn-beijing.aliyuncs.com/youngfit/tomcat:${tag} tomcat:${tag}"
  40. }
  41. }
  42. //更新k8s容器
  43. stage ("k8s集群更新docker镜像") {
  44. steps {
  45. sh "ansible '${k8s_master}' -m shell -a 'kubectl set image deployment tomcat-deployment tomcat=registry.cn-beijing.aliyuncs.com/youngfit/tomcat:${tag} -n ${NS}'"
  46. }
  47. }
  48. }
  49. }
###### 2.13.4 进行构建 ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831541396-79a8d318-d0e4-474f-a65c-82480ab9bc0d.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uaf365808&margin=%5Bobject%20Object%5D&name=image.png&originHeight=482&originWidth=728&originalType=url&ratio=1&rotation=0&showTitle=false&size=52224&status=done&style=none&taskId=u876202cd-c8f7-422a-9f26-01e6632f1f8&title=)
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831541759-141c3135-12ca-4060-bbb7-3df71c1843bc.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u54f5002d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=787&originWidth=1538&originalType=url&ratio=1&rotation=0&showTitle=false&size=64890&status=done&style=none&taskId=ub3b13075-78f1-4664-96c3-5b77294091f&title=)
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831541924-4e3fa744-1d78-4f82-9da2-c40043263135.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ue5ed3df4&margin=%5Bobject%20Object%5D&name=image.png&originHeight=200&originWidth=1100&originalType=url&ratio=1&rotation=0&showTitle=false&size=29068&status=done&style=none&taskId=u7b19dbb4-94bc-4a04-aace-89e359df1ba&title=) ###### 2.13.5 访问测试 ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831542374-f5c00e11-8d74-4d85-9048-0fc587bd97f9.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u9236256e&margin=%5Bobject%20Object%5D&name=image.png&originHeight=254&originWidth=946&originalType=url&ratio=1&rotation=0&showTitle=false&size=39520&status=done&style=none&taskId=uf25f18cf-4777-41c7-a8ca-5343be56f3c&title=)
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831543198-1035f0b6-7a50-43aa-953c-ee49038210e2.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uc1cac02f&margin=%5Bobject%20Object%5D&name=image.png&originHeight=394&originWidth=1571&originalType=url&ratio=1&rotation=0&showTitle=false&size=87735&status=done&style=none&taskId=u4dd0a0bb-8a8d-452a-a6ca-f7d8430b81b&title=) ###### 2.13.6 再次模拟代码更新 开发人员:
  1. [root@git-client webapp]# pwd
  2. /tmp/easy-springmvc-maven/src/main/webapp
  3. [root@git-client webapp]# ls
  4. index.jsp result.jsp WEB-INF
  5. [root@git-client webapp]# vim index.jsp
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831543205-bcdf13e7-db4c-4eb9-b15f-a88d90f3d9ec.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u752137fc&margin=%5Bobject%20Object%5D&name=image.png&originHeight=624&originWidth=1448&originalType=url&ratio=1&rotation=0&showTitle=false&size=95678&status=done&style=none&taskId=u9d65a8b2-3acd-4cb2-bb05-0c8b6635487&title=)
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831543495-492d09d0-51ab-47b2-b4e6-31e2e8cd7d03.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ud0c1625c&margin=%5Bobject%20Object%5D&name=image.png&originHeight=608&originWidth=1430&originalType=url&ratio=1&rotation=0&showTitle=false&size=80836&status=done&style=none&taskId=u66726eb4-7267-476b-98cb-09b34e9df6a&title=)
代码推送到仓库,记得打tag
  1. root@git-client webapp]# cd ../../..
  2. [root@git-client easy-springmvc-maven]# pwd
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831543573-ae73b192-2155-4b6f-b3be-9404e2a7f9e8.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u91a8f155&margin=%5Bobject%20Object%5D&name=image.png&originHeight=131&originWidth=1013&originalType=url&ratio=1&rotation=0&showTitle=false&size=22742&status=done&style=none&taskId=uad1dda7b-871c-4b6d-8e5a-ad501c2e17a&title=)
  1. [root@git-client easy-springmvc-maven]# git add *
  2. [root@git-client easy-springmvc-maven]# git commit -m "v2.1"
  3. [root@git-client easy-springmvc-maven]# git tag -a "v2.1" -m "v2.1"
  4. [root@git-client easy-springmvc-maven]# git checkout v2.1
  5. [root@git-client easy-springmvc-maven]# git push origin v2.1


image.png
再次构建
image.png
image.png
image.png
image.png
image.png

2.13.7 私仓secret拉取镜像

对于公司内部的项目, 我们不可能使用公有开放的镜像仓库, 一般情况可能会花钱买 docker私仓服务, 或者说自己在服务器上搭建自己的私仓, 但不管怎样, 我们如何让k8s能够拉取私有仓库的镜像

1、私有仓库准备

用Harbor、阿里云、腾讯云容器镜像仓库都可以,在公司一般都使用Harbor仓库。不过使用方式都一样
我这里用阿里云容器镜像仓库
将之前的公有仓库修改为私有。
Jenkins-pipeline - 图25
私有仓库,pod去拉取镜像的时候就需要认证了,但是我们又不确定是pod会分配到哪个k8s的node节点上,所以无法在pipeline代码中直接编写。这个时候我们可以采用secret拉取私有镜像;

2、拉取失败案例

开发人员更改代码,推送到私有仓库

  1. # git clone git@gitee.com:youngfit/easy-springmvc-maven.git
  2. # cd easy-springmvc-maven/
  3. # vim src/main/webapp/index.jsp

![](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649821294909-a266d1ac-aa5f-4f26-b6ba-e302981064d0.png#crop=0&crop=0&crop=1&crop=1&from=url&id=o38GR&margin=%5Bobject%20Object%5D&originHeight=483&originWidth=1638&originalType=binary&ratio=1&rotation=0&showTitle=false&status=done&style=none&title=)
  1. # git add *
  2. # git commit -m "用户名称密码"
  3.  
  4. # git tag -a "v2.3" -m "v2.3"
  5. # git push origin v2.3

Jenkins-pipeline - 图26
使用Jenkins进行构建
Jenkins-pipeline - 图27

[root@k8s-master pipe]# kubectl get pod #master节点查看


Jenkins-pipeline - 图28
可看到镜像拉取失败,因为没有提前登录;
但是我们在pipeline的代码中,无法确定pod会分配到哪台node节点,无法编写

2、配置secret

登录我的阿里云私有仓库
# docker login —username=youngfit —password=’QianFeng@123’ registry.cn-hangzhou.aliyuncs.com
就算登录成功了。这个时候我们虽然可以通过docker pull命令拉取镜像, 但无法通过k8s创建pod方式拉取

3、创建secret

在k8s-master节点操作
# docker login --username=youngfit --password='QianFeng@123' registry.cn-hangzhou.aliyuncs.com

其中:regsecret: 指定密钥的键名称, 可自行定义—docker-server: 指定docker仓库地址—docker-username: 指定docker仓库账号—docker-password: 指定docker仓库密码—docker-email: 指定邮件地址(选填)
可以看到当前除了默认的密钥, 还有我们刚才生成的. 另外要注意的是, 该密钥只能在对应namespace使用, 也就是这里的default, 如果需要用到其他namespace, 比如说test, 就需要在生成的时候指定参数 -n test。
[root@k8s-master ~]# kubectl get secret

Jenkins-pipeline - 图29

4、yml文件加入密钥参数

[root@k8s-master pipe]# vim tomcat.yaml
Jenkins-pipeline - 图30

[root@k8s-master pipe]# kubectl apply -f tomcat.yaml


Jenkins-pipeline - 图31
Jenkins-pipeline - 图32
再次访问,成功
Jenkins-pipeline - 图33

二、脚本式Script

image.png
特点:
1.最外层有node{}包裹
2.可直接使用groovy语句
如何创建最基本的Pipeline直接在Jenkins Web UI 网页界面中输入脚本通过创建一个jenkinsfile可以检入项目的源代码管理库
通常推荐在Jenkins中直接从源代码控制(SCM)中载入Jenklinsfile Pipeline

脚本式pipeline案例:

image.png
image.png

1.引用linux命令
  1. node {
  2. stage('step1'){
  3. sh 'touch /home/qf.txt'
  4. sh 'echo "Jenkins-pipeline\ntest1" >> /home/qf.txt'
  5. }
  6. }

![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831546048-869976f6-0460-4a98-8ac2-47c964e7e75d.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ub583b3b6&margin=%5Bobject%20Object%5D&name=image.png&originHeight=682&originWidth=1481&originalType=url&ratio=1&rotation=0&showTitle=false&size=54903&status=done&style=none&taskId=u309ad702-7d51-4360-9511-9ba30360df4&title=)
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831546330-15bc3b34-55bc-4f29-a8bd-d2b9682bff5b.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u18b443cf&margin=%5Bobject%20Object%5D&name=image.png&originHeight=645&originWidth=1107&originalType=url&ratio=1&rotation=0&showTitle=false&size=103111&status=done&style=none&taskId=u9af14923-2857-4627-a9fa-c728eb273ef&title=)
![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831546485-6922e687-8741-4e11-857f-2d2da7b18a90.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ued1e17aa&margin=%5Bobject%20Object%5D&name=image.png&originHeight=633&originWidth=1077&originalType=url&ratio=1&rotation=0&showTitle=false&size=72814&status=done&style=none&taskId=u51e815d7-c0e5-41a8-8a92-df529751063&title=)
  1. [root@jenkins-server1 .jenkins]# cat /home/qf.txt
  2. Jenkins-pipeline
  3. test1
  4. 如果有jenkins从节点,想要指定在从节点上面执行,可以这样写
  5. node (node-test1) { //括号中,指定从节点名称;
  6. stage('step1'){
  7. sh 'touch /home/qf.txt'
  8. sh 'echo "Jenkins-pipeline\ntest1" >> /home/qf.txt'
  9. }
  10. }
##### 2.设置变量
  1. //为注释,鼠标多行标记,CTRL+/即可
  2. node {
  3. def username = "飞哥" //def可加可不加
  4. stage ('step1') {
  5. print "${username}"
  6. // sh 'touch /home/qf.txt'
  7. // sh 'echo "Jenkins-pipeline\ntest1" >> /home/qf.txt'
  8. }
  9. }


image.png

3.自定义分隔符
  1. node {
  2. def username = "飞哥"
  3. value = "****************************************************"
  4. stage ('step1') {
  5. print "${username}"
  6. }
  7. stage ('step2') {
  8. print "${value}"
  9. sh 'echo "youngfit2021"'
  10. }
  11. }


image.png

4.列表
  1. node {
  2. def username = "飞哥"
  3. list dest_servers = ["192.168.153.190", "192.168.153.195"]
  4. value = "****************************************************"
  5. stage ('step1') {
  6. print "${username}"
  7. }
  8. stage ('step2') {
  9. print "${value}"
  10. sh 'echo "youngfit2021"'
  11. }
  12. stage ('ping3') {
  13. print "${value}"
  14. String dest_num1 = dest_servers[0]
  15. sh "ping -c3 ${dest_num1}"
  16. }
  17. }

![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831546983-58ec97a6-5a89-4fa7-ac75-529dbe19dbbc.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u3ccefcd7&margin=%5Bobject%20Object%5D&name=image.png&originHeight=882&originWidth=1120&originalType=url&ratio=1&rotation=0&showTitle=false&size=87329&status=done&style=none&taskId=ub9b87e17-18fd-47e0-aae3-8e18a692e12&title=) ##### 5.for循环,遍历列表 不使用之前
  1. node {
  2. def username = "飞哥"
  3. host1 = "192.168.153.195" //定义多个变量,比较麻烦
  4. host2 = "192.168.153.196"
  5. value = "****************************************************"
  6. stage ('step1') {
  7. print "${username}"
  8. }
  9. stage ('step2') {
  10. print "${value}"
  11. sh 'echo "youngfit2021"'
  12. }
  13. stage ('ping3') {
  14. print "${value}"
  15. sh "ping -c2 ${host1}"
  16. sh "ping -c2 ${host2}"
  17. }
  18. }

可以使用列表
  1. node {
  2. def username = "飞哥"
  3. list dest_servers = ["192.168.153.190", "192.168.153.195"]
  4. value = "****************************************************"
  5. stage ('step1') {
  6. print "${username}"
  7. }
  8. stage ('step2') {
  9. print "${value}"
  10. sh 'echo "youngfit2021"'
  11. }
  12. stage ('ping3') {
  13. print "${value}"
  14. for (i=0; i

  15. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831547495-7aed2e39-d261-4894-beeb-9003fd690b6f.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u3285ce65&margin=%5Bobject%20Object%5D&name=image.png&originHeight=892&originWidth=1190&originalType=url&ratio=1&rotation=0&showTitle=false&size=66530&status=done&style=none&taskId=udce64f91-387c-48f7-a860-ca4e6f4f715&title=)
  16. ##### 6.拉取代码
  17. node {
  18.     def githubUrl = "https://github.com/bingyue/easy-springmvc-maven"
  19.     value = "****************************************************"
  20.     stage ('step1') {
  21.         print "${value}"
  22.         git branch: "master", url: "${githubUrl}"
  23.     }
  24. }

  25. image.png

  26. 我这里用gitee仓库创建了1个私有仓库
    仓库urlgit@gitee.com:youngfit/jenkins.git
    image.png

  27. jenkins服务器制作密钥对

    [root@jenkins-server1 ~]# yum -y install git
  28. [root@jenkins-server1 ~]# git config --global user.name "feige"
  29. [root@jenkins-server1 ~]# git config --global user.email "908367919@qq.com"
  30. [root@jenkins-server1 ~]# ssh-keygen

  31. # 将公钥配置到gitee中

  32. [root@jenkins-server1 ~]# cat /root/.ssh/id_rsa.pub
  33. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCzBgMIt5DPSY8In23zsTtyXcMdGk4AXtJlGBp177qxOYAscKuCycr3CZkH9fjNw/F6QMUcTcT0A4qjBxJdnHvnLfNOp2EwufBjBbWGGeiKNFmicm6slM+HT+JlhbDubUMD9YGi793dT+ie10Xt5veVvVFfjtQ4O2/Ic6mdmJDVajQL5YEaa72lR4rU8DegbY3t8Ux44Kzf1PANhMq3Swmi6q2E8UGjwU1LE1YzPqXO9otoDVDWSN9gNf/jzV/lY+4YMAriaKU+pkTzv3tVrGA/VY6QWxwMCloJY269Ql5dEXOEFNqHzd40vTD1qBmnNuypYOgC8vFh8TNUpie5tUg/ root@jenkins-server1

  34. image.png

  35. image.png

  36. image.png

  37. node {
  38.     def githubUrl = "git@gitee.com:youngfit/jenkins.git"
  39.     value = "****************************************************"
  40.     stage ('step1') {
  41.         print "${value}"
  42.         git branch: "master", url: "${githubUrl}", credentialsId: '250'
  43.     }
  44. }

  45. image.png
    来到Jenkins服务器查看,代码拉取成功

  46. image.png

  47. 7.版本参数化构建

    jenkins参数化构建
    可以用git的分支、标签、修订/commit的版本来进行使用
    这里我用的是标签的概念,也让我们再了解git的标签概念
    说明”标签就是tag,是开发人员提交代码之前,可以给将要提交的代码打一个tag,比如v1.0,这样容易分辨代码的历史版本”
    也就是说,Jenkins可以识别到仓库里面的分支,历史版本commit、和标签;
    可以选择任何分支、任何历史版本、任何标签进行构建;所以,一旦新发布的代码有问题,我们还可以利用Jenkins重新构建一次没有问题的代码,确保应用使用正常;感受到jenkins的强大了嘛!!!
    Jenkins安装git参数插件“Git Parameter

  48. image.png

  49. 7.1标签式参数

    添加git参数

  50. image.png
    标签/tag、每次提交代码的时候,可以打个tag,类似于版本号;这样可以明确历史版本。
    首先用1台机器作为git客户端,拉取代码,修改代码,打tag,上传至远程仓库,这里远程仓库我用的仍是gitee
    这里git客户端机器,可以用新的,我这里仍然使用的jenkins服务器,各位应该都能看懂;

  51. [root@git-client ~]# yum -y install git
  52. [root@git-client ~]# git config --global user.name "jenkins"
  53. [root@git-client ~]# git config --global user.email "feigeyoungfit@163.com"
  54. [root@git-client ~]# git clone git@gitee.com:youngfit/jenkins.git

  55. 仓库里不是代码,我们这里只是一些文件,看实验效果:
    image.png
    image.png
    image.png
    image.png
    来到gitee页面查看
    image.png
    image.png
    那么来到jenkins服务器
    image.png
    image.png

  56. node {
  57.     def githubUrl = "git@gitee.com:youngfit/jenkins.git"
  58.     value = "****************************************************"
  59.     stage ('step1') {
  60.         print "${value}"
  61.         git branch: "master", url: "${githubUrl}"
  62.         print "${Tag}"
  63.     }
  64. }

  65. image.png
    image.png
    image.png
    image.png
    其实不是出了问题,他拉取下来了,但是并不会自动切换tag,也就是位置仍然处于master分支,并没有切换到v1.0tag的位置;
    #来到Jenkins的工作目录
    [root@jenkins-server1 test-p1]# pwd
    /root/.jenkins/workspace/test-p1
    image.png
    image.png
    如果,你想直接拉取之后,并切换到指定的tag版本中,可以这样写

  66. node {
  67.     def githubUrl = "git@gitee.com:youngfit/jenkins.git"
  68.     value = "-------------------------------------------"
  69.     stage ('step1') {
  70.         print "${value}"
  71.         git branch: "master", url: "${githubUrl}"
  72.         print "${Tag}" //这里打印,只是在页面上显示一下tag,方便我们查看是否生效
  73.         sh "git checkout ${Tag}; git branch"
  74.     }
  75. }

  76. image.png
    image.png
    image.png
    如果每次推送前都打一次tag,那我们再进行版本切换的时候,也就方便多了,比如开发更新了代码,再次推送,并且打了tag,如果推送的代码有问题,我们可以随时用jenkins进行重新构建;
    模拟开发更新代码
    image.png
    image.png
    image.png
    image.png
    image.png
    image.png
    标签:也就是开发推送代码之前,我们要和他们沟通,每次推送前要打个tag,也就是给当前即将推送的代码打个版本号,方便后续我们的管理;各位程序猴应该体会到了~~
    这就是标签式参数化了,不过这种方式有个小缺陷,不是什么大问题;如果是第一次构建,也就是没有拉取过代码的时候,它是无法识别到仓库中的标签的,因为它根本没有尝试识别过仓库地址,更别说识别到里面的tag了;不过识别不到,我们就点击构建即可,让他拉取一次,第二次就可以识别到了
    演示一下:
    1.将刚才的pipline任务删除,或者新建1pipeline任务都可以
    我这里删除吧
    image.png
    image.png
    然后,选择之前的配置,以及标签构建,配置之前的pipline代码
    image.png
    image.png

  77. 7.2commit/修订式参数

    标签式参数搞明白,这个也就容易了;简单说一下就行
    image.png

  78. node {
  79.     def githubUrl = "git@gitee.com:youngfit/jenkins.git"
  80.     value = "-------------------------------------------"
  81.     stage ('step1') {
  82.         print "${value}"
  83.         git branch: "master", url: "${githubUrl}"
  84.         print "${Commit}" //这里打印,只是在页面上显示一下commit历史id,方便我们查看是否生效
  85.         sh 'git checkout ${Commmit}; git branch'
  86.     }

  87. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831557524-de5b7a78-a86f-4db2-baad-20bf225db8d1.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ufa172e14&margin=%5Bobject%20Object%5D&name=image.png&originHeight=745&originWidth=1400&originalType=url&ratio=1&rotation=0&showTitle=false&size=91339&status=done&style=none&taskId=u35e6af55-deca-4b79-99eb-5b3fc71648d&title=)
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831557487-5b768c90-015d-4ec0-a1fb-3be2368c43e0.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u607bb95c&margin=%5Bobject%20Object%5D&name=image.png&originHeight=134&originWidth=983&originalType=url&ratio=1&rotation=0&showTitle=false&size=20371&status=done&style=none&taskId=ue771d1b6-2756-49b3-8d6b-fe457de2468&title=)
    所以,Jenkins是可以直接进行版本的切换构建,发布的,只不过我们这里没有设置后续的动作;各位要理解到位;
  88. ###### 7.3分支式参数
  89. 后续哥再整理!!个人感觉有上面2个,已经够用;
  90. ##### 8.脚本式中引用Credentials
  91. 其实,私有仓库,上面拉取代码应该是要凭证的,指定拉取代码的用户的公钥,要配置到私有仓库中,私钥要配置Jenkins中。上面gitee,并没有使用,也能正常拉取有点奇怪,应该是gitee没有这个要求,githubgitlab都是有要求的不过我们先不纠结这个;记得都配置就可以了;
    我们现在用的是root用户,公钥已经配置到了gitee的配置中,私钥已经配置到了id1Credentials中;
    下面再pipeline的代码中引用对应的Credentials即可
  92. node {
  93.     def githubUrl = "git@gitee.com:youngfit/jenkins.git"
  94.     value = "-------------------------------------------"
  95.     stage ('pull code') {
  96.         print "${value}"
  97.         git branch: "master", url: "${githubUrl}", credentialsId: "1"
  98.     }
  99. }

  100. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831557503-4f63cd59-3212-48cb-a623-1baa69d57a21.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u40ed6024&margin=%5Bobject%20Object%5D&name=image.png&originHeight=760&originWidth=1200&originalType=url&ratio=1&rotation=0&showTitle=false&size=64254&status=done&style=none&taskId=ub809dccb-ac26-4652-be89-8288f6917d8&title=)
  101. ##### 9.定义多版本构建工具
  102. 虽然我们的Jenkins服务器上,暂时只有1个版本的jdk、和1个版本的maven工具;但是在公司的Jenkins服务器上,可能会有多个版本的Jdk或者maven工具,因为在构建一些项目代码时,可能需要用到jdk11的新特性,那么Jenkins服务器上就要有jdk11的版本;
    #上传不同版本的jdk工具和maven工具包,到Jenkins服务器,解压,进行配置
    [root@jenkins-server1 ~]# tar -xvzf openjdk-11+28_linux-x64_bin.tar.gz -C /usr/local/
    [root@jenkins-server1 ~]# tar -xvzf apache-maven-3.5.4-bin.tar.gz -C /usr/local/jdk-11/
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831557917-ca75b004-ae68-4957-b5d0-dca53646e97f.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u3cede4e6&margin=%5Bobject%20Object%5D&name=image.png&originHeight=752&originWidth=1479&originalType=url&ratio=1&rotation=0&showTitle=false&size=51316&status=done&style=none&taskId=u2eb832f2-4e3e-48b9-9dbd-e769e6d83b2&title=)
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831558462-e198b095-818b-4650-bddf-0cdc950405d2.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uc2d0c67c&margin=%5Bobject%20Object%5D&name=image.png&originHeight=769&originWidth=1499&originalType=url&ratio=1&rotation=0&showTitle=false&size=59324&status=done&style=none&taskId=ue4b7b07f-61b5-4185-85d9-0456df924fc&title=)
    node {
    value = "-------------------------------------------"
    mavenHome = tool "maven-jdk11" //这里tool指的是工具,工具名称和jenkins定义的要对应好
    env.MAVEN_HOME = "${mavenHome}"
    env.PATH = "${mavenHome}/bin:${env.PATH}"
    stage ('test1') {
    print "${env.PATH}"
    sh "mvn -version"
    }
    }
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831558497-5a884cee-e1c3-4eb1-b6e4-29877091844b.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u40f710a0&margin=%5Bobject%20Object%5D&name=image.png&originHeight=760&originWidth=1834&originalType=url&ratio=1&rotation=0&showTitle=false&size=104187&status=done&style=none&taskId=uc1ef5ef7-6180-4c8a-871a-b1129ea5196&title=)
    #换成maven-jdk8,试试看效果
    node {
    value = "-------------------------------------------"
    mavenHome = tool "maven-jdk8" //这里tool指的是工具,工具名称和jenkins定义的要对应好
    env.MAVEN_HOME = "${mavenHome}"
    env.PATH = "${mavenHome}/bin:${env.PATH}"
    stage ('test1') {
    print "${env.PATH}"
    sh "mvn -version"
    }
    }
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831559320-5c17407a-964f-483c-8b28-66a7911e6677.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u685a7342&margin=%5Bobject%20Object%5D&name=image.png&originHeight=739&originWidth=1805&originalType=url&ratio=1&rotation=0&showTitle=false&size=102605&status=done&style=none&taskId=u0a88be81-8db1-44af-bdfa-63706453115&title=)
  103. ##### 10.调用Docker容器
  104. 调用工具的时候,麻烦点在于,Jenkins服务器上安装对应工具,而且Jenkins服务也要添加上,后续pipeline的代码中,路径也要配置正确;
    如果Jenkins服务器不想安装工具,可以使用docker容器的方式,比如拉取jdkdocker镜像,镜像中自然会有jdk工具,然后进行调用;那么Jenkins服务器要安装docker服务,Jenkins也要安装调用docker的插件
    [root@jenkins-server1 yum.repos.d]# pwd
    /etc/yum.repos.d
  105. [root@jenkins-server1 yum.repos.d]# wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
    [root@jenkins-server1 yum.repos.d]# yum -y install docker-ce
    [root@jenkins-server1 yum.repos.d]# systemctl start docker && systemctl enable docker
  106. ###### 10.1安装调用docker的插件
  107. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831559407-f8d064fb-0e74-4d50-ae65-5de936d906dd.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u49898be9&margin=%5Bobject%20Object%5D&name=image.png&originHeight=818&originWidth=1806&originalType=url&ratio=1&rotation=0&showTitle=false&size=119555&status=done&style=none&taskId=u10aa2dbb-1b62-4466-a611-91bd00f3f26&title=)
  108. ###### 10.2调用node容器
  109. node {
    value = "-------------------------------------------"
    stage ("test-node") {
    print "${value}"
    docker.image("node:14-alpine").inside{
    sh "node -v"
    }
    }
    }
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831559577-4043c87d-285f-4d39-9c99-5e55ce343c2c.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u7e20aa29&margin=%5Bobject%20Object%5D&name=image.png&originHeight=828&originWidth=1290&originalType=url&ratio=1&rotation=0&showTitle=false&size=87401&status=done&style=none&taskId=u69e05601-8b77-4e7a-a2aa-0581e3a7cd6&title=)
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831560634-1405283f-1a00-4b60-9b18-e772c96a8663.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uf62547c8&margin=%5Bobject%20Object%5D&name=image.png&originHeight=190&originWidth=1240&originalType=url&ratio=1&rotation=0&showTitle=false&size=26527&status=done&style=none&taskId=u9924dd65-4998-469f-b35e-8012e394dcf&title=)
    10.2调用jdk容器
    [root@jenkins-server1 ~]# docker pull daocloud.io/ld00000/jdk-8:latest #提前拉取镜像也可以
    node {
    value = "-------------------------------------------"
    stage ('test-jdk8') {
    docker.image ("daocloud.io/ld00000/jdk-8:latest").inside("-v /path:/path"){
    sh "java -version"
    }
    }
    }
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831561024-fd55d51e-0861-4ce0-b4dc-0a3f548b91ad.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u4e792e4a&margin=%5Bobject%20Object%5D&name=image.png&originHeight=789&originWidth=1867&originalType=url&ratio=1&rotation=0&showTitle=false&size=105935&status=done&style=none&taskId=ub21912e0-64d1-4035-849b-4774087b90d&title=)
  110. ###### 10.2调用maven容器
  111. [root@jenkins-server1 ~]# docker pull maven:latest #提前拉取镜像也可以
    node {
    value = "-------------------------------------------"
    stage ('test-maven') {
    print "${value}"
    docker.image ("maven:latest").inside("-v /path:/path"){
    sh "mvn -v"
    }
    }
    }
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831561209-a52187db-1b33-4aa2-b42a-da262845c2d9.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u8d043cb8&margin=%5Bobject%20Object%5D&name=image.png&originHeight=899&originWidth=1841&originalType=url&ratio=1&rotation=0&showTitle=false&size=106065&status=done&style=none&taskId=u5afe2f58-39b2-4dc0-a29c-02ddeb75bf9&title=)
    总之:想要调用什么工具的命令,就拉取对应的docker镜像,要比安装工具方便的多;注意我们现在调用之后,容器会自动关闭和删除;我们这里没有用工具做具体的运行服务的命令,先感受一下;
  112. ###### 10.3withDockerContainer语法风格
  113. ```javascript
  114. {
    withDockerContainer (args: '-e "http_proxy=xxxx" -e "https_proxy=yyyyy" -v "/home:/home"',
    image: "maven:latest") {
    sh "cd /home && touch qf.txt"
    sh "mvn -v"
    }
    }
  115. ```
  116. // args:添加的参数,可以-v挂载目录,-e设置变量,-p映射端口等
  117. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831561256-237e4eb4-85ab-46e9-8d23-e23f5e13d082.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=udd67e854&margin=%5Bobject%20Object%5D&name=image.png&originHeight=847&originWidth=1845&originalType=url&ratio=1&rotation=0&showTitle=false&size=118413&status=done&style=none&taskId=u3706b4ae-ae4e-4fb5-bccc-7e0c0a20858&title=)
    可以看到,jenkins本机保留了文件,成功了
  118. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831561184-c75c601e-53b1-4e13-8dd8-3d8b80e3fee2.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u27f7f0c3&margin=%5Bobject%20Object%5D&name=image.png&originHeight=149&originWidth=651&originalType=url&ratio=1&rotation=0&showTitle=false&size=8771&status=done&style=none&taskId=u204e64d0-6250-409a-bb79-67ce2411c33&title=)
  119. ##### 11.发送邮件
  120. 前提:Jenkins已安装邮箱插件,Jenkins已配置过关于邮箱的配置
    node {
    stage ('email-test') {
    emailext (
    subject: 'test-jenkins',
    body: '''云计算,YYds!!!
    千锋郑州云计算
    享受学习,低调挣钱!!!''',
    to: '908367919@qq.com'
    )
    }
    }
  121. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831561332-16b196a7-005b-4c9d-ab49-3c34c15b7cc8.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u962a4836&margin=%5Bobject%20Object%5D&name=image.png&originHeight=404&originWidth=907&originalType=url&ratio=1&rotation=0&showTitle=false&size=28929&status=done&style=none&taskId=u19ca5724-8831-429c-ab21-35b4401a160&title=)
  122. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831562107-d6a0f7d7-6b44-4ec9-aad7-c383b096d506.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u01a37631&margin=%5Bobject%20Object%5D&name=image.png&originHeight=577&originWidth=1047&originalType=url&ratio=1&rotation=0&showTitle=false&size=68785&status=done&style=none&taskId=ud276d140-1a4d-4884-aa9e-0641af0745a&title=)
  123. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831562336-157520cd-646b-4954-a7f1-a78f695868fc.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u59f53a1d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=448&originWidth=809&originalType=url&ratio=1&rotation=0&showTitle=false&size=55824&status=done&style=none&taskId=u35c0ab8c-c02d-40c3-951f-da4c01858ab&title=)
  124. 邮箱变量
    node {
    stage ('email-test') {
    emailext (
    subject: 'test-jenkins',
    body: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
    to: '908367919@qq.com'
    )
    }
    }
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831562565-037cf2a6-54e6-43a6-8194-3405f88731b2.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ucc16f7f0&margin=%5Bobject%20Object%5D&name=image.png&originHeight=487&originWidth=805&originalType=url&ratio=1&rotation=0&showTitle=false&size=59691&status=done&style=none&taskId=u30349e81-5029-4efa-ac4a-c0d37b32a6a&title=)
  125. ##### 12.实战项目Pipeline+Maven+Ansible+Tomcat
  126. | **Jenkins服务器** | **192.168.153.190** |
  127. | --- | --- |
  128. | java服务器 | 192.168.153.195 |
  129. [root@jenkins-server1 ~]# ssh-copy-id -i 192.168.153.195 //发送公钥
  130. ###### 12.1 Jenkins服务器配置解析
  131. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831562803-3b64053a-60fe-4370-ac5d-de5a2bb2cdba.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ucf4e7c87&margin=%5Bobject%20Object%5D&name=image.png&originHeight=149&originWidth=1123&originalType=url&ratio=1&rotation=0&showTitle=false&size=18101&status=done&style=none&taskId=u1dbc25e0-7628-4114-b385-77358406044&title=)
  132. ###### 12.2 Jenkins服务器Ansible配置
  133. [root@jenkins-server1 ~]# yum -y install epel-release
    [root@jenkins-server1 ~]# yum -y install ansible
    [root@jenkins-server1 ~]# cat /etc/ansible/hosts #在最后面添加下面内容
    [java-server]
    java-server1
    [java-server:vars] #设置变量,vars--照写
    ansible_ssh_port=22
    ansible_ssh_user=root
    ansible_ssh_private_key_file=/root/.ssh/id_rsa
    #ansible_ssh_pass=test
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831562989-f554ebe0-a758-44a7-bba6-6a9caaaa37a1.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u10b727c8&margin=%5Bobject%20Object%5D&name=image.png&originHeight=257&originWidth=759&originalType=url&ratio=1&rotation=0&showTitle=false&size=19980&status=done&style=none&taskId=ue890ca62-83c8-4e10-b378-e0c98d1bcd2&title=)
    [root@jenkins-server1 ~]# ansible java-server1 -m ping //测试连通性
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831563233-6f6e3938-43ca-46ce-81a8-2acbb68d965b.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u2dadecb1&margin=%5Bobject%20Object%5D&name=image.png&originHeight=316&originWidth=1426&originalType=url&ratio=1&rotation=0&showTitle=false&size=33494&status=done&style=none&taskId=u59159cc2-ca50-4a80-a7ba-340ecc01736&title=)
  134. ###### 12.3 Jenkins剧本
  135. [root@jenkins-server1 ~]# vim /etc/ansible/jenknis_scp.yaml
    ---
    - hosts: java-server1
    user: root
    vars:
    - source_war: "/root/.jenkins/workspace/test-p3/target/easy-springmvc-maven.war"
    - dest_war: "/root/upload"
    tasks:
    - name: scp war
    copy: src={{ source_war }} dest={{ dest_war }}
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831563374-add92317-706c-4a9a-ace1-c9df74bee171.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u3db17a90&margin=%5Bobject%20Object%5D&name=image.png&originHeight=378&originWidth=1286&originalType=url&ratio=1&rotation=0&showTitle=false&size=57350&status=done&style=none&taskId=u3df83156-9e93-4961-bfd2-b978c591a09&title=)
  136. ###### 12.4 后端服务器jdk、tomcat配置
  137. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831563667-80f86130-59ba-4b48-aae0-d8fc09c77788.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ud129378c&margin=%5Bobject%20Object%5D&name=image.png&originHeight=143&originWidth=1208&originalType=url&ratio=1&rotation=0&showTitle=false&size=20861&status=done&style=none&taskId=ub5002c16-ec8c-4097-afc3-3a9ce506944&title=)
    [root@java-server ~]# tar -xvzf jdk-8u211-linux-x64.tar.gz -C /usr/local/
    [root@java-server ~]# tar -xvzf apache-tomcat-8.5.45.tar.gz -C /data/application/
    [root@java-server ~]# cd /usr/local/
    [root@java-server local]# mv jdk1.8.0_211/ java
    [root@java-server local]# cd /data/application/
    [root@java-server application]# mv apache-tomcat-8.5.45/ tomcat
    配置环境变量
    [root@java-server ~]# vim /etc/profile //在最后面添加即可
    export JAVA_HOME=/usr/local/java
    export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
    export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
    export TOMCAT_HOME=/data/application/tomcat
    测试jdk是否可用
    ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831563751-3590e841-2e31-447f-ae43-81a0c22b5812.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u2650fc45&margin=%5Bobject%20Object%5D&name=image.png&originHeight=235&originWidth=971&originalType=url&ratio=1&rotation=0&showTitle=false&size=25205&status=done&style=none&taskId=u82308317-1b11-4e0a-a16e-45d87a5d9e5&title=)
    [root@java-server ~]# mkdir /root/upload/ #创建接受war包的目录。我这里脚本定义的是此目录
    [root@java-server ~]# rm -rf /data/application/tomcat/webapps/*
  138. ###### 12.5 后端服务器配置脚本
  139. [root@java-server ~]# mkdir /opt/script
  140. [root@java-server ~]# vim /opt/script/app-jenkins.sh  #编写重启tomcat的脚本
  141. 说明:此脚本定义了接受war的目录,tomcat的发布目录,主要的思路就是将发布过来的war包,移动到tomcat的发布目录下。
  142. 并检测tomcat是否已经运行,已运行的话,杀死启动,未运行,直接运行;
  143. #!/usr/bin/bash
  144. #本脚本适用于jenkins持续集成,实现备份war包到代码更新上线!使用时请注意全局变量。
  145. #====================================================
  146. #Defining variables
  147. export JAVA_HOME=/usr/local/java
  148. webapp_path="/data/application/tomcat/webapps"
  149. tomcat_run="/data/application/tomcat/bin"
  150. updata_path="/data/update/`date +%F-%T`"
  151. backup_path="/data/backup/`date +%F-%T`"
  152. tomcat_pid=`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`
  153. files_dir="easy-springmvc-maven"
  154. files="easy-springmvc-maven.war"
  155. job_path="/root/upload"
  156. #Preparation environment
  157. echo "Creating related directory"
  158. mkdir -p $updata_path
  159. mkdir -p $backup_path
  160. echo "Move the uploaded war package to the update directory"
  161. mv $job_path/$files $updata_path
  162. echo "========================================================="
  163. cd /opt
  164. echo "Backing up java project"
  165. if [ -f $webapp_path/$files ];then
  166.         tar czf $backup_path/`date +%F-%H`.tar.gz $webapp_path
  167.         if [ $? -ne 0 ];then
  168.                 echo "打包失败,自动退出"
  169.                 exit 1
  170.         else
  171.                 echo "Checking if tomcat is started"
  172.                 if [ -n "$tomcat_pid" ];then
  173.                         kill -9 $tomcat_pid
  174.                         if [ $? -ne 0 ];then
  175.                                 echo "tomcat关闭失败,将会自动退出"
  176.                                 exit 2
  177.                         fi
  178.                 fi
  179.                 cd $webapp_path
  180.                 rm -rf $files && rm -rf $files_dir
  181.                 cp $updata_path/$files $webapp_path
  182.                 cd /opt
  183.                 $tomcat_run/startup.sh
  184.                 sleep 5
  185.                 echo "显示tomcat的pid"
  186.                 echo "`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`"
  187.                 echo "tomcat startup"
  188.                 echo "请手动查看tomcat日志。脚本将会自动退出"
  189.         fi
  190. else
  191.         echo "Checking if tomcat is started"
  192.         if [ -n "$tomcat_pid" ];then
  193.                 kill -9 $tomcat_pid
  194.                 if [ $? -ne 0 ];then
  195.                         echo "tomcat关闭失败,将会自动退出"
  196.                         exit 2
  197.                 fi
  198.         fi
  199.         cp $updata_path/$files $webapp_path
  200.         nohup $tomcat_run/startup.sh &
  201.         sleep 5
  202.         echo "显示tomcat的pid"
  203.         echo "`ps -ef | grep tomcat | grep -v grep | awk '{print $2}'`"
  204.         echo "tomcat startup"
  205.         echo "请手动查看tomcat日志。脚本将会自动退出"
  206. fi
  207. [root@java-server ~]# chmod +x /opt/script/app-jenkins.sh
  208. ###### 12.6 Jenkins pipeline代码
  209. node {
  210.     def gitUrl = "git@gitee.com:youngfit/easy-springmvc-maven.git"
  211.     def git_Branch = 'master'
  212.     value = "=========================================================="
  213.     stage ('拉取代码') {
  214.         print "${value}"
  215.         git branch: "${git_Branch}", url: "${gitUrl}", credentialsId: "1"
  216.     }
  217.     stage ('编译打包') {
  218.         print "${value}"
  219.         sh 'mvn clean package'
  220.     }
  221.     stage ('scp发布到tomcat后端服务') {
  222.         print '${values}'
  223.         sh 'ansible-playbook /etc/ansible/jenknis_scp.yaml'
  224.         // sh 'sleep 40'
  225.         sh 'ansible java-server1 -m shell -a "/opt/script/app-jenkins.sh"'
  226.     }
  227. }

  228. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831564628-62e90060-1573-4bda-8668-7dba94e09a76.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u3df40203&margin=%5Bobject%20Object%5D&name=image.png&originHeight=775&originWidth=1773&originalType=url&ratio=1&rotation=0&showTitle=false&size=87548&status=done&style=none&taskId=u11180468-e1f1-40f7-956e-b0e99edd6de&title=)
  229. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831564624-31279795-28e8-4b51-9ca1-52c2e63343b9.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ua871039c&margin=%5Bobject%20Object%5D&name=image.png&originHeight=753&originWidth=1060&originalType=url&ratio=1&rotation=0&showTitle=false&size=42307&status=done&style=none&taskId=uc0f69a09-f874-4d8e-97e5-a99a3df4868&title=)

  230. 来到Jenkins服务器上查看:
  231. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831564601-3f88b523-03ab-4007-9b49-01109f4dfa86.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ue5f39306&margin=%5Bobject%20Object%5D&name=image.png&originHeight=389&originWidth=1514&originalType=url&ratio=1&rotation=0&showTitle=false&size=49788&status=done&style=none&taskId=ub23564a5-3881-425d-a642-1876bd13e74&title=)
  232. 来到java后端服务器上查看:
  233. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831564626-d6cb02ed-1742-4010-9505-2e3d3a6c8380.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=ubcd10c3d&margin=%5Bobject%20Object%5D&name=image.png&originHeight=359&originWidth=1662&originalType=url&ratio=1&rotation=0&showTitle=false&size=86277&status=done&style=none&taskId=ude9b20ea-6ed1-41b3-be95-39f059ce981&title=)
  234. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831564609-10d00e74-b423-4695-a550-a8ae937e523b.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u79d9cd87&margin=%5Bobject%20Object%5D&name=image.png&originHeight=272&originWidth=930&originalType=url&ratio=1&rotation=0&showTitle=false&size=35286&status=done&style=none&taskId=u500873fe-1dc9-44c4-a79f-b5db261f7c0&title=)
  235. ###### 12.7 结合git tag使用
  236. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831565613-9e9b33ef-9d46-4558-836a-601858c861b6.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uab56331b&margin=%5Bobject%20Object%5D&name=image.png&originHeight=884&originWidth=1357&originalType=url&ratio=1&rotation=0&showTitle=false&size=78174&status=done&style=none&taskId=uae95f4f5-baf0-4cee-98cb-a0d740f9361&title=)
  237. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831565607-b98fb541-2701-4169-a7fe-376fb447091b.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uf612b044&margin=%5Bobject%20Object%5D&name=image.png&originHeight=803&originWidth=1182&originalType=url&ratio=1&rotation=0&showTitle=false&size=54218&status=done&style=none&taskId=uedc5d0d0-199d-4173-af6e-146d6a7bc8f&title=)
  238. 更新代码
  239. [root@git-client easy-springmvc-maven]# touch a.txt
  240. [root@git-client easy-springmvc-maven]# git add *
  241. [root@git-client easy-springmvc-maven]# git commit -m "add a.txt"
  242. [master 80210ab] add a.txt
  243.  1 file changed, 0 insertions(+), 0 deletions(-)
  244.  create mode 100644 a.txt
  245. [root@git-client easy-springmvc-maven]# git tag -a v1.1 -m "v1.1"
  246. [root@git-client easy-springmvc-maven]# git push origin v1.1
  247. Counting objects: 5, done.
  248. Delta compression using up to 2 threads.
  249. Compressing objects: 100% (3/3), done.
  250. Writing objects: 100% (4/4), 439 bytes | 0 bytes/s, done.
  251. Total 4 (delta 0), reused 0 (delta 0)
  252. remote: Powered by GITEE.COM [GNK-6.1]
  253. To git@gitee.com:youngfit/easy-springmvc-maven.git
  254.  * [new tag]         v1.1 -> v1.1

  255. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831565691-aadbf9a1-0acf-4f53-98b6-b03f027995d4.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uafac2d12&margin=%5Bobject%20Object%5D&name=image.png&originHeight=647&originWidth=1139&originalType=url&ratio=1&rotation=0&showTitle=false&size=69815&status=done&style=none&taskId=u3317bbc4-c29e-4c1f-a7f7-770d43c9c00&title=)
    再次拉取,就会有tag
  256. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/23214851/1649831566053-85a831b6-3f89-4924-9cda-d5a44ab18f9c.png#clientId=ub47523fb-7fa4-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u0c41752e&margin=%5Bobject%20Object%5D&name=image.png&originHeight=495&originWidth=795&originalType=url&ratio=1&rotation=0&showTitle=false&size=52494&status=done&style=none&taskId=u3fc4d312-21f2-4182-b44a-81ac87b3588&title=)
  257. node {
  258.     gitee_url = "git@gitee.com:youngfit/easy-springmvc-maven.git"
  259.     value = "=========================================================="
  260.     stage('拉取代码'){
  261.         print "${value}"
  262.         git branch: "master", url: "${gitee_url}", credentialsId: "250"
  263.         print "${tag}"
  264.         sh "git checkout ${tag}"
  265.     }
  266.     stage('编译打包'){
  267.         print "${value}"
  268.         sh "mvn clean package"
  269.     }
  270.     stage('发布启动tomcat'){
  271.         sh "ansible-playbook /etc/ansible/jenknis_scp.yaml"
  272.         sh 'ansible java-server1 -m shell -a "nohup /opt/script/app-jenkins.sh &"'
  273.     }
  274. }