DevOps

jenkins回滚

2021-11-17  本文已影响0人  小李飞刀_lql

回滚流程

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
上一篇下一篇

猜你喜欢

热点阅读