第四篇 jenkins怎么样部署k8s项目之pipeline-podTemplate

配置说明

上两篇说了,如何在Jenkins配置PodTemplate,有点麻烦,我现在贴出我整理的一套配置,各位看官请拿走

/********************************************************************************
** Company: 
** auth:    老C
** date:    2021/5/14 17:08:28
** desc:    本配置为Jenkins对接k8s自动化部署服务配置
** Ver.:     V1.0.0
*********************************************************************************/
def label = "jenkins-slave-${UUID.randomUUID().toString()}"
podTemplate(
    cloud: '测试环境',
    label: label,
    name: label,
    namespace: 'devops',
    serviceAccount: 'sa-jenkins',
    containers: [
        containerTemplate(
            name: 'maven',
            image: 'maven:3.8-jdk-8',
            command: 'sleep',
            args: '9999999',
            workingDir: '/home/jenkins/agent',
            // container resource limit
            resourceLimitCpu: '',
            resourceLimitEphemeralStorage: '',
            resourceLimitMemory: '',
            resourceRequestCpu: '',
            resourceRequestEphemeralStorage: '',
            resourceRequestMemory: '',
            // 探针
            livenessProbe: containerLivenessProbe(
                execArgs: '',
                failureThreshold: 0,
                initialDelaySeconds: 0,
                periodSeconds: 0,
                successThreshold: 0,
                timeoutSeconds: 0
            )
        ),
        containerTemplate(
            name: 'jenkins-slave',
            image: 'jenkins/jnlp-slave:latest',
            command: 'sleep',
            args: '9999999',
            workingDir: '/home/jenkins/agent',
            // container resource limit
            resourceLimitCpu: '',
            resourceLimitEphemeralStorage: '',
            resourceLimitMemory: '',
            resourceRequestCpu: '',
            resourceRequestEphemeralStorage: '',
            resourceRequestMemory: '',
            // 探针
            livenessProbe: containerLivenessProbe(
                execArgs: '',
                failureThreshold: 0,
                initialDelaySeconds: 0,
                periodSeconds: 0,
                successThreshold: 0,
                timeoutSeconds: 0
            )
        ),
        containerTemplate(
            name: 'docker',
            image: 'docker:20.10.6',
            command: 'sleep',
            args: '9999999',
            workingDir: '/home/jenkins/agent',
            // container resource limit
            resourceLimitCpu: '',
            resourceLimitEphemeralStorage: '',
            resourceLimitMemory: '',
            resourceRequestCpu: '',
            resourceRequestEphemeralStorage: '',
            resourceRequestMemory: '',
            // 探针
            livenessProbe: containerLivenessProbe(
                execArgs: '',
                failureThreshold: 0,
                initialDelaySeconds: 0,
                periodSeconds: 0,
                successThreshold: 0,
                timeoutSeconds: 0
            )
        ),
        containerTemplate(
            name: 'kubectl',
            image: 'lachlanevenson/k8s-kubectl:latest',
            command: 'sleep',
            args: '9999999',
            workingDir: '/home/jenkins/agent',
            // container resource limit
            resourceLimitCpu: '',
            resourceLimitEphemeralStorage: '',
            resourceLimitMemory: '',
            resourceRequestCpu: '',
            resourceRequestEphemeralStorage: '',
            resourceRequestMemory: '',
            // 探针
            livenessProbe: containerLivenessProbe(
                execArgs: '',
                failureThreshold: 0,
                initialDelaySeconds: 0,
                periodSeconds: 0,
                successThreshold: 0,
                timeoutSeconds: 0
            )
        )
    ],
    envVars: [
        envVar(key: 'GIT_SSL_NO_VERIFY', value: '1')
    ],
    volumes: [
        hostPathVolume(hostPath: '/data/jenkins_home/.m2', mountPath: '/root/.m2'),
        hostPathVolume(hostPath: '/var/run/docker.sock', mountPath: '/var/run/docker.sock')
    ]
)
    {
        // pipeline
        node(label) {
            stage('初始化') {
                    script {
                        container('jenkins-slave') {
                            sh 'echo "初始化"'
                            sh 'git config --global http.sslverify false'
                            sh 'git config --global https.sslverify false'
                            sh 'mkdir -p configure'
                        }
                    }
            }
            stage('获取源码') {
                    script {
                        git branch: 'dev', credentialsId: 'a0f736bb-6bc8-43ce-93d6-xxx', url: 'https://xxx/xxx/xxx-CLOUD.git'
                        dir('configure') {
                            git credentialsId: 'a0f736bb-6bc8-43ce-93d6-xxx', url: 'https://xxx/xxx/config.git'
                        }
                    }
            }
            stage('构建maven') {
                    script {
                        container('maven') {
                            dir('xxx-cloud/xxx-config') {
                                // some block
                                // sh 'java -version'
                                sh 'mvn clean package'
                            }
                        }
                    }
            }
            stage('打包镜像') {
                    script {
                        container('docker') {
                            dir('xxx-cloud/xxx-config/target') {
                                sh 'cp ../../../configure/config/* .'
                                sh "docker build -t $registry_host'/'$docker_image':'$BUILD_NUMBER --no-cache ."
                                sh "docker push $registry_host'/'$docker_image':'$BUILD_NUMBER"
                                sh "docker tag $registry_host'/'$docker_image':'$BUILD_NUMBER $registry_host'/'$docker_image':latest'"
                                sh "docker push $registry_host'/'$docker_image':latest'"
                                sh "docker rmi $registry_host'/'$docker_image':'$BUILD_NUMBER"
                            }
                        }
                    }
            }
            stage('部署deploy') {
                    script {
                        container('kubectl') {
                            dir('xxx-cloud/xxx-config/target') {
                                try {
                            sh 'kubectl delete -f service.yaml'
                                }catch (Exception e) {
                            println e
                                }
                                try {
                            sh 'kubectl delete -f deployment.yaml'
                                }catch (Exception e) {
                            println e
                                }
                                sh 'kubectl apply -f deployment.yaml'
                                sh 'kubectl apply -f service.yaml'
                            }
                        }
                    }
            }
        }
    }
parameters {
        string(name: 'registry_host', defaultValue: '172.25.42.xxx:5000', description: '镜像仓库地址')
        string(name: 'docker_image', defaultValue: 'xxx/config', description: 'docker镜像名')
}

那么上面这个pipeline配置怎么用呢

很简单,普及一下吧!

新建一个JOB

风格要选 流水线pipeline项目

进入JOB的配置页面

往下拉,一直到流水线配置项目,把上面配置贴到里面,完活保存!

在这里插入图片描述

见证奇迹的时刻

版权声明:本文为作者原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原创文章,作者:老C,如若转载,请注明出处:https://www.code404.icu/608.html

发表评论

登录后才能评论