pipeline {
//指定任务在哪个集群节点上执行
agent any
//声明环境变量,方便后面使用
environment {
key='value'
}
parameters {
choice(name: 'BRANCH', choices: ['dev', 'test', 'master'], description: '代码分支')
}
stages {
stage('检查版本号') {
when {
equals expected: 'master', actual: params.BRANCH
equals expected: '', actual: params.VERSION
}
steps {
echo 'Aborting the build because conditions are not met'
error("Aborting the build because conditions are not met")
}
}
stage('拉取git仓库代码') {
steps {
checkout scmGit(branches: [[name: '*/${BRANCH}']], extensions: [], userRemoteConfigs: [[credentialsId: 'da081bcb-677d-4e23-b864-f1741cd9835c', url: 'http://192.168.0.101:9992/xxx/xxx.git']])
echo '拉取git仓库代码-SUCCESS'
}
}
stage('通过maven构建项目') {
steps {
sh 'mvn clean package -DskipTests'
echo '通过maven构建项目-SUCCESS'
}
}
// stage('代码检查 ') {
// steps {
// withSonarQubeEnv('SonarQube') {
// sh '''mvn sonar:sonar -DskipTests \\
// -Dsonar.projectKey=beef-service \\
// -Dsonar.host.url=http://192.168.0.102:9000/sonarqube \\
// -Dsonar.login=squ_17b9345e946d3f55ed4dfbb531a6b237180d27b2'''
// }
// echo '通过maven构建项目-SUCCESS'
// }
// }
// stage("Quality gate") {
// steps {
// waitForQualityGate abortPipeline: true
// }
// }
stage('通过docker制作自定义镜像') {
steps {
script {
if ( branch == 'master' ) {
sh '/usr/bin/docker build -t 192.168.0.103:18082/docker/beef-service-${BRANCH}:${VERSION} .'
}else{
sh '/usr/bin/docker build -t 192.168.0.103:18082/docker/beef-service-${BRANCH} .'
}
}
echo 'docker制作自定义镜像-SUCCESS'
}
}
stage('将自定义镜像推送到仓库中') {
steps {
script {
if ( branch == 'master' ) {
sh 'docker push 192.168.0.103:18082/docker/xxx-service-${BRANCH}:${VERSION}'
}else{
sh 'docker push 192.168.0.103:18082/docker/xxx-service-${BRANCH}'
}
}
echo '推送到harbor仓库-SUCCESS'
}
}
stage('部署'){
steps{
script{
switch(BRANCH)
{
case 'dev':
sh '''curl -X PUT \\
-H "Content-Type: application/yaml" \\
-H "Cookie: KuboardUsername=admin; KuboardAccessKey=3amr52y36jsw.apw7jitm62pakb2463znfdptfm7hbx4k" \\
-d \'{"kind":"deployments","namespace":"default","name":"xxx-service"}\' \\
"http://192.168.0.132/kuboard-api/cluster/k8s-dev/kind/CICDApi/admin/resource/restartWorkload"'''
echo 'deploy dev SUCCESS'
break
case 'test':
sh '''curl -X PUT \\
-H "Content-Type: application/yaml" \\
-H "Cookie: KuboardUsername=admin; KuboardAccessKey=3amr52y36jsw.apw7jitm62pakb2463znfdptfm7hbx4k" \\
-d \'{"kind":"deployments","namespace":"default","name":"xxx-service"}\' \\
"http://192.168.0.132/kuboard-api/cluster/k8s-test/kind/CICDApi/admin/resource/restartWorkload"'''
echo 'deploy test SUCCESS'
break
}
}
}
}
}
}
Jenkins pipeline
于 2023-01-13 14:01:34 首次发布