Skip to content
SRE运维进阶之路SRE运维进阶之路
github icon
  • Jenkins

    • Jenkins 学习笔记
      • 1 Jenkins 简介

        • 1.1 DevOps、CI、CD都是什么鬼?
          • 1.2 Jenkins简介
          • 2 安装 Jenkins

            • 2.1 yum 安装 jenkins
              • 2.2 war 包安装 jenkins
                • 2.3 使用 ansible 安装 jenkins
                  • 2.4 使用docker安装jenkins
                  • 3 使用 Jenkins

                    • 3.1 Jenkins用户权限管理
                      • 3.2 访问 API
                      • 4 流水线核心语法

                        • 4.1 开始编写Jenkinsfile
                          • 4.2 Jenkins流水线语法
                            • 4.3 Groovy 简明教程
                              • 4.4 Jenkins共享库应用
                                • 4.5 共享库之钉钉消息推送
                                  • 4.6 流水线开发工具
                                    • 4.7 pipeline 生产配置实例
                                      • pipeline配置java项目
                                        • pipeline 配置前端项目
                                          • 局部变量的定义和传递
                                            • exit code, stdout and stderr 返回值和输出
                                            • 参考链接
                                            • 4.8 在 VS Code 中校验 Jenkinsfile
                                            • 5 流水线集成篇

                                              • 5.1 构建发布工具集成
                                                • 5.2 用户认证系统集成
                                                • 6 管理 jenkins

                                                  • 6.1 Jenkins 关闭和重启的实现方式
                                                  • 7 日常维护

                                                    • 7.1 Jenkins Job迁移
                                                    • 8 Jenkins 常见问题
                                                    • Ansible

                                                      • Ansible 学习笔记
                                                        • 1 初识Ansible
                                                          • 2 Ansible Inventory配置详解
                                                            • 3 Ansible Ad-hoc命令集

                                                              • 3.1 Ansible Ad-hoc 常用命令集
                                                                • 3.2 Ansible lineinfile模块详解
                                                                • 4 Ansible Playbook

                                                                  • 4.1 Playbook的结构及handler用法
                                                                    • 4.2 Playbook循环语句
                                                                      • 4.3 Playbook条件语句
                                                                        • 4.4 Playbook高级用法
                                                                          • 4.5 Playbook之tags
                                                                            • 4.6 巧用Roles
                                                                              • 4.7 Jinja2 模板
                                                                                • 4.8 yaml语法
                                                                                • 5 Ansible变量

                                                                                  • 5.1 自定义变量
                                                                                    • 5.2 Fact变量
                                                                                      • 5.3 魔法变量
                                                                                        • 5.4 使用lookup生成变量
                                                                                          • 5.5 使用vault配置加密
                                                                                          • 6 Ansible使用优化
                                                                                            • 7 常见问题
                                                                                              • 8 综合项目

                                                                                                • 使用 ansible 快速安装 k8s 机器

                                                                                              4.7 pipeline 生产配置实例

                                                                                              author iconClaycalendar icon2021年6月2日category icon
                                                                                              • 自动化工具
                                                                                              tag icon
                                                                                              • Jenkins
                                                                                              timer icon大约 3 分钟

                                                                                              此页内容
                                                                                              • pipeline配置java项目
                                                                                              • pipeline 配置前端项目
                                                                                                • 局部变量的定义和传递
                                                                                                • exit code, stdout and stderr 返回值和输出
                                                                                              • 参考链接

                                                                                              # 4.7 pipeline 生产配置实例

                                                                                              # pipeline配置java项目

                                                                                              pipeline {
                                                                                                  agent { label 'slave' }
                                                                                                  options {
                                                                                                      timestamps()
                                                                                                      disableConcurrentBuilds()
                                                                                                      buildDiscarder(
                                                                                                          logRotator(
                                                                                                              numToKeepStr: '20',
                                                                                                              daysToKeepStr: '30',
                                                                                                          )
                                                                                                      )
                                                                                                  }
                                                                                                  parameters {
                                                                                                      choice(
                                                                                                         name: "DEPLOY_FLAG",
                                                                                                         choices: ['deploy', 'rollback'],
                                                                                                         description: "发布/回滚"
                                                                                                      )
                                                                                                  }
                                                                                                  /*=======================================常修改变量-start=======================================*/
                                                                                                  environment {
                                                                                                      gitUrl = "git地址"
                                                                                                      branchName = "分支名称"
                                                                                                      gitlabCredentialsId = "认证凭证"
                                                                                                      projectRunDir = "项目运行目录"
                                                                                                      jobName = "${env.JOB_NAME}"
                                                                                                      serviceName = "服务名称"
                                                                                                      serviceType = "jar"
                                                                                                      runHosts = "192.168.167.xx,192.168.167.xx"
                                                                                                      rollbackVersion = ""
                                                                                                  }
                                                                                                  stages {
                                                                                                      stage('Deploy'){
                                                                                                          when {
                                                                                                              expression { return params.DEPLOY_FLAG == 'deploy' }
                                                                                                          }
                                                                                                          stages {
                                                                                                              stage('Pre Env') {
                                                                                                                  steps {
                                                                                                                      echo "======================================项目名称 = ${env.JOB_NAME}"
                                                                                                                      echo "======================================项目 URL = ${gitUrl}"
                                                                                                                      echo "======================================项目分支 = ${branchName}"
                                                                                                                      echo "======================================当前编译版本号 = ${env.BUILD_NUMBER}"
                                                                                                                  }
                                                                                                              }
                                                                                                              stage('Git Clone') {
                                                                                                                  steps {
                                                                                                                      git branch: "${branchName}",
                                                                                                                      credentialsId: "${gitlabCredentialsId}",
                                                                                                                      url: "${gitUrl}"
                                                                                                                  }
                                                                                                              }
                                                                                                              stage('Mvn Build') {
                                                                                                                  steps {
                                                                                                                      withMaven(jdk: 'jdk1.8', maven: 'maven') {
                                                                                                                          sh "mvn clean package -Dmaven.test.skip=true -U -f ${serviceName}/pom.xml"
                                                                                                                      }
                                                                                                                  }
                                                                                                              }
                                                                                                              stage('Ansible Deploy') {
                                                                                                                  steps{
                                                                                                                      script {
                                                                                                                          sleep 5
                                                                                                                          ansiColor('xterm') {
                                                                                                                              ansiblePlaybook colorized: true, extras: '-e "directory=${projectRunDir}" -e "job=${jobName}" -e "service=${serviceName}" -e "type=${serviceType}"', installation: 'ansible', inventory: '/etc/ansible/hosts.yml', limit: "${runHosts}", playbook: '/etc/ansible/playbook/deploy-jenkins.yml'                            
                                                                                                                          }
                                                                                                                      }
                                                                                                                  }
                                                                                                              }
                                                                                                          }   
                                                                                                      }
                                                                                                      stage('Rollback') {
                                                                                                          when {
                                                                                                              expression { return params.DEPLOY_FLAG == 'rollback' }
                                                                                                          }
                                                                                                          steps{
                                                                                                              script {
                                                                                                                  rollbackVersion = input(
                                                                                                                      message: "请填写要回滚的版本",
                                                                                                                      parameters: [
                                                                                                                          string(name:'last_number')
                                                                                                                      ]
                                                                                                                  )
                                                                                                                  sh """
                                                                                                                      echo "正在回滚至就近第${rollbackVersion}个版本"
                                                                                                                      ansible ${runHosts} -m shell -a "sh ${projectRunDir}/rollback.sh ${rollbackVersion} ${serviceName}"
                                                                                                                  """
                                                                                                              }
                                                                                                          }
                                                                                                      }
                                                                                                  }
                                                                                                  post {
                                                                                                      always {
                                                                                                          deleteDir()
                                                                                                      }
                                                                                                      success {
                                                                                                          echo 'This task is successful!'
                                                                                                      }
                                                                                                  }
                                                                                              }
                                                                                              
                                                                                              1
                                                                                              2
                                                                                              3
                                                                                              4
                                                                                              5
                                                                                              6
                                                                                              7
                                                                                              8
                                                                                              9
                                                                                              10
                                                                                              11
                                                                                              12
                                                                                              13
                                                                                              14
                                                                                              15
                                                                                              16
                                                                                              17
                                                                                              18
                                                                                              19
                                                                                              20
                                                                                              21
                                                                                              22
                                                                                              23
                                                                                              24
                                                                                              25
                                                                                              26
                                                                                              27
                                                                                              28
                                                                                              29
                                                                                              30
                                                                                              31
                                                                                              32
                                                                                              33
                                                                                              34
                                                                                              35
                                                                                              36
                                                                                              37
                                                                                              38
                                                                                              39
                                                                                              40
                                                                                              41
                                                                                              42
                                                                                              43
                                                                                              44
                                                                                              45
                                                                                              46
                                                                                              47
                                                                                              48
                                                                                              49
                                                                                              50
                                                                                              51
                                                                                              52
                                                                                              53
                                                                                              54
                                                                                              55
                                                                                              56
                                                                                              57
                                                                                              58
                                                                                              59
                                                                                              60
                                                                                              61
                                                                                              62
                                                                                              63
                                                                                              64
                                                                                              65
                                                                                              66
                                                                                              67
                                                                                              68
                                                                                              69
                                                                                              70
                                                                                              71
                                                                                              72
                                                                                              73
                                                                                              74
                                                                                              75
                                                                                              76
                                                                                              77
                                                                                              78
                                                                                              79
                                                                                              80
                                                                                              81
                                                                                              82
                                                                                              83
                                                                                              84
                                                                                              85
                                                                                              86
                                                                                              87
                                                                                              88
                                                                                              89
                                                                                              90
                                                                                              91
                                                                                              92
                                                                                              93
                                                                                              94
                                                                                              95
                                                                                              96
                                                                                              97
                                                                                              98
                                                                                              99
                                                                                              100

                                                                                              # pipeline 配置前端项目

                                                                                              vue

                                                                                              pipeline {
                                                                                                  agent { label 'master'}
                                                                                              
                                                                                                  options {
                                                                                                      timestamps()
                                                                                                      disableConcurrentBuilds()
                                                                                                      buildDiscarder(
                                                                                                          logRotator(
                                                                                                              numToKeepStr: '20',
                                                                                                              daysToKeepStr: '30',
                                                                                                          )
                                                                                                      )
                                                                                                  }
                                                                                              
                                                                                                  parameters {
                                                                                                      choice(
                                                                                                         name: "DEPLOY_FLAG",
                                                                                                         choices: ['deploy', 'rollback'],
                                                                                                         description: "发布/回滚"
                                                                                                      )
                                                                                                  }
                                                                                              
                                                                                                  /*=======================================常修改变量-start=======================================*/
                                                                                              
                                                                                                  environment {
                                                                                                      gitUrl = "http://gitlab.schengle.com/driving-frontend/h5.git"
                                                                                                      branchName = "master"
                                                                                                      gitlabCredentialsId = "437c69b5-a874-4da6-8fe1-61e0275fdf0d"
                                                                                                      projectBuildDir = "build"
                                                                                                      projectBuildPath = "${env.WORKSPACE}/${projectBuildDir}/"
                                                                                                      nginxIp = "192.168.16.141"
                                                                                                      nginxHtmlRoot = "/tmp/${env.JOB_NAME}"
                                                                                                      owner = "font"
                                                                                                      group = "font"
                                                                                                      backupRootDir = "/opt/backup"
                                                                                                      backupJob = "${backupRootDir}/${env.JOB_NAME}"
                                                                                                      backupDir = "${backupJob}/${env.BUILD_NUMBER}"
                                                                                                      rollbackVersion = ""
                                                                                                  }
                                                                                              
                                                                                                  /*=======================================常修改变量-end=======================================*/
                                                                                              
                                                                                                  stages {
                                                                                                      stage('Deploy') {
                                                                                                          when {
                                                                                                              expression { return params.DEPLOY_FLAG == 'deploy' }
                                                                                                          }
                                                                                                          stages {
                                                                                                              stage('Pre Env') {
                                                                                                                  steps {
                                                                                                                      echo "======================================项目名称 = ${env.JOB_NAME}"
                                                                                                                      echo "======================================项目 URL = ${gitUrl}"
                                                                                                                      echo "======================================项目分支 = ${branchName}"
                                                                                                                      echo "======================================当前编译版本号 = ${env.BUILD_NUMBER}"
                                                                                                                      echo "======================================项目 Build 文件夹路径 = ${projectBuildPath}"
                                                                                                                      echo "======================================项目 Nginx 的 ROOT 路径 = ${nginxHtmlRoot}"
                                                                                                                  }
                                                                                                              }
                                                                                              
                                                                                                              stage('Git Clone') {
                                                                                                                  steps {
                                                                                                                      git branch: "${branchName}",
                                                                                                                      credentialsId: "${gitlabCredentialsId}",
                                                                                                                      url: "${gitUrl}"
                                                                                                                  } 
                                                                                                              }
                                                                                              
                                                                                                              stage('NPM Install') {
                                                                                                                  steps {
                                                                                                                      nodejs('nodejs') {
                                                                                                                          sh "npm install"
                                                                                                                      }
                                                                                                                  }
                                                                                                              }
                                                                                              
                                                                                                              stage('NPM Build') {
                                                                                                                  steps {
                                                                                                                      nodejs('nodejs') {
                                                                                                                          sh "npm run build"
                                                                                                                      }
                                                                                                                  }
                                                                                                              }
                                                                                              
                                                                                                              stage('Backup') {
                                                                                                                  agent { label 'ansible'}
                                                                                                                  steps {
                                                                                                                      script {
                                                                                                                          try {
                                                                                                                              isItBackupToday = sh (returnStatus: true, script:'ansible ${nginxIp} -m shell -a "ls -l --time-style=+%D ${backupJob} | grep $(date +%D)"')
                                                                                                                              if (isItBackupToday !=0){
                                                                                                                                  try {
                                                                                                                                      sh 'ansible ${nginxIp} -m file -a "path=${backupDir} state=directory owner=${owner} group=${group}"'
                                                                                                                                      sh 'ansible ${nginxIp} -m shell -a "cp -a ${nginxHtmlRoot}/* ${backupDir}"'
                                                                                                                                  }
                                                                                                                                  catch (exc) {
                                                                                                                                      echo 'Something failed!'
                                                                                                                                  }  
                                                                                                                              }
                                                                                                                          }
                                                                                                                          catch (exc) {
                                                                                                                              echo 'Something failed!'
                                                                                                                          }                             
                                                                                                                      }
                                                                                                                  }
                                                                                                              }
                                                                                              
                                                                                                              stage('Nginx Deploy') {
                                                                                                                  agent { label 'ansible'}
                                                                                                                  steps {
                                                                                                                      sh 'ansible ${nginxIp} -m synchronize -a "src=${projectBuildPath} dest=${nginxHtmlRoot} delete=yes"'
                                                                                                                      sh 'ansible ${nginxIp} -m file -a "path=${nginxHtmlRoot} owner=${owner} group=${group} recurse=yes"'
                                                                                                                  }
                                                                                                              }
                                                                                              
                                                                                                              stage('Tar Build') {
                                                                                                                  steps {
                                                                                                                      sh "tar -zcf  ${env.JOB_NAME}.tar.gz ${projectBuildDir}"
                                                                                                                  }
                                                                                                              }
                                                                                              
                                                                                                              stage('Archive Artifacts') {
                                                                                                                  steps {
                                                                                                                      archiveArtifacts "${env.JOB_NAME}.tar.gz"
                                                                                                                  }
                                                                                                              }
                                                                                                          }
                                                                                                      }
                                                                                              
                                                                                                      stage('Rollback') {
                                                                                                          when {
                                                                                                              expression { return params.DEPLOY_FLAG == 'rollback' }
                                                                                                          }
                                                                                                          agent { label 'ansible'}
                                                                                                          steps{
                                                                                                              script {
                                                                                                                  sh 'ansible ${nginxIp} -m shell -a "ls -l  ${backupJob}" | grep -v "CHANGED"'
                                                                                                                  rollbackVersion = input(
                                                                                                                      message: "请填写要回滚的版本",
                                                                                                                      parameters: [
                                                                                                                          string(name:'BUILD_NUMBER')
                                                                                                                      ]
                                                                                                                  )
                                                                                                                  sh 'ansible ${nginxIp} -m file -a "path=${nginxHtmlRoot} state=absent"'
                                                                                                                  sh 'ansible ${nginxIp} -m file -a "path=${nginxHtmlRoot} state=directory owner=${owner} group=${group}"'                    
                                                                                                                  withEnv(["rollbackVersion=${rollbackVersion}"]){
                                                                                                                      sh 'ansible ${nginxIp} -m shell -a "cp -a  ${backupJob}/${rollbackVersion}/* ${nginxHtmlRoot}"'
                                                                                                                  }
                                                                                                              }
                                                                                                          }
                                                                                                      }
                                                                                                  }
                                                                                              }
                                                                                              
                                                                                              1
                                                                                              2
                                                                                              3
                                                                                              4
                                                                                              5
                                                                                              6
                                                                                              7
                                                                                              8
                                                                                              9
                                                                                              10
                                                                                              11
                                                                                              12
                                                                                              13
                                                                                              14
                                                                                              15
                                                                                              16
                                                                                              17
                                                                                              18
                                                                                              19
                                                                                              20
                                                                                              21
                                                                                              22
                                                                                              23
                                                                                              24
                                                                                              25
                                                                                              26
                                                                                              27
                                                                                              28
                                                                                              29
                                                                                              30
                                                                                              31
                                                                                              32
                                                                                              33
                                                                                              34
                                                                                              35
                                                                                              36
                                                                                              37
                                                                                              38
                                                                                              39
                                                                                              40
                                                                                              41
                                                                                              42
                                                                                              43
                                                                                              44
                                                                                              45
                                                                                              46
                                                                                              47
                                                                                              48
                                                                                              49
                                                                                              50
                                                                                              51
                                                                                              52
                                                                                              53
                                                                                              54
                                                                                              55
                                                                                              56
                                                                                              57
                                                                                              58
                                                                                              59
                                                                                              60
                                                                                              61
                                                                                              62
                                                                                              63
                                                                                              64
                                                                                              65
                                                                                              66
                                                                                              67
                                                                                              68
                                                                                              69
                                                                                              70
                                                                                              71
                                                                                              72
                                                                                              73
                                                                                              74
                                                                                              75
                                                                                              76
                                                                                              77
                                                                                              78
                                                                                              79
                                                                                              80
                                                                                              81
                                                                                              82
                                                                                              83
                                                                                              84
                                                                                              85
                                                                                              86
                                                                                              87
                                                                                              88
                                                                                              89
                                                                                              90
                                                                                              91
                                                                                              92
                                                                                              93
                                                                                              94
                                                                                              95
                                                                                              96
                                                                                              97
                                                                                              98
                                                                                              99
                                                                                              100
                                                                                              101
                                                                                              102
                                                                                              103
                                                                                              104
                                                                                              105
                                                                                              106
                                                                                              107
                                                                                              108
                                                                                              109
                                                                                              110
                                                                                              111
                                                                                              112
                                                                                              113
                                                                                              114
                                                                                              115
                                                                                              116
                                                                                              117
                                                                                              118
                                                                                              119
                                                                                              120
                                                                                              121
                                                                                              122
                                                                                              123
                                                                                              124
                                                                                              125
                                                                                              126
                                                                                              127
                                                                                              128
                                                                                              129
                                                                                              130
                                                                                              131
                                                                                              132
                                                                                              133
                                                                                              134
                                                                                              135
                                                                                              136
                                                                                              137
                                                                                              138
                                                                                              139
                                                                                              140
                                                                                              141
                                                                                              142
                                                                                              143
                                                                                              144
                                                                                              145
                                                                                              146
                                                                                              147
                                                                                              148
                                                                                              149
                                                                                              150
                                                                                              151
                                                                                              152

                                                                                              这个jenkinsfile中有保存 制品,

                                                                                              需要注意的是:制品的保存时间,和jenkins丢弃旧的构建参数,相同

                                                                                              # 局部变量的定义和传递

                                                                                              自定义变量(局部)

                                                                                              def username = 'Jenkins'
                                                                                              echo "Hello Mr.${username}"
                                                                                              
                                                                                              1
                                                                                              2

                                                                                              环境变量(局部)

                                                                                              withEnv(['MYTOOL_HOME=/usr/local/mytool']){
                                                                                                  sh '$MYTOOL_HOME/bin/start'
                                                                                              }
                                                                                              
                                                                                              1
                                                                                              2
                                                                                              3

                                                                                              # exit code, stdout and stderr 返回值和输出

                                                                                              其做法是,把stdout定向到一个文件,sh 配置 returnStatus: true,它的返回是一个0或非0的整数,然后从文件读取stdout的内容。stderr同理可得。

                                                                                              def status = sh(returnStatus: true, script: "git merge --no-edit $branches > merge_output.txt")
                                                                                              if (status != 0) {
                                                                                                currentBuild.result = 'FAILED'
                                                                                                def output = readFile('merge_output.txt').trim()
                                                                                                slackSend channel: SLACK_CHANNEL, message: "<${env.JOB_URL}|${env.JOB_NAME}> ran into an error merging the PR branches into the ${TARGET_BRANCH} branch:\n```\n${output}\n```\n<${env.BUILD_URL}/console|See the full output>", color: 'warning', tokenCredentialId: 'slack-token'
                                                                                                error 'Merge conflict'
                                                                                              }
                                                                                              sh 'rm merge_output.txt'
                                                                                              
                                                                                              1
                                                                                              2
                                                                                              3
                                                                                              4
                                                                                              5
                                                                                              6
                                                                                              7
                                                                                              8

                                                                                              # 参考链接

                                                                                              Jenkins 安装和配置| judasn/Linux-Tutorial | githubopen in new window

                                                                                              jenkins pipeline 局部变量定义及传递 | triThirty | csdnopen in new window

                                                                                              Jenkins pipeline 中获取 exit code, stdout and stderr 返回值和输出 | pekkle | 博客园open in new window

                                                                                              edit icon编辑此页open in new window
                                                                                              上次编辑于: 2022/4/27 15:33:00
                                                                                              贡献者: clay-wangzhi
                                                                                              上一页
                                                                                              4.6 流水线开发工具
                                                                                              下一页
                                                                                              4.8 在 VS Code 中校验 Jenkinsfile
                                                                                              备案号:冀ICP备2021007336号
                                                                                              Copyright © 2023 Clay