Jenkins自动发布Pipeline脚本

properties([
    parameters([
        string(name: 'tag', defaultValue: '', description: 'which tag you want to deploy?')
     
    ])
])

node() {
    def ip_list = ["10.10.10.2","10.10.10.3"]
    def app_name = "mobile-biz-server"

    stage('Download code'){
        checkout([$class: 'GitSCM', branches: [[name: "${params.tag}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '57416da9-4867-474b-ba70-8826449f4990', url: 'http://gitlab.xxx.com/byb/mobile-biz.git']]])
    }

    stage('compile'){
        // 编译失败自动退出
        sh '/data/jenkins/tools/hudson.tasks.Maven_MavenInstallation/3.5.0/bin/mvn deploy -f ../byb-mobile-gateway-test/pom.xml  --update-snapshots -DskipTests=true'
        sh '/data/jenkins/tools/hudson.tasks.Maven_MavenInstallation/3.5.0/bin/mvn clean package -U  -Dmaven.test.skip=true'
    }

    // Loop through all parallel branched stage names
    ip_list.each{ip->

        stage('同步jar包和配置文件') {
            withCredentials([sshUserPrivateKey(credentialsId: '8d3e56e8-57c8-44a3-81e7-30a450310836', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'username')]) {
               sh "ansible  all -i ${ip}, -e ansible_ssh_user=$username -e ansible_ssh_private_key_file=$identity -m file -a 'path=/data/app/${app_name} state=directory owner=user01 group=user01 mode=0755' -u user01" 

               sh "ansible  all -i ${ip}, -e ansible_ssh_user=$username -e ansible_ssh_private_key_file=$identity -m copy -a 'src=${JENKINS_HOME}/workspace/${JOB_NAME}/mobile-biz-server/target/mobile-biz-server-1.0.0-SNAPSHOT.jar dest=/data/app/mobile-biz-server/mobile-biz.jar owner=user01 group=user01 mode=0755 backup=yes' -u user01"
            }
        }

        stage('关闭与启动服务'){
            withCredentials([sshUserPrivateKey(credentialsId: '8d3e56e8-57c8-44a3-81e7-30a450310836', keyFileVariable: 'identity', passphraseVariable: '', usernameVariable: 'username')]) {
               sh "ansible  all -i ${ip}, -e ansible_ssh_user=$username -e ansible_ssh_private_key_file=$identity -m shell -a 'sh /data/app/mobile-biz-server/scripts/stop.sh' -u user01"
               sh "ansible  all -i ${ip}, -e ansible_ssh_user=$username -e ansible_ssh_private_key_file=$identity -m shell -a 'sh /data/app/mobile-biz-server/scripts/start.sh' -u user01"
            }
        }
        println "发布" + ip  + "成功" 
    }
    
}
  
原文地址:https://www.cnblogs.com/NGU-PX/p/14155906.html