前置条件可参考
Jenkins Pipeline 流水线 - 拉代码(SVN) + Maven 编译打包
Jenkins Pipeline 流水线 - 添加节点 使用代理
Jenkins Pipeline 流水线 - withCredentials 使用
Jenkins Pipeline 流水线 - 上传文件 Publish over SSH 执行命令
Jenkins Pipeline 流水线 - 上传文件 Publish over SSH + Docker 编译 + 上传到阿里仓库
Jenkins Pipeline 流水线 - 使用代理节点,Remote SSH 对 K8S 进行升级
Jenkins Pipeline 流水线 - Parameters 参数化构建
完整的 Pipeline Script
pipeline { agent any tools { maven 'Maven36' //添加 Maven 工具 } parameters { string defaultValue: '2.0', description: '编译版本号', name: 'BUILD_NUMBER' } stages { stage('拉取SVN代码') { steps { checkout([$class: 'SubversionSCM', additionalCredentials: [], excludedCommitMessages: '', excludedRegions: '', excludedRevprop: '', excludedUsers: '', filterChangelog: false, ignoreDirPropChanges: false, includedRegions: '', locations: [[cancelProcessOnExternalsFail: true, credentialsId: '凭证ID', depthOption: 'infinity', ignoreExternalsOption: true, local: '.', remote: 'SVN地址']], quietOperation: true, workspaceUpdater: [$class: 'UpdateUpdater']]) //该脚本由流水线语法自动生成 echo '拉取成功' } } stage('Maven 编译打包') { steps { //bat 'mvn --version' bat 'mvn clean package -Dmaven.test.skip=true' echo 'Maven 编译打包 完成' } } stage('上传文件到 Docker 服务器、编译、上传阿里仓库') { steps { 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 '上传文件 SUCCESS' } } stage('K8S 升级') { agent { label 'K8SAgent' } //代理节点的方式比远程 ssh 要简单些 steps { sh 'kubectl set image deployment/javademo1 vipsoft=registry.cn-shanghai.aliyuncs.com/vipsoft/vipsoft:${BUILD_NUMBER}' echo 'K8S 升级成功' } } } }
可以嵌套
pipeline { agent any stages { stage('Stage 1') { steps { echo "Hello world" } } stage('Sequential') { environment { FOR_SEQUENTIAL = "some-value" } stages { stage('Stage 1') { steps { echo "Step 1" } } stage('Stage 2') { steps { echo "Step 2" } } } } } }