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’中定义对用户名和密码。