「Jenkins Pipeline」- 实践经验、常见问题、代码技巧

问题描述

该笔记将记录:与 Jenkine Pipeline 有关的实践经验,以及相关问题的解决办法。

我们的自动化构建流程

开发提交代码到代码服务器(GitHub Gitlab BitBucket)
代码服务器通过 WebHook 触发 CI/CD(Codeship、Shippable、CircleCI、Jenkins)
CI 服务拉去最新的代码,构建 Docker 镜像,进行测试
自动集成测试完成后,将镜像推送到私有的 Registry
运维使用最新的 Docker 镜像进行部署

加快 Jenkins Pipline 执行速度

Scaling Pipelines
Project Cheetah – Faster, Leaner Pipeline That Can Keep Up With Demand

显示构建过程消耗的时间

Jenkins Pipeline: Enable timestamps in build log console – Stack Overflow

// 对于脚本式,使用 timestamps {} 包括脚本

node {
    timestamps {
  	    // do your job
    }
}

// 对于声明式,使用 options 包裹时间

pipeline {
  agent any
  options { timestamps () }
  // stages and rest of pipeline.
}

当检查特定目录发生变化后,再执行某些构建步骤

Pipeline Syntax
git – How to trigger a build only if changes happen on particular set of files – Stack Overflow

通过 when 的 chagneset 能够进行判断:when { changeset “**/*.js” }

when {
    anyOf {
        changeset "nginx/**"
        changeset "fluent-bit/**"
    }
}
steps {
    sh "make build-nginx"
    sh "make start-nginx"
}