jenkins pipline 脚本

def getHost(ip){
   def remote = [:]
    remote.name = 'server-${ip}'
    remote.host = ip
    remote.port = 22
    remote.allowAnyHosts = true
    withCredentials([usernamePassword(credentialsId: '31ed3f9c-3e80-496d-9733-98b78db29128', passwordVariable: 'password', usernameVariable: 'userName')]) {
        remote.user = "${userName}"
        remote.password = "${password}"
    }
    return remote
}

def imageName="10.1.80.126:9001/library/etecsa-server-config-test"
def imageTag="latest"
def prjName="etecsa-server-config-test"
def register="http://10.1.80.126:9001"
pipeline {
    agent any
    
      environment{
        def server = ''
    }   

    
      stages {
              
    stage('init-server'){
            steps {
                script {                 
                   server = getHost("10.1.80.126")                                   
                }
            }
        }
          
        stage('Pre'){
            steps {
              sh label: '', script: """  if [ \$(docker ps -a | grep -i $imageName | wc -l) -eq 1 ]; then 
                         docker stop $prjName
                         docker rm -v -f $prjName
                    fi 
                    if [ \$(docker images | grep -i $imageName | wc -l) -eq 1 ]; then
                         docker rmi -f $imageName
                    fi
                    """
            }
        }
        
        stage('BuildJar') {
         steps {
              echo '拉取代码'
              checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: '8be443f3-8a57-4ce3-9905-1433c9f1b333', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'svn://10.1.80.111/ETECSA-SERVER']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']])
              withMaven(maven: 'maven', mavenSettingsConfig: '62b4b95d-fe93-45f7-8eea-e551823d1b98') {
                  sh label: '', script: 'mvn clean install -pl ETECSA-SERVER-CONFIG-CENTER -am -amd -Dmaven.test.skip=true'
                }
            }
        }
        
        stage('DockerBuild') {
            steps {
                dir ("ETECSA-SERVER-CONFIG-CENTER/") {
                    script {
                        def image = docker.build("$imageName:$imageTag")
                            docker.withRegistry("$register", 'a2609603-66d9-4cfe-9ddf-e8ace35c1279'){
                            image.push()
                        }  
                    }
                }
            }
        }
        
        stage('Run'){
            steps {
                script {
                  sshCommand remote: server, command: """
                   svn checkout svn://10.1.80.111/ETECSA-SERVER/DOCKER-COMPOSE --username zhanghao --password 123456
                   cd DOCKER-COMPOSE
                   docker-compose up -d $prjName
                """
                }
            }
        }
    }
}

你可能感兴趣的:(jenkins pipline 脚本)