1. git -> dockerhub -> kubernetes

  • 适用jenkins运行在kubernetes中。
  • 需要安装git和kubernetes插件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
podTemplate(cloud: '<cloud-name>', yaml:
'''
apiVersion: v1
kind: Pod
spec:
containers:
- name: docker-kubectl
image: <docker-kubectl-image>
securityContext:
privileged: true
''') {
node(POD_LABEL) {
def myRepo = checkout scm
def commitHash = sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
def imageTag = "<dockerhub-username>/<reponame>:${commitHash}"

stage('构建镜像并部署') {
container('构建镜像并部署') {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'dockerhub',
usernameVariable: 'DOCKER_HUB_USER',
passwordVariable: 'DOCKER_HUB_PASSWORD']]) {
container('docker') {
sh """
docker login -u ${DOCKER_HUB_USER} -p ${DOCKER_HUB_PASSWORD}
docker build -t ${imageTag} .
docker tag ${imageTag} ${imageTag}
docker push ${imageTag}
"""
withKubeConfig([namespace: "star"]) {
sh """
kubectl set image deployment/<deployment-name> <container-name>=${imageTag} --record
"""
}
}
}
}
}
}
}