pipeline
记录一些使用到的pipeline:
仓库多分支,选择分支下载代码:
pipeline { agent any parameters { gitParameter branchFilter: 'origin/(.*)', defaultValue: 'main', name: 'BRANCH', type: 'PT_BRANCH', description: '', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*' } stages { stage('Build') { steps { echo 'Building..' //git(url: 'git@192.168.77.101:root/devops.git', branch: 'main', credentialsId: 'jenkins-ssh-gitlab') } } stage('Test') { steps { checkout([$class: 'GitSCM', branches: [[name: "${params.BRANCH}"]], doGenerateSubmoduleConfigurations: false, extensions: [], gitTool: 'Default', submoduleCfg: [], credentialsId: "jenkins-ssh-gitlab", userRemoteConfigs: [[url: 'git@192.168.77.101:root/test-web.git']] ]) } } stage('Deploy') { steps { echo 'Deploying....' } } } }
Jenkins+gitee+k8s结合构建部署:
def patchMethod(params) { def patchJson = [ [ op: 'replace', path: '/spec/template/spec/containers/0/image', value: params.image ] ] //使用httpRequest需要安装HTTP Request插件 httpRequest( url: 'https://192.168.77.103:6443/apis/apps/v1/namespaces/testns/deployments/springboot-manager', httpMode: 'PATCH', ignoreSslErrors: true, customHeaders: [ [ name: 'Authorization', value: 'Bearer ' + params.Token ], [ name: 'Content-Type', value: 'application/json-patch+json' ] ], //使用writeJSON需要安装Pipeline Utility Steps插件 requestBody: writeJSON(returnText: true, json: patchJson) //requestBody: patchJson ) } pipeline { agent any stages { stage('git checkout') { steps { script { git( branch: 'master', url: 'git@gitee.com:jsonhc/springboot-manager.git', credentialsId: 'jenkins-ssh-gitee' ) } } } stage('build') { steps { script { sh "docker build -t registry.cn-hangzhou.aliyuncs.com/jsonhc/springboot-manager ." sh "docker push registry.cn-hangzhou.aliyuncs.com/jsonhc/springboot-manager" } } } stage('deploy to k8s') { steps { script { withCredentials([string(credentialsId: 'k8s-token', variable: 'TOKEN')]) { def reault = [ Token: TOKEN, image: "registry.cn-hangzhou.aliyuncs.com/jsonhc/springboot-manager" ] echo "${reault.Token}" patchMethod(reault) } } } } } }
git多分支选择,节点选择进行ssh节点操作部署:
pipeline { agent any parameters { gitParameter name: 'BRANCH_TAG',type: 'PT_BRANCH_TAG',defaultValue: 'master',branch: '',branchFilter: 'origin/(.*)',description: '',quickFilterEnabled: false,selectedValue: 'NONE',sortMode: 'NONE',tagFilter: '*' choice choices: ['all', '192.168.77.100', '192.168.77.103'], description: '节点', name: 'vm' } stages { stage('Example') { steps { checkout([$class: 'GitSCM', branches: [[name: "${params.BRANCH_TAG}"]], doGenerateSubmoduleConfigurations: false, extensions: [], gitTool: 'Default', submoduleCfg: [], credentialsId: 'jenkins-ssh-gitlab', userRemoteConfigs: [[url: 'git@192.168.77.101:root/springboot-manager.git']] ]) } } stage('ip') { steps {
// 插件参考:https://plugins.jenkins.io/ssh-steps/#plugin-content-sshput withCredentials([usernamePassword(credentialsId: 'user-192.168.77.103', passwordVariable: 'password', usernameVariable: 'userName')]) { script { def remote = [:] //remote.name = '192.168.77.103' //remote.host = '192.168.77.103' remote.name = params.vm remote.host = params.vm remote.user = userName remote.password = password remote.allowAnyHosts = true stage('Remote SSH') { sshCommand remote: remote, command: "hostname" } } } } } } }
选择git仓库,选择仓库分支,选择部署节点:
pipeline { agent any parameters { choice choices: ['gitlab-frontend', 'gitlab-backend'], description: '代码仓库', name: 'repository' gitParameter name: 'BRANCH_TAG',type: 'PT_BRANCH_TAG',defaultValue: 'main',branch: '',branchFilter: 'origin/(.*)',description: '',quickFilterEnabled: false,selectedValue: 'NONE',sortMode: 'NONE',tagFilter: '*' choice choices: ['all', '192.168.77.100', '192.168.77.103'], description: '节点', name: 'vm' } stages { stage('clone gitlab-frontend') { when { expression { return params.repository == "gitlab-frontend"} } steps { checkout([$class: 'GitSCM', branches: [[name: "${params.BRANCH_TAG}"]], doGenerateSubmoduleConfigurations: false, extensions: [], gitTool: 'Default', submoduleCfg: [], credentialsId: 'jenkins-ssh-gitlab', userRemoteConfigs: [[url: 'git@192.168.77.101:root/test-web.git']] ]) } } stage('clone gitlab-backend') { when { expression { return params.repository == "gitlab-backend"} } steps { checkout([$class: 'GitSCM', branches: [[name: "${params.BRANCH_TAG}"]], doGenerateSubmoduleConfigurations: false, extensions: [], gitTool: 'Default', submoduleCfg: [], credentialsId: 'jenkins-ssh-gitlab', userRemoteConfigs: [[url: 'git@192.168.77.101:root/springboot-manager.git']] ]) } } stage('build gitlab-frontend') { when { expression { return params.repository == "gitlab-frontend"} } steps { script { echo "${params.vm}: build gitlab-frontend" } } } stage('build gitlab-backend') { when { expression { return params.repository == "gitlab-backend"} } steps { withCredentials([usernamePassword(credentialsId: 'user-192.168.77.103', passwordVariable: 'password', usernameVariable: 'userName')]) { script { def remote = [:] //remote.name = '192.168.77.103' //remote.host = '192.168.77.103' remote.name = params.vm remote.host = params.vm remote.user = userName remote.password = password remote.allowAnyHosts = true stage('Remote SSH') { sshCommand remote: remote, command: "hostname" } } } } } stage('publish over ssh gitlab-frontend') { when { expression { return params.repository == "gitlab-frontend"} } steps { sshPublisher(publishers: [sshPublisherDesc(configName: "node2-192.168.77.101", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "hostname && ls -l /root ", execTimeout: 1080000, flatten: false, makeEmptyDirs: true, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "test", remoteDirectorySDF: false, removePrefix: '', sourceFiles: "README.md")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)]) } } stage('publish over ssh gitlab-backend') { when { expression { return params.repository == "gitlab-backend"} } steps { sshPublisher(publishers: [sshPublisherDesc(configName: "node2-192.168.77.101", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: "hostname && ls -l /root ", execTimeout: 1080000, flatten: false, makeEmptyDirs: true, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: "test", remoteDirectorySDF: false, removePrefix: '', sourceFiles: ".gitlab-ci.yml")], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: true)]) } } } }
git仓库分支选择,部署节点选择进行ssh执行命令部署:
pipeline { agent any parameters { gitParameter name: 'BRANCH_TAG',type: 'PT_BRANCH_TAG',defaultValue: 'main',branch: '',branchFilter: 'origin/(.*)',description: '',quickFilterEnabled: false,selectedValue: 'NONE',sortMode: 'NONE',tagFilter: '*' choice choices: ['all', '192.168.77.100', '192.168.77.103'], description: '节点', name: 'vm' } stages { stage('clone gitlab-backend') { steps { checkout([$class: 'GitSCM', branches: [[name: "${params.BRANCH_TAG}"]], doGenerateSubmoduleConfigurations: false, extensions: [], gitTool: 'Default', submoduleCfg: [], credentialsId: 'jenkins-ssh-gitlab', userRemoteConfigs: [[url: 'git@192.168.77.101:root/springboot-manager.git']] ]) } } stage('build gitlab-backend') { steps { script { sh "docker build -t registry.cn-hangzhou.aliyuncs.com/jsonhc/springboot-manager ." sh "docker push registry.cn-hangzhou.aliyuncs.com/jsonhc/springboot-manager" } } } stage('deploy') { steps { withCredentials([usernamePassword(credentialsId: 'user-192.168.77.103', passwordVariable: 'password', usernameVariable: 'userName')]) { script { def remote = [:] //remote.name = '192.168.77.103' //remote.host = '192.168.77.103' remote.name = params.vm remote.host = params.vm remote.user = userName remote.password = password remote.allowAnyHosts = true stage('Remote SSH') { sshCommand remote: remote, command: "docker run -d -p 7777:8080 --name springboot-manager registry.cn-hangzhou.aliyuncs.com/jsonhc/springboot-manager" } } } } } } }
pipeline dir用法:
pipeline { agent any stages { stage('Clone Repository 1') { steps { dir('repo1') { git branch: 'main', url: 'https://github.com/example/repo1.git' } } } stage('Clone Repository 2') { steps { dir('repo2') { git branch: 'main', url: 'https://github.com/example/repo2.git' } } } }
pipeline if else用法:
pipeline { agent any parameters { //定义一个pipeline参数 choice choices: ['master', 'release', 'test'], description: '分支名称', name: 'branchName' } stages { stage('deploy') { steps { script { if( params.branchName == "test" ) { //使用pipeline参数 echo "deploy to test。" } else { echo "deploy to dev。" } } } } } }
pipeline expression用法:
//逻辑与 when { expression { return A && B} } //逻辑或 when { expression { return A || B} } //从文件中取值 when { expression { return readFile("pom.xml").contains("mycomponent") } } //正则表达式 when { expression { return token ==~/(?i)(Y|YES|T|TRUE)/} }