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

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: '[email protected]: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’中定義對用戶名和密碼。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章