设为首页 收藏本站
查看: 1515|回复: 0

[经验分享] jenkins-使用Jenkinsfile来定义pipeline

[复制链接]

尚未签到

发表于 2018-9-3 13:13:56 | 显示全部楼层 |阅读模式
  
一、概念
  
1、什么是 pipeline
  
Jenkins Pipeline is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins. Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines "as code" via the Pipeline DSL
  

  
pipeline 是 jenkins 的一套插件,用于定义 CD 流程,弹性,可管理。
  

  

  
2、什么是 Jenkinsfile
  
通过代码的方式来管理 pipeline
  
这样一来,可以通过版本控制系统来管理。
  

  

  
3、Jenkinsfile 的内容示例
  
---------------------------------------------------------------------------------------
  
~]# cat Jenkinsfile
  
#!/usr/bin/env groovy
  

  
pipeline {
  
    agent any
  

  
    stages {
  
        stage('Prepare') {
  
            steps {
  
                echo 'Preparing..'
  
                echo "[+] --> Job: ${env.BUILD_URL}"
  

  
            }
  
        }
  
        stage('Build') {
  
            steps {
  
                echo 'Building..'
  
                sh 'xxx.sh build'
  
            }
  
        }
  

  
        stage('Test') {
  
            steps {
  
                echo 'Testing..'
  
                sh 'xxx.sh test'
  
            }
  
        }
  

  
        stage('Deploy') {
  
            when {
  
              expression {
  
                currentBuild.result == null || currentBuild.result == 'SUCCESS'
  
              }
  
            }
  
            steps {
  
                echo 'Deploying....'
  
                sh 'xxx push'
  
            }
  
        }
  
    }
  

  
    post {
  
        always {
  
            echo 'Sending....'
  
            emailext attachLog: true, body: '$DEFAULT_CONTENT', recipientProviders: [[$class: 'DevelopersRecipientProvider']], subject: '$DEFAULT_SUBJECT'
  
        }
  
    }
  
}
  
---------------------------------------------------------------------------------------
  

  

  
4、Blue Ocean
  
Blue Ocean rethinks the user experience of Jenkins. Designed from the ground up for Jenkins Pipeline, but still compatible with Freestyle jobs, Blue Ocean reduces clutter and increases clarity for every member of the team.
  

  
Blue Ocean 是 jenkins 为了增强用户体验放出来的一个新的 UI 插件。
  

  

  

  
二、如何工作
  
1、安装插件
  
在插件管理页面,搜搜 pipeline 和 Blue Ocean 的关键词来安装你想要的插件。
  

  
2、创建一个任务
  
选择菜单:“Jenkins-新建”
  
------------------------------------------------------------------------------
  
    Item名称:                test_pipeline
  
    (勾选)Pipeline
  

  
确定
  

  

  
【General】
  

  
    项目名称:                test_pipeline
  

  

  

  

  
    参数化构建过程:
  
            String parameter
  
                        名字: key01
  
                        默认值: default_not_exist
  
            String parameter
  
                        名字: key02
  
                        默认值: default_not_exist
  

  

  
【Build Triggers】
  

  
    触发远程构建 (例如,使用脚本)
  
        身份验证令牌:test_build_token
  

  

  
【Pipeline】
  
Definition:         Pipeline script from SCM (下拉菜单选择)
  
            SCM:        Subvision
  
                            Modules:
  
                                        Repository URL: xxx
  
                                        Credentials: xxx
  
            Script Path: Jenkinsfile
  

  

  
保存
  
------------------------------------------------------------------------------
  

  

  
3、打开上述任务页面,有如下菜单
  
Back to Dashboard
  
Status
  
Changes
  
Build with Parameters
  
删除 Pipeline
  
配置
  
Open Blue Ocean
  
Full Stage View
  
Embeddable Build Status
  
Pipeline Syntax
  

  

  
其中,
  
选择: Open Blue Ocean
  
结果:新版本的 UI 入口。
  

  
选择: Pipeline Syntax
  
结果:编写 Jenkinsfile 的帮助文档(咱们重点查看这个)
  
进入上述页面,有一个菜单:
  
Back
  
Snippet Generator
  
Step Reference
  
Global Variables Reference
  
Online Documentation
  
IntelliJ IDEA GDSL
  

  
【 重点1: Snippet Generator 】
  
通过这个工具,可以自动的生成你想要的 Pipeline Script 实例,快去试试吧。(上一节提到的 Jenkinsfile 的内容示例中,最后一段发邮件的内容,我百思不得其解,后来就是这样轻易得到代码的。)
  
This Snippet Generator will help you learn the Pipeline Script code which can be used to define various steps. Pick a step you are interested in from the list, configure it, click Generate Pipeline Script, and you will see a Pipeline Script statement that would call the step with that configuration. You may copy and paste the whole statement into your script, or pick up just the options you care about. (Most parameters are optional and can be omitted in your script, leaving them at default values.)
  

  
【 重点2: Global Variables Reference 】
  
你肯定会想要用到一些全局变量,参数,那么请到这里找找文档吧。(有点遗憾的是 svn 相关的变量没找到,希望你用的是 git 来管理代码)
  

  

  

  
4、提交到代码仓库,触发构建后观察页面。
  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  

  
ZYXW、参考
  
1、Getting Started with Pipeline
  
https://jenkins.io/doc/book/pipeline/getting-started/



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-562081-1-1.html 上篇帖子: gitlab+jenkins+maven+docker持续集成(十一)——sonarqube及sonarscanner代码审查 下篇帖子: 【Jenkins系列】Jenkins详细介绍
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表