当前位置:网站首页>Jenkins+webhooks- multi branch parametric construction-
Jenkins+webhooks- multi branch parametric construction-
2022-07-01 13:06:00 【An operation and maintenance young man】
Jenkins+webhooks- Multi branch parametric construction -
demand :
I'm here because the company has a static resource , The completion of the construction needs to be placed in nginx Published catalog , But it's not very convenient to build manually every time , What I have here is webhook Updating code triggers automatic build , But our environment is divided into formal environment and test environment , You need to build and publish to different environments and servers
My solution :
Scheme 1 :
The first way is to create two pipeline, Trigger for different branches webhook, It is not recommended to use too much memory
Option two :
If there are two branches , Development - test
hold jenkinsfile Write complete , Put it in the development branch ,jenkins-webhook Recognize that the development branch has been updated , Then pull the development branch code , Recognize the inside jenkinsfile, Will build and release as required
Senior boss document
https://xie.infoq.cn/article/600f642fcb26f0c280a7acf59
https://www.cnblogs.com/testopsfeng/p/15139538.html
https://blog.csdn.net/weixin_39637661/article/details/110087309?spm=1001.2101.3001.6650.7&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-110087309-blog-106900128.pc_relevant_antiscanv3&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-7-110087309-blog-106900128.pc_relevant_antiscanv3&utm_relevant_index=14
https://blog.csdn.net/shm19990131/article/details/107529357
https://gitbook.curiouser.top/origin/jenkins-generic-webhook-trigger%E6%8F%92%E4%BB%B6.html
Adopt the second scheme
pipeline adopt Custom parameters (This project is parameterized - String Parame), To build the deployment .
( notes : Parametric construction - String parameters )
One 、 Implementation content
Gitlab -- perhaps gitea The main branch 、 From branch After the code is modified , adopt webhook Trigger jenkins.
jenkins Can pass Branch variables To build the deployment .
Two 、 Implementation steps
1、 stay pipeline Pipeline project starts parameterized construction

Here I define two

2、 modify pipeline Of Jenkinsfile file , Specify variables to pull code
Be careful : What is used here is Jenkinsfile - pipeline, Each branch needs such a file

