在38、jenkins持续集成(一)中,我们介绍完了基于free style的持续集成构建
接下来我们来学习pipeline实现的持续集成构建
为什么之前已经有基于free style的持续集成了,还需要pipeline?
在基于free style的持续集成中,我们发现都是界面化的配置,而且一旦jenkins宕机了就无法修改了
而pipeline模式主要是依靠脚本化代码化的方式实现持续集成,更利于我们维护,当然相对于free style门槛更高
对于比较复杂的构建,已经我们很关心构建过程,不希望构建丢失建议使用pipeline
需要提前安装好pipeline插件,参考:https://blog.csdn.net/qq23001186/article/details/126372380



pipeline {
agent any
stages {
stage('pull code') {
steps {
echo 'pull code'
}
}
stage('build project') {
steps {
echo 'pull project'
}
}
stage('deploy project') {
steps {
echo 'deploy project'
}
}
}
}








构建之前我们先进入pipeline-test目标新建target目录及good.txt

删除目标服务器之前使用free style构建传输的文件

pipeline {
agent any
stages {
stage('pull code') {
steps {
git credentialsId: 'gin-test-jenkins', url: 'https://github.com/qq23001186/gin_test.git'
}
}
stage('build project') {
steps {
sh '''echo “开始构建”
echo “构建完成”'''
}
}
stage('deploy project') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.124.51', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'echo “jenkins test success”', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'gin_test', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'target/**')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}



上面我们依然是在jenkins中使用pipeline脚本构建,如果jenkins挂掉了,而我们的pipeline脚本很重要,这就丢失了
同时我们也希望,pipeline的脚本也跟我们的代码一样有版本控制的功能
Jenkinsfile
pipeline {
agent any
stages {
stage('pull code') {
steps {
git credentialsId: 'gin-test-jenkins', url: 'https://github.com/qq23001186/gin_test.git'
}
}
stage('build project') {
steps {
sh '''echo “开始构建”
echo “构建完成”'''
}
}
stage('deploy project') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.124.51', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'echo “jenkins test success”', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'gin_test', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'target/**')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}











* * * * *;定时字符串从左到右依次为 -> 分 时 日 月 周;* 等同于H,表示任意一个合理的数;
5 * * * * ,表示每个小时的第5分钟都会构建一次* 5 * * *,表示在每天5点的时候,一小时内每一分钟都会构建一次* * 5 * *,表示在每个月5号的时候,0点开始每分钟构建一次* * * 5 *,表示每年的5月份,1号0点开始每分钟构建一次* * * * 5,表示每周五0点开始每分钟构建一次H 2 * * *H/5 * * * *H H/2 * * *H 12 * * *或0 12 * * *(0这种写法已被H替代了)H 18 * * *H/15 * * * *或*/15 * * * *(这种已经被第一种替代了,jenkins也不推荐这种写法了)H 18-23/3 * * 6-7*/1 * * * *,注意这里不能用H,否则触发不了


之前虽然我们使用Jenkinsfile的方式将构建脚本放到了git上,但是pipeline脚本中有很多参数被写死了
例如,服务器的ip地址,shell脚本提示等
我们可以使用参数化构建过程来解决这个问题




remoteDirectory: 'gin_test'修改为remoteDirectory: '${remote_dir}'
pipeline {
agent any
stages {
stage('pull code') {
steps {
git credentialsId: 'gin-test-jenkins', url: 'https://github.com/qq23001186/gin_test.git'
}
}
stage('build project') {
steps {
sh '''echo “开始构建”
echo “构建完成”'''
}
}
stage('deploy project') {
steps {
sshPublisher(publishers: [sshPublisherDesc(configName: '192.168.124.51',
transfers: [sshTransfer(cleanRemote: false, excludes: '',
execCommand: 'echo “jenkins test success”',
execTimeout: 120000, flatten: false, makeEmptyDirs: false,
noDefaultExcludes: false, patternSeparator: '[, ]+',
remoteDirectory: '${remote_dir}', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'target/**')],
usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
}
}
}
}

