jenkins流水线部署docker容器

def getHost(){
    def remote = [:]
    remote.name = 'test'
    remote.host = '1.1.1.1'
    remote.user = 'root'
    remote.password = 'password'
    remote.allowAnyHosts = true
    return remote
}
pipeline {
    agent any
    stages {
        stage('get host') {
            steps {
                script {
                    server = getHost()
                }
            }
        }
        stage('Remote SSH') {
            steps {
                sshCommand remote: server, command: "ls -lrt"
                sshCommand remote: server, command: "for i in {1..5}; do echo -n \"Loop \$i \"; date ; sleep 1; done"      
            }
        }
        stage('checkout') {
            steps {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'https://gitee.com/dl88250/solo.git']]])
            }
        }
        stage('docker build') {
            steps {
                script {
                    sh 'docker build -t $(hostname -I | awk \'{print $1}\')/test/solo:$tag ./'
                }
            }
        }
        stage('push library') {
            steps {
                script {
                    sh 'docker login $(hostname -I | awk \'{print $1}\') -u admin -p Harbor12345'
                    sh 'docker push $(hostname -I | awk \'{print $1}\')/test/solo:$tag'
                }
            }
        }
        stage('deploy') {
            steps {
                sshCommand remote: server, command: "docker login -u admin -p Harbor12345 134.175.85.103"
                sshCommand remote: server, command: "docker run -itd --name mysolo 134.175.85.103/test/solo:$tag"
            }
        }
        stage('test') {
            steps {
                echo "testing"
            }
        }
    }
}

 

posted @ 2021-05-17 23:18  力王7314  阅读(399)  评论(0编辑  收藏  举报