Jenkins 将deploy stage抽离为共享库

1,257次阅读
没有评论

共计 3766 个字符,预计需要花费 10 分钟才能阅读完成。

Jenkins 将deploy stage抽离为共享库

以往博主在部署springboot项目时,用的jar包方式启动,大致只需要经过以下stage

  • 拉取代码
  • mvn构建
  • 单元测试
  • 部署jar包
  • 通知

pipeline类似如下

pipeline {
    agent any
    environment {
        branch = 'master'
        scmUrl = 'ssh://git@myScmServer.com/repos/myRepo.git'
        serverPort = '8080'
        developmentServer = 'dev-myproject.mycompany.com'
        // javaopt = ''
    }
    stages {
        stage('checkout git') {
            steps {
                git branch: branch, credentialsId: 'GitCredentials', url: scmUrl
            }
        }

        stage('build') {
            steps {
                sh 'mvn clean package -DskipTests=true'
            }
        }

        stage ('test') {
            steps {
                parallel (
                    "unit tests": { sh 'mvn test' },
                    "integration tests": { sh 'mvn integration-test' }
                )
            }
        }

        stage('deploy development'){
            steps {
                // 通过Spring Boot Actuator来停止服务
                httpRequest httpMode: 'POST', url: "http://${developmentServer}:${serverPort}/shutdown", validResponseCodes: '200,408'
                sshagent(['RemoteCredentials']) {
                    sh "scp target/*.jar root@${developmentServer}:/opt/jenkins-demo-${BUILD_ID}.jar"
                    sh "ssh root@${developmentServer} nohup java -Dserver.port=${serverPort} -jar /opt/jenkins-demo-${BUILD_ID}.jar &"
                }
                retry (3) {
                    sleep 5
                    httpRequest url:"http://${developmentServer}:${serverPort}/health", validResponseCodes: '200', validResponseContent: '"status":"UP"'
                }
            }
        }       
    }
    post {
        failure {
            mail to: 'team@example.com', subject: 'Pipeline failed', body: "${env.BUILD_URL}"
        }
    }
}

看着deploy stage那段代码复用程度很高,得找个方式将其抽离为共享库,后面pipeline只需要提供两个参数serverport即可,所以抽离出来ss部署方式的deploy方法,所以我们的共享库工程如下

mkdir ssh-deploy/{src,vars,resources}
cat >ssh-deploy/vars/deploy.groovy<<-'EOF'
def call(def server, def port) {
    // 通过Spring Boot Actuator来停止服务
    httpRequest httpMode: 'POST', url: "http://${server}:${port}/shutdown", validResponseCodes: '200,408'
    sshagent(['RemoteCredentials']) {
        sh "scp target/*.jar root@${server}:/opt/jenkins-demo-${BUILD_ID}.jar"
        sh "ssh root@${server} nohup java -Dserver.port=${port} -jar /opt/jenkins-demo-${BUILD_ID}.jar &"
    }
    retry (3) {
        sleep 5
        httpRequest url:"http://${server}:${port}/health", validResponseCodes: '200', validResponseContent: '"status":"UP"'
    }
}
EOF

# 将上述代码创建为sshdeploy仓库,并提交到远程仓库中,略
# https://gitlab.xadocker.cn/devops-jenkins-shared-library/ssh-deploy.git

最后博主的pipeline样例如下

library identifier: 'ssh-deploy@master', retriever: modernSCM([
    $class: 'GitSCMSource',
    remote: 'https://github.com/devops-ws/jenkins-shared-library',
    traits: [[$class: 'jenkins.plugins.git.traits.BranchDiscoveryTrait']]
])

pipeline {
    agent any
    environment {
        branch = 'master'
        scmUrl = 'ssh://git@myScmServer.com/repos/myRepo.git'
        serverPort = '8080'
        developmentServer = 'dev-myproject.mycompany.com'
        // javaopt = ''
    }
    stages {
        stage('checkout git') {
            steps {
                git branch: branch, credentialsId: 'GitCredentials', url: scmUrl
            }
        }

        stage('build') {
            steps {
                sh 'mvn clean package -DskipTests=true'
            }
        }

        stage ('test') {
            steps {
                parallel (
                    "unit tests": { sh 'mvn test' },
                    "integration tests": { sh 'mvn integration-test' }
                )
            }
        }

        stage('deploy development'){
            steps {
                deploy(developmentServer, serverPort)
            }
        }       
    }
    post {
        failure {
            mail to: 'team@example.com', subject: 'Pipeline failed', body: "${env.BUILD_URL}"
        }
    }
}

如果单pipeline部署多个环境则可以调整为

library identifier: 'ssh-deploy@master', retriever: modernSCM([
    $class: 'GitSCMSource',
    remote: 'https://github.com/devops-ws/jenkins-shared-library',
    traits: [[$class: 'jenkins.plugins.git.traits.BranchDiscoveryTrait']]
])

pipeline {
    agent any
    environment {
        branch = 'master'
        scmUrl = 'ssh://git@myScmServer.com/repos/myRepo.git'
        serverPort = '8080'
        developmentServer = 'dev-myproject.mycompany.com'
        stagingServer = 'staging-myproject.mycompany.com'
        productionServer = 'production-myproject.mycompany.com'
        // javaOpt = ''
        // jarName = ''
    }
    stages {
        stage('checkout git') {
            steps {
                git branch: branch, credentialsId: 'GitCredentials', url: scmUrl
            }
        }

        stage('build') {
            steps {
                sh 'mvn clean package -DskipTests=true'
            }
        }

        stage ('test') {
            steps {
                parallel (
                    "unit tests": { sh 'mvn test' },
                    "integration tests": { sh 'mvn integration-test' }
                )
            }
        }

        stage('deploy development'){
            steps {
                deploy(developmentServer, serverPort)
            }
        } 
        stage('deploy staging'){
            steps {
                deploy(stagingServer, serverPort)
            }
        }
        stage('deploy production'){
            steps {
                deploy(productionServer, serverPort)
            }
        }      
    }
    post {
        failure {
            mail to: 'team@example.com', subject: 'Pipeline failed', body: "${env.BUILD_URL}"
        }
    }
}

正文完
 
xadocker
版权声明:本站原创文章,由 xadocker 2020-10-27发表,共计3766字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)