jenkins回滚

作者: 小李飞刀_lql | 来源:发表于2021-11-17 06:23 被阅读0次

回滚流程

1635046045645.png

版本手工设置

#宿主机的/opt/jenkins_home和容器的/var/jenkins_home是持久化映射
[root@bogon ~]# cd /opt/jenkins_home/
[root@bogon jenkins_home]# vi backup_version
abc=1,2,3

回滚版本设置

1635047025482.png 1635050858799.png
parameters {
  extendedChoice description: '请选择要回滚的版本', multiSelectDelimiter: ',', name: 'RollbackVersion', propertyFile: '/var/jenkins_home/backup_version', propertyKey: 'abc', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_SINGLE_SELECT', visibleItemCount: 5
}

回滚版本效果

1635047729109.png

按版本号排序并取5个最近版本

[root@bogon backup]# ls /data/backup/ |sort -t - -k 6 -nr
demo-ROOT-2021-10-24-17.war
demo-ROOT-2021-10-24-16.war
demo-ROOT-2021-10-23-15.war
[root@bogon backup]# ls /data/backup/ |sort -t - -k 6 -nr |awk 'BEGIN{printf "abc="}{printf $0","}'
abc=demo-ROOT-2021-10-24-17.war,demo-ROOT-2021-10-24-16.war,demo-ROOT-2021-10-23-15.war,

[root@bogon backup]# ls /data/backup/ |sort -t - -k 6 -nr |head -n5 |awk 'BEGIN{printf "abc="}{printf $0","}' > /tmp/backup_version
[root@bogon backup]# cat /tmp/backup_version 
abc=demo-ROOT-2021-10-24-17.war,demo-ROOT-2021-10-24-16.war,demo-ROOT-2021-10-23-15.war,

将生成的版本号远程发送到jekins服务器

sshpass 工具需要安装,ansible自带

#sshpass 工具需要安装,ansible自带
[root@gitlab backup]# yum install sshpass

[root@bogon backup]# ls /data/backup/ |sort -t - -k 6 -nr |head -n5 |awk 'BEGIN{printf "abc="}{printf \$0","}' > /tmp/backup_version
[root@bogon backup]#sshpass -p"root" scp  -o StrictHostKeyChecking=no /tmp/backup_version root@192.168.153.19:/opt/jenkins_home/

java项目完整脚本

