Jenkins 多分支流水线(SVN)
实际应用过程中,一般多分支流水线的方式用得比较多一些,
master 对应 生成环境
develop 对应 测试环境,
将不同分支的代码构建到不同的环境中
添加 Jenkinsfile 文件
Jenkinsfile 内容见:Jenkins Pipeline 流水线 - 完整构建 Pipeline Script 脚本
DevOps/trunk 专门用来放运维脚本
提交 Jenkins 到 SVN
注意代码要放到 trunk,branches 等 分支中
创建多分支流任务
运行
多个 stage 使用不同代理,会在每一步都进行SVN 的切换
完整 pipeline 脚本
pipeline {
agent { label 'JenkinsAgent' }
parameters {
string defaultValue: '4.2', description: '编译版本号', name: 'BUILD_NUMBER'
}
tools {
maven 'Maven36' //添加 Maven 工具
}
stages {
stage('CheckOut SVN Code') {
steps {
checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: 'c3b4ef0e-b414-4376-8a21-2e08e1c84dfb', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'SVN地址']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']]) //该脚本由流水线语法自动生成
echo 'CheckOut SVN Success'
}
}
// Jenkinsfile 的 地址和代码不在同一个仓库下时,需要用这种嵌套的方式,否则,代码刚拉下来,执行第二个 stage 时就被删除了。
stage('Maven Build') {
steps {
bat 'mvn clean package -Dmaven.test.skip=true'
echo 'Maven Build Success'
}
}
//Docker 在 3.88服务器
stage('Docker Build 、Push Repository') {
steps {
#sourceFiles windows下注意 用 \\ 否则可能上传时导致文件找不到。
sshPublisher(publishers: [sshPublisherDesc(configName: 'DockerPublishSSH', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'PipelineDemo', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'target/demojenkins.jar'), sshTransfer(cleanRemote: false, excludes: '', execCommand: '''
cd /opt/jenkins/PipelineDemo
docker build -t registry.cn-shanghai.aliyuncs.com/vipsoft/vipsoft:${BUILD_NUMBER} .
docker login -u hi帐号ID@aliyun.com -p 密码 registry.cn-shanghai.aliyuncs.com
docker push registry.cn-shanghai.aliyuncs.com/vipsoft/vipsoft:${BUILD_NUMBER}''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: 'PipelineDemo', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'Dockerfile')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)])
echo 'Docker SUCCESS'
}
}
//K8S 在 3.181服务器
stage('K8S Deployment') {
steps {
script {
def remote = [:]
remote.name = 'K8S Server'
remote.host = '172.16.3.181'
remote.allowAnyHosts = true
withCredentials([usernamePassword(credentialsId: 'K8SMaster', passwordVariable: 'password', usernameVariable: 'username')]) {
remote.user = "${username}" // 变量要使用“”双引号
remote.password = "${password}" //把凭证中的用户名、密码拿出来给 remote参数
}
sshCommand remote: remote, command: "kubectl set image deployment/javademo1 vipsoft=registry.cn-shanghai.aliyuncs.com/vipsoft/vipsoft:${BUILD_NUMBER}"
echo 'K8S Deployment SUCCESS'
}
}
}
}
}
本文来自博客园,作者:VipSoft 转载请注明原文链接:https://www.cnblogs.com/vipsoft/p/16855204.html