pipeline grammar
pipeline {
agent any
stages {
stage(' Pull the code ') {
steps {
checkout([$class: 'GitSCM', branches: [[name: '${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: '7f49313d-880d-4a5e-836f-cef4bf2ec37a', url: 'http://192.168.2.204:3000/aike/test.git']]])
}
}
stage(' choice node Version compilation and packaging ') {
steps {
nodejs('node') {
sh '''cd ${single_project_name}
npm install
npm run build
tar -zcvf ./front.end-levee.tar.gz ./dist'''
}
}
}
stage(' Publish to server ') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.2.204', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''
cd /a
tar -xzf front.end-levee.tar.gz -C ./
cp -r dist/* ./
rm -rf front.end-levee.tar.gz''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/a', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'front.end-levee.tar.gz')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
Code warehouse

Put this code , Upload to each branch

Look at the code

①、 To configure webhook, Notice that there are several branches , Just drive a few webhook
The main branch webhook:

To configure jenkins - Pipeline Build tasks
Build trigger

The problem summary
The above build , Basically, trigger automatic construction has been realized , But the only drawback is that each build pulls by default master Branch code
Full version
stay Jenkinsfile in , Add the configuration : Detailed explanation of variables
triggers {
GenericTrigger (
// Title during construction
causeString: 'Triggered by $ref',
// obtain POST Variables in parameters ,key Refers to the variable name , adopt $ref To access the corresponding value ,value refer to JSON Match the value ( Reference resources Jmeter Of JSON Extractor )
// ref Refers to the push Branch , The format is as follows :refs/heads/master
genericVariables: [[key: 'ref', value: '$.ref']],
// Print the obtained variable key-value, Here will be printed as :ref=refs/heads/master
printContributedVariables: true,
// Print POST Parameters passed
printPostContent: true,
// regexpFilterExpression And regexpFilterExpression Use in pairs
// When the two are equal , It will trigger the construction of the corresponding branch
regexpFilterExpression: '^refs/heads/(master|production)$',
regexpFilterText: '$ref',
// And webhook Configured in token Consistent parameter values
token: 'mytoken'
)
}
Sort out your ideas
This multi pipeline construction , Manage a lot pipeline Assembly line , There are only a few pipelines to manage
But you have to Jenkinsfile Put the file in your code , To recognize ,J It's capital

Identified 
Code repository configuration

Pipeline configuration
Here is the pipeline created
Defining variables

Post content parameters Middle configuration
For example, here we want to get ref The value of can be configured in this way
# Custom variable name
Variable: ref
# The expression uses JSONPath The way
Expression: $.ref # ref value
# Expression get value filter and match , for example refs/tags/Test_cet_v1.0.0 , When the configuration is as follows ref Its value is Test_cet_v1.0.0
Value filter: refs/tags/
# Does not match the default value
Default value:master

To configure token

Cause To configure
Triggered by $ref

Ignore some branches
It can be done by Filter by name (with wildcards) Realization :

To configure Optional filter
^refs/heads/(master|release)$

complete jenkinsfile You can refer to
pipeline {
agent any
triggers {
GenericTrigger (
// Title during construction
causeString: 'Triggered by $ref',
// obtain POST Variables in parameters ,key Refers to the variable name , adopt $ref To access the corresponding value ,value refer to JSON Match the value ( Reference resources Jmeter Of JSON Extractor )
// ref Refers to the push Branch , The format is as follows :refs/heads/master
genericVariables: [[key: 'ref', value: '$.ref']],
// Print the obtained variable key-value, Here will be printed as :ref=refs/heads/master
printContributedVariables: true,
// Print POST Parameters passed
printPostContent: true,
// regexpFilterExpression And regexpFilterExpression Use in pairs
// When the two are equal , It will trigger the construction of the corresponding branch
regexpFilterExpression: '^refs/heads/(master|release)$',
regexpFilterText: '$ref',
// And webhook Configured in token Consistent parameter values
token: 'aikeya'
)
}
stages {
stage(" Test the deployed ") {
when {
branch 'release'
}
steps {
echo 'release branch'
}
}
stage(" Production deployment ") {
when {
branch 'master'
}
steps {
echo 'master branch'
}
}
stage(' choice node Version compilation and packaging ') {
steps {
nodejs('node') {
sh '''cd ${single_project_name}
npm install
npm run build
tar -zcvf ./front.end-levee.tar.gz ./dist'''
}
}
}
stage(' Publish to server ') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.2.204', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''
cd /b
tar -xzf front.end-levee.tar.gz -C ./
cp -r dist/* ./
rm -rf front.end-levee.tar.gz''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '/a', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'front.end-levee.tar.gz')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
efaultExcludes: false, patternSeparator: ‘[, ]+’, remoteDirectory: ‘/a’, remoteDirectorySDF: false, removePrefix: ‘’, sourceFiles: ‘front.end-levee.tar.gz’)], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}
###### Configuration complete Test it
边栏推荐
- 基因检测,如何帮助患者对抗疾病?
- 请问flink mysql cdc 全量读取mysql某个表数据,对原始的mysql数据库有影响吗
- Wang Xing's infinite game ushers in the "ultimate" battle
- 有没有大佬 遇到过flink监控postgresql数据库, 检查点无法使用的问题
- Fiori applications are shared through the enhancement of adaptation project
- ROS2 Foxy depthai_ ROS tutorial
- How to play with the reading and writing operations of blocking sockets?
- R语言基于h2o包构建二分类模型:使用h2o.gbm构建梯度提升机模型GBM、使用h2o.auc计算模型的AUC值
- Use Net core access wechat official account development
- Shell script imports stored procedures into the database
猜你喜欢

nexus搭建npm依赖私库

Ikvm of toolbox Net project new progress

Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing

Operator-1 first acquaintance with operator

The future of game guild in decentralized games

【开发大杀器】之Idea

ROS2 Foxy depthai_ros教程

logstash报错:Cannot reload pipeline, because the existing pipeline is not reloadable

香港科技大学李泽湘教授:我错了,为什么工程意识比上最好的大学都重要?

Huawei HMS core joins hands with hypergraph to inject new momentum into 3D GIS
随机推荐
工具箱之 IKVM.NET 项目新进展
题目 1004: 母牛的故事(递推)
MySQL gap lock
【牛客刷题-SQL大厂面试真题】NO2.用户增长场景(某度信息流)
Zabbix 6.0 源码安装以及 HA 配置
【邂逅Django】——(二)数据库配置
Topic 2612: the real topic of the 12th provincial competition of the Blue Bridge Cup in 2021 - the least weight (enumerating and finding rules + recursion)
Mysql间隙锁
MySQL statistical bill information (Part 2): data import and query
Ikvm of toolbox Net project new progress
Jenkins+webhooks-多分支参数化构建-
Tencent always takes epoll, which is annoying
Redis exploration: cache breakdown, cache avalanche, cache penetration
6.30 simulation summary
使用nvm管理nodejs(把高版本降级为低版本)
Use Net core access wechat official account development
哪个券商公司开户佣金低又安全又可靠
我花上万学带货:3天赚3元,成交靠刷单
Simple two ball loading
硬件开发笔记(九): 硬件开发基本流程,制作一个USB转RS232的模块(八):创建asm1117-3.3V封装库并关联原理图元器件