1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| // helm 发布 def helmDeploy(Map args) { // Helm 尝试部署 if (args.dry_run) { println '尝试 Helm 部署,验证是否能正常部署' sh 'helm repo update' sh "helm upgrade --install ${args.name} --version ${args.version} --set image.repository=${args.repository} --set image.tag=${args.version} chartmuseum/${args.name} --dry-run --debug" } // Helm 正式部署 else { println '正式 Helm 部署' sh 'helm repo update' sh "helm upgrade --install ${args.name} --version ${args.version} --set image.repository=${args.repository} --set image.tag=${args.version} chartmuseum/${args.name}" } }
def noticeRtx(String ids, String info) {}
pipeline { agent any parameters { string(name: 'project_name', defaultValue: 'rbac-service', description: '项目名称') string(name: 'repo_url', defaultValue: 'git@192.168.1.28:Web/rbac-service.git', description: 'git 仓库') // Git Parameter gitParameter(name: 'tag_name', type: 'PT_TAG', branchFilter: 'origin/(.*)', defaultValue: '0.0.1', selectedValue: 'DEFAULT', sortMode: 'DESCENDING_SMART', useRepository: env.repo_url, description: 'git 标签') }
stages { stage('Init') { steps { sh 'printenv' script { if (env.gitlabSourceRepoSshUrl) { env.repo_url = env.gitlabSourceRepoSshUrl } if (env.gitlabSourceRepoName) { env.project_name = env.gitlabSourceRepoName } if (env.gitlabSourceBranch) { env.tag_name = env.gitlabSourceBranch.split('/')[-1] } } buildName "#${BUILD_NUMBER}-${project_name}-${tag_name}" } }
stage('Source') { steps { checkout([$class: 'GitSCM', branches: [[name: "refs/tags/${tag_name}"]], userRemoteConfigs: [[url: "${repo_url}"]]]) script { env.imagePath = project_name env.repository = env.imagePath + ':' + env.tag_name env.commitInfo = sh(script:'git log --oneline --no-merges|head -1', returnStdout: true) } echo 'repository: ' + env.repository } }
stage('Build') { steps { echo 'build repository: ' + env.repository script { docker.withRegistry('https://hub.docker.com') { def customImage = docker.build(repository) customImage.push()
sh 'docker images | grep citest | awk \'{print $1":"$2}\' | xargs docker rmi -f || true' sh 'docker rmi -f `docker images | grep \'<none>\' | awk \'{print $3}\'` || true' } } } }
stage('PackageHelm') { steps { echo 'package helm' sh 'helm package ' + 'chart/' + project_name + ' --app-version ' + env.tag_name + ' --version ' + env.tag_name echo 'push helm' sh 'helm cm-push ' + env.project_name + '-' + env.tag_name + '.tgz chartmuseum -f' } }
stage('Deploy') { steps { withKubeConfig([credentialsId: 'k8s 密钥id', serverUrl: 'k8s地址']) { // 执行 Helm 方法 echo 'Helm 执行部署测试' helmDeploy(dry_run: true ,name: env.project_name , repository: env.imagePath , version: env.tag_name) echo 'Helm 执行正式部署' helmDeploy(dry_run: false, name: env.project_name, repository: env.imagePath, version: env.tag_name) } } }
stage('Clean') { steps { cleanWs( cleanWhenAborted: true, cleanWhenFailure: true, cleanWhenNotBuilt: true, cleanWhenSuccess: true, cleanWhenUnstable: true, cleanupMatrixParent: true, disableDeferredWipeout: true, deleteDirs: true ) } } }
post { always { echo env.project_name + ': 项目构建完成' } success { echo env.project_name + ': 构建成功' noticeRtx("", env.user + '成功构建了: ' + env.project_name + '\n版本号: ' + env.tag_name + '\n更新内容: \n' + env.commitInfo) } unstable { echo env.project_name + ': 构建过程报错' noticeRtx("", env.user + '构建: ' + env.project_name + '失败了 \n版本号: ' + env.tag_name + '\n更新内容: \n' + env.commitInfo) } failure { echo env.project_name + ': 构建失败' noticeRtx("", env.user + '构建: ' + env.project_name + '失败了 \n版本号: ' + env.tag_name + '\n更新内容: \n' + env.commitInfo) } } }
|