Jenkins Pipeline 带webhook推送

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

node() {
    def ip_list = ["10.10.10.11"]
    def app_name = "enterprise-ebank-biz"
    stage('Download code'){
        checkout([$class: 'GitSCM', branches: [[name: "${params.tag}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '57416da9-4867-474b-ba70-8826449f4990', url: 'http://192.168.138.192/byb/enterprise-ebank-service.git']]])
	}
    
sh "curl --location --request POST 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d94ecea4-42d6-4197-baae-e9cb66ba20a5' --header 'Content-Type: application/json' --data '{"msgtype": "markdown","markdown": {"content": "环境<font color= \"warning\"  >${params.tag}</font>,正在发布,请相关同学注意。\n >服务名:<font color=\"comment\">${app_name}</font> \n>进度:<font color=\"comment\">发布中</font> \n>ip:<font color=\"comment\">${ip_list}</font>"}}'"

    stage('compile'){
        // 编译失败自动退出
        sh '/data/jenkins/tools/hudson.tasks.Maven_MavenInstallation/3.5.0/bin/mvn clean deploy -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}/${app_name}/target/${app_name}-1.0.0-SNAPSHOT.jar dest=/data/app/${app_name}/${app_name}.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/${app_name}/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/${app_name}/scripts/start.sh' -u user01"
            }
        }
        sh "curl --location --request POST 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d94ecea4-42d6-4197-baae-e9cb66ba20a5' --header 'Content-Type: application/json' --data '{"msgtype": "markdown","markdown": {"content": "环境<font color= \"warning\"  >test</font>,发布完成。\n >服务名:<font color=\"comment\">enterprise-ebank-service</font> \n>进度:<font color=\"comment\">发布完成</font> \n>ip:<font color=\"comment\">${ip_list}</font>"}}'"

        println "发布" + ip  + "成功" 
    }
    
        
} 

  

原文地址:https://www.cnblogs.com/NGU-PX/p/14188254.html