码迷,mamicode.com
首页 > Web开发 > 详细

jenkins docker-plugin 和 kubernetes-plugin插件实用案例

时间:2018-01-15 11:12:47      阅读:764      评论:0      收藏:0      [点我收藏+]

标签:tab   获取   nta   containe   ber   方法   step   stp   mac   

Jenkins docker-plugin的使用方法:
node {
        stage(‘Clone Code‘) {
                dir(‘baas-ops‘) {
                        git credentialsId: ‘umarkci‘, url: ‘git@github.******.com:umark/baas-ops.git‘
                }
        }

        stage(‘Unit testing‘) {
                docker.image(‘busybox‘).inside {
                        sh ‘echo "Unit testing step !!!"‘
                }
        }

        stage(‘Build‘) {
                docker.image(‘busybox‘).inside {
                        sh ‘echo  Build step !!!‘
                }
        }

        stage(‘Image Build And Push‘) {
                dir(‘baas-ops/oboe/cli‘) {
                     docker.withRegistry(‘https://registry.******.com:8080‘, ‘registry-hub-credentials‘) {
                                docker.build(‘oboe-cli‘).push(‘t1‘)
                        } 
                }

        }

        stage(‘Deploy images‘) {
                try {
                        sh ‘docker rm -f test‘
                } catch(e) {

                }
                docker.image(‘oboe-cli:t1‘).run(‘-p 80:3000 --name test‘)
        }
}

jenkins kubernetes-plugin 插件使用:

podTemplate(label: ‘test‘, securityContext: [ runAsUser: ‘root‘],
    containers: [
        containerTemplate(name: ‘jnlp‘, image: ‘registry.******.com:8088/jnlp-slave:alpine‘, args: ‘${computer.jnlpmac} ${computer.name}‘),
        containerTemplate(name: ‘docker‘, image: ‘docker:stable‘, ttyEnabled: true, command: ‘cat‘)
        ],
    volumes: [
            hostPathVolume(hostPath: ‘/var/run/docker.sock‘, mountPath: ‘/var/run/docker.sock‘),
        //   hostPathVolume(hostPath: ‘/tmp/test‘, mountPath: ‘/data‘),
            persistentVolumeClaim(claimName: ‘jenkins-slave-gfs‘, mountPath: ‘/home/jenkins‘, readOnly: false)
        ],
    ) {

    node(‘test‘) {
        def registryAddr=‘registry.******.com:8080‘

        stage(‘build image‘) {
                git credentialsId: ‘oschina-test‘, url: ‘git@gitee.com:yonchin/jenkins-test.git‘
                container(‘docker‘) {
                        sh "docker build -t ${registryAddr}/busybox:k8s ."
                }
        }

        stage(‘push image‘) {
                container(‘docker‘) {
                        withCredentials([usernamePassword(credentialsId: ‘registry-hub-credentials‘, passwordVariable: ‘registryPass‘, usernameVariable: ‘registryUser‘)]) {
                                sh "docker login ${registryAddr} -u ${env.registryUser} -p ${registryPass}"
                                sh "docker push ${registryAddr}/busybox:k8s"
                        }
                }
        }
    }
}

注: 其中 credentialsId 是要在jenkins -> credentials -> system -> Global credentials (unrestricted) -> Add credentials 中事先创建好。 其中,registryUser 和 registryPass 这两个变量会自动获取在‘Add credentials’中定义对用户名和密码。

jenkins docker-plugin 和 kubernetes-plugin插件实用案例

标签:tab   获取   nta   containe   ber   方法   step   stp   mac   

原文地址:http://blog.51cto.com/noican/2061008

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!