Jenkins是一种广泛使用的CICD工具。多微服务的场景下流水线非常复杂。进行一些很小的变更都是一项繁琐的任务,例如更新一个URL一样。因为必须为每个微服务都进行更改。由于缺少更改日志,因此也很难跟踪进行了哪些更改以及由谁进行更改。
解决方案:使用“ Jenkins Pipeline”将作业定义为Groovy代码
使用插件套件“ Jenkins Pipeline”,您可以将不同的作业定义为Groovy代码。然后,您可以将其检入到首选的版本控制系统中,并维护和进一步开发项目代码。与多分支流水线连接时,将根据“ Jenkinsfile”中的说明自动构建项目的所有分支。
Groovy代码示例
一个具有多种服务的项目,全部使用Maven构建。分支开发中的合并是为了构建新版本。
Coffee-Service, Food-Service: Jenkinsfile
def pipeline
stage('Load pipeline') {
// Load the pipeline from the shared repository
fileLoader.withGit(
'https://url-to-pipeline-repo.git',
'master',
' id-of-in-jenkins-stored-credentials') {
// Every service is able to use pipeline.groovy
pipeline = fileLoader.load('pipeline.groovy')
}
}
pipeline.execute()
Pipeline Repo: pipeline.groovy
def execute() {
stage('Checkout') {
checkout scm
}
stage('Build service') {
sh "mvn clean install"
}
// The variable env.BRANCH_NAME is automatically set to the current branch.
// As a consequence of this feature the pipeline can deal with every new branch
if ("develop".equals(env.BRANCH_NAME) {
stage('Release') {
sh "mvn release:prepare release:perform"
}
}
}
文章来源
文章源地址:https://www.jambit.com/en/latest-info/toilet-papers/groovy-jenkins-pipeline-baby/