jenkins学习笔记-6-部署

来源:互联网 发布:ug编程安全几何体 编辑:程序博客网 时间:2024/05/18 00:00

部署

最基本的持续交付管道至少需要在Jenkinsfile文件中定义三个阶段:构建,测试和部署。

第一部分我们将主要关注部署阶段,但是应该注意,稳定的构建和测试阶段是部署动作的重要的前置条件。

Jenkinsfile(Declarative Pipeline)

pipeline{

         agentany

         stages{

         stage(‘Build’){

         steps{

         echo ‘Building’

}

}

stage(‘Test’){

         steps{

         echo ‘Testing’

}

}

stage(‘Deploy’){

         steps{

         echo ‘Deploying’

}

}

}

}

 

Toggle ScriptedPipeline (高级)

Jenkinsfile (Scripted Pipeline)

node {

    stage('Build') {

        echo'Building'

    }

    stage('Test') {

        echo'Testing'

    }

    stage('Deploy') {

        echo'Deploying'

    }

}

 

 

阶段作为部署环境

一种通用的模式是扩展各个阶段以捕获额外的部署环境,就像下面展示的:“staging”或“production”

 

stage(‘Deploy - Staging’){

         steps{

         sh ‘./deploy staging’

         sh‘./run-smoke-tests’

}

}

stage(‘Deploy - Production’){

         steps{

         sh ‘./deploy production’

}

}

 

在这个例子中,我们假设 “smoke tests” 是被‘./run-smoke-tests’脚本运行的。这个脚本可以验证版本发布到生产环境中的是不是合格。这种自动部署代码到生产环境的管道可以被看做是“持续部署”的是实现。但这是理想状态,对许多人而言,有充分理由认为持续部署并不实用,但是他们仍然能够从持续部署中受益。这两者不相矛盾。

 

请求人工输入以继续进行

在两个阶段尤其是两个环境阶段之间,你可能需要人工输入之后再继续。例如,判断应用是否处于足够好的能够推送到生产环境的状态。这个可以通过输入(input)步骤来完成。下面的例子,

“完整性检查”(Sanity check)阶段实际上为等待输入而阻塞了,没有人工确认前进是不会继续进行的。

Jenkinsfile(Declarative Pipeline)

pipeline{

         agentany

         stages{

                   /**“构建”(build)和“测试” test阶段省略

         stage(‘Deploy - Staging’){

         steps{

         sh ‘./deploy staging’

         sh ‘./run-smoke-tests’

}

}

stage(‘Sanity check’){

         steps{

         input “Does thestaging environment look ok?”

}

}

 

stage(‘Deploy - Production’){

         steps{

         sh ‘./deployproduction’

}

}

}

}

 

Toggle ScriptedPipeline (高级)

Jenkinsfile (Scripted Pipeline)

node {

   /* "Build" and "Test" stages omitted*/

 

    stage('Deploy - Staging') {

        sh'./deploy staging'

        sh'./run-smoke-tests'

    }

 

    stage('Sanity check') {

        input"Does the staging environment look ok?"

    }

 

    stage('Deploy - Production') {

        sh'./deploy production'

    }

}

 

 

总结

这个小指导只是引导你初步使用jenkins和jenkis的管道。因为它是可以完全扩展的,jenkis可以被改变和配置来处理几乎自动化的任何方面。要了解jenkins可以做哪些更多的内容,查看“用户手册”, jenkis的博客,了解更多的最新事件,教程和更新。

原创粉丝点击