pipeline {
  agent {
    label 'web1'
  }
  parameters {
    gitParameter branch: '', branchFilter: '.*', defaultValue: 'master', description: '请选择要发布的分支名称', name: 'selectBranch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH'
    choice choices: ['webservers1', 'webservers2', 'webservers3'], description: '灰度发布策略,分批次部署', name: 'Servers'
  } 
  environment { 
    gitlab_address = "http://192.168.153.18/test/demo.git"
    gitlab_auth = "711e362f-2255-4c8c-bb4a-a81353e2ee29"
    gitlab_ssh_auth = "6665ff79-b7c8-4bbc-aff3-85c52eb465fa"
  }
  stages {
    stage('拉取代码') {
      steps {
        checkout([$class: 'GitSCM', branches: [[name: "${params.selectBranch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "$gitlab_auth", url: "$gitlab_address"]]])
      }
    }
    stage('编译构建') {
      steps {
        sh 'mvn clean package -Dmaven.test.skip=true'
      }
    }
    stage('部署') {
      steps {
        withCredentials([usernamePassword(credentialsId: '711e362f-2255-4c8c-bb4a-a81353e2ee29', passwordVariable: 'password', usernameVariable: 'username')]) {
        sh """
cat > /opt/jenkins_home/.hosts << EOF
[webservers1]
192.168.153.17
192.168.153.18
[webservers2]
192.168.153.17
192.168.153.18
[webservers3]
192.168.153.17
192.168.153.18
EOF
cat > /opt/jenkins_home/.playbook.yaml << "EOF"
- hosts: $Servers
  gather_facts: no
  vars:
    workspace: $WORKSPACE
    build_number: $BUILD_NUMBER
    tomcat_dir: "/usr/local/tomcat"
    backup_dir: "/data/backup"
    backup_filename: "demo-ROOT-\$(date +%F)-{{build_number}}.war"
  tasks:
  - name: 推送部署包到远程服务器
    copy: src="{{ item }}" dest={{tomcat_dir}}/webapps
    with_fileglob:
      - "{{workspace}}/target/*.war"
  - name: 部署新程序并重启Tomcat
    shell: |
      cd {{tomcat_dir}}/webapps
      mv ROOT.war {{backup_dir}}/{{backup_filename}}
      mv *.war ROOT.war
      pid=\$(ps -ef |grep {{tomcat_dir}} |egrep -v 'grep' |awk '{print \$2}')
      [ -n "\$pid" ] && kill -9 \$pid
      export JAVA_HOME=/usr/local/jdk
      nohup {{tomcat_dir}}/bin/startup.sh
      ls /data/backup/ |sort -t - -k 6 -nr |head -n5 |awk 'BEGIN{printf "abc="}{printf \$0","}' > /tmp/backup_version
      sshpass -p"root" scp  -o StrictHostKeyChecking=no /tmp/backup_version root@192.168.153.19:/opt/jenkins_home/
EOF
        """
        }
        ansiblePlaybook(
            playbook: '/opt/jenkins_home/.playbook.yaml',
            inventory: '/opt/jenkins_home/.hosts',
            credentialsId: "${gitlab_ssh_auth}"
        )
      }
    }
  }
}

rollback项目的完整脚本

pipeline {
  agent {
    label 'web1'
  }
  parameters {
    extendedChoice description: '请选择要回滚的版本', multiSelectDelimiter: ',', name: 'RollbackVersion', propertyFile: '/var/jenkins_home/backup_version', propertyKey: 'abc', quoteValue: false, saveJSONParameterToFile: false, type: 'PT_SINGLE_SELECT', visibleItemCount: 5
    choice choices: ['webservers1', 'webservers2', 'webservers3'], description: '灰度发布策略,分批次部署', name: 'Servers'
  } 
  environment { 
    gitlab_ssh_auth = "6665ff79-b7c8-4bbc-aff3-85c52eb465fa"
  }
  stages {
    stage('部署') {
      steps {

        sh """
cat > /opt/jenkins_home/.hosts << EOF
[webservers1]
192.168.153.17
192.168.153.18
[webservers2]
192.168.153.17
[webservers3]
192.168.153.18
EOF
cat > /opt/jenkins_home/.playbookrollback.yaml << "EOF"
- hosts: $Servers
  gather_facts: no
  vars:
    tomcat_dir: "/usr/local/tomcat"
    backup_dir: "/data/backup"
    backup_filename: $RollbackVersion
  tasks:
  - name: 恢复备份程序并重启tomcat
    shell: |
      cd {{tomcat_dir}}/webapps
      [! -d {{backup_dir}}] && mkdir -p {{backup_dir}}
      rm -rf ROOT*
      mv {{backup_dir}}/{{backup_filename}} ROOT.war
      pid=\$(ps -ef |grep {{tomcat_dir}} |egrep -v 'grep' |awk '{print \$2}')
      [ -n "\$pid" ] && kill -9 \$pid
      export JAVA_HOME=/usr/local/jdk
      nohup {{tomcat_dir}}/bin/startup.sh
EOF
        """

        ansiblePlaybook(
            playbook: '/opt/jenkins_home/.playbookrollback.yaml',
            inventory: '/opt/jenkins_home/.hosts',
            credentialsId: "${gitlab_ssh_auth}"
        )
      }
    }
  }
}

rollback项目发布效果

1635059399504.png

相关文章

  • jenkins回滚

    回滚流程 版本手工设置 回滚版本设置 回滚版本效果 按版本号排序并取5个最近版本 将生成的版本号远程发送到jeki...

  • Jenkins的回滚

    jenkins 基于上次构建结果回滚方式思路:设置构建参数,脚本根据参数执行,拉取代码后压缩为项目名称+构建号的...

  • Jenkins 备份回滚实现

    准备 Jenkins 插件Active choicesConditional BuildStep 实现 参数化构建...

  • 授之以渔-运维平台发布模块三(Jenkins篇)

    接上篇《授之以渔-运维平台发布模块二(Jenkins篇)》,今天介绍下项目的回滚机制。 我的平台用到Jenkins...

  • Jenkins 构建及回滚任务

    以 golang 构建项目为例。 新建任务 输入一个任务名称 选择:构建一个自由风格的软件项目 可选:也可以复制已...

  • jenkins上线打包以及回滚

    配合参数化构建过程 丢弃构建 配置git 写shell脚本

  • 常用脚本汇总

    java启动脚本命令示例: sh service.sh restart jenkins发布回滚脚本该脚本内部启...

  • Jenkins在windows环境发布,回滚

    安装配置Jenkins:在windows下安装Jenkins,并使用Jenkins打包,然后部署jar包到服务器 ...

  • Git - 回滚

    回滚命令 场景回滚某个commit回滚到某个commit恢复到与HEAD一致回滚Merge回滚最新的Merge回滚...

  • 回滚

    努力了一段时间,突然因为一件不如意的事情发生,荒废一整天。然而调整的时间又要一段时间。 今天就是如此,由于本周末家...

网友评论

    本文标题:jenkins回滚

    本文链接:https://www.haomeiwen.com/subject/nothtrtx.html