美文网首页
Jenkins pipeline 从零开始

Jenkins pipeline 从零开始

作者: 九天之鹤 | 来源:发表于2019-03-27 19:04 被阅读0次

    前言

    一次服务维护中重启了jenkins服务,然后发现服务部署一团乱,排查原因到jenkins配置时发现,jenkins在重启之后配置丢失。如下图

    丢失配置位置

    具体作用:

    Do not build if only specified paths have changed : 发生变更时特定目录不进行编译

    Invert ignore?  :反转,选中后效果:特定目录发生变更时进行编译,这样我们就实现了在提交一个子项目的时候只部署该子项目

    尝试解决方案:

    还原老jenkins版本、升级jenkins版本至最新,均为解决。

    暂定解决方案:

    尝试jenkins pipeline

    一、创建新的gitlab项目,用于pipeline测试

    check out 代码并新建 Jenkinsfile文件并录入最简单的pipeline脚本

    pipeline {

        agent any

        stages {

            stage('Build') {

                steps {

                    echo 'Building..'

                }

            }

            stage('Test') {

                steps {

                    echo 'Testing..'

                }

            }

            stage('Deploy') {

                steps {

                    echo 'Deploying....'

                }

            }

        }

    }

    提交到git仓库

    现在测试用的git项目准备好了。接下来配置jenkins

    二、创建新的jenkins任务

    新建任务,选择流水线(pipeline)这里显示中文有点low,大家表示理解

    勾选 Gitlab webhook,选择只在 Push Events 触发,下方的Allowed branches 点击 Advance 选择 Filter branches by regex,通过正则匹配master分支。

    注:因为新版gitlab在接受merge request后会再次触发push event,在配置jenkins的时候就不需要勾选 accept merge request 选项。如果是旧版gitlab是需要勾选的。

    注意:这里的正则 .*master 和下面配置pipeline选项里的 Branches to build 的正则 */master 不一样。

    回到gitlab配置webhook,这里也只选择 Push events即可。

    重点来了 pipeline设置选择 Pipeline script from SCM

    配置git仓库,下方Script Path中默认填写Jenkinsfile,会使用git项目中的Jenkinsfile执行,我们之前已经创建了。也可以自定义文件名,这里我们使用默认文件名Jenkinsfile。

    点击save保存,这样jenkins就配置好了

    三、执行Jenkins任务

    是不是迫不及待了,点击jenkins里的 立即构建 ,开始了。

    这里会显示pipeline里的每个步骤执行的时间及结果,绿色表示通过。

    点击左侧 Build History 里的小球球,查看log。上边一大段check git 代码信息,这里忽略。重点看我们各阶段的输出:Building..、Testing..、Deploying....,最后的 Finished: SUCCESS。

    First time build. Skipping changelog.

    [Pipeline] }

    [Pipeline] // stage

    [Pipeline] withEnv

    [Pipeline] {

    [Pipeline] stage

    [Pipeline] { (Build)

    [Pipeline] echo

    Building..

    [Pipeline] }

    [Pipeline] // stage

    [Pipeline] stage

    [Pipeline] { (Test)

    [Pipeline] echo

    Testing..

    [Pipeline] }

    [Pipeline] // stage

    [Pipeline] stage

    [Pipeline] { (Deploy)

    [Pipeline] echo

    Deploying....

    [Pipeline] }

    [Pipeline] // stage

    [Pipeline] }

    [Pipeline] // withEnv

    [Pipeline] }

    [Pipeline] // node

    [Pipeline] End of Pipeline

    Finished: SUCCESS

    结束语

    到这里,jenkins上的配置就已经完成了。接下来我们来学习 pipeline 的概念和不断的完善 Jenkinsfile。

    相关文章

      网友评论

          本文标题:Jenkins pipeline 从零开始

          本文链接:https://www.haomeiwen.com/subject/rxjpiftx.html