def getHost(){
    def remote = [:]
    remote.name = 'server02'
    remote.host = '39.19.90'
    remote.user = 'root'
    remote.port = 22
    remote.password = '#########'
    remote.allowAnyHosts = true
    return remote
}
pipeline {
    agent any
    environment{
        def server = ''
    } 
    stages {
        stage('init-server'){
            steps {
                script {                 
                   server = getHost()                                   
                }
            }
        }
        stage('检出') {
            steps {
                sh "cd ~/cobbler.sysconfig/cobbler.identityserver; git pull;"
                git url: 'https://github.com/cysnet/cobbler.identityserver.git', branch: 'master', credentialsId:'86a8ba78-9bb7-4a9b-bb53-3a77224c27ce'
                sh "cp ~/cobbler.sysconfig/cobbler.identityserver/appsettings.json $WORKSPACE/Cobbler.IdentityServer/"
                }
        }
        stage('docker清理') {
            steps {
                script{
                    image_name = sh(script: "docker images -a|grep 'chenyishi/cobbler.identityserver'| awk '{print \$1 }'", returnStdout: true).trim()
                    echo image_name
                    if (image_name!=null&&image_name!=""){
                        sh(script: "docker rmi chenyishi/cobbler.identityserver:1.0 --force")
                    }
                }
               script{
                    container_name = sh(script: "docker ps -a -f name=cobbler.identityserver -q", returnStdout: true).trim()
                    echo container_name
                    if (container_name!=null &&container_name!=""){
                        sh(script: "docker rm --force ${container_name}")
                    }
                }
                script{
                    image_name = sh(script: "docker images -f label=name=cobbler.identityserver -q", returnStdout: true).trim()
                    echo image_name
                    if (image_name!=null&&image_name!=""){
                        sh(script: "docker rmi ${image_name} --force")
                    }
                }
            }
        }
         stage('构建') {
            steps {
                sh "docker build --label name=cobbler.identityserver -t cobbler.identityserver:1.0 .;"
                sh "docker login -u ##### -p #####; docker tag cobbler.identityserver:1.0 chenyishi/cobbler.identityserver:1.0"
            }
        }  
        stage('docker push') {
            steps {
                sh "docker push chenyishi/cobbler.identityserver:1.0"
            }
        } 
        stage('remote server docker run'){
            steps {
                script {
                  sshCommand remote: server, command: """                 
                  container_name=`docker ps -a -f name=cobbler.identityserver -q`;
                  if [ \$container_name ];then docker rm --force \$(docker ps -a -f name=cobbler.identityserver -q); fi;
                  image_name=`docker images -f "label=name=cobbler.identityserver" -q`;
                  if [ \$image_name ];then sudo docker rmi \$(docker images -f "label=name=cobbler.identityserver" -q); fi;
                  docker login -u ##### -p #######; 
                  docker run --name cobbler.identityserver -d -p 5000:5000 chenyishi/cobbler.identityserver:1.0;
                  """
                }
            }
        }
    }
}

 

posted on 2019-05-29 11:56  chester·chen  阅读(329)  评论(0编辑  收藏  举报