Jenkins pipeline

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   
                }
             }   
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值