forked from Yan316/example-product-service
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
90 lines (80 loc) · 3.25 KB
/
Jenkinsfile
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def label = "example-product-service-slave"
podTemplate(
label: label,
cloud: 'kubernetes',
containers: [
containerTemplate(name: 'jnlp', image: 'amuguelove/jenkins-jnlp:4.3', ttyEnabled: true)
],
serviceAccount: 'jenkins',
volumes: [
hostPathVolume(mountPath: '/usr/bin/docker', hostPath: '/usr/bin/docker'),
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
hostPathVolume(mountPath: '/root/.gradle', hostPath: '/opt/data/gradle'),
hostPathVolume(mountPath: '/usr/bin/kubectl', hostPath: '/usr/bin/kubectl'),
hostPathVolume(mountPath: '/root/.kube/', hostPath: '/root/.kube/'),
hostPathVolume(mountPath: '/usr/bin/docker-compose', hostPath: '/usr/bin/docker-compose'),
]
) {
node(label) {
def IMAGE_NAME = 'ccr.ccs.tencentyun.com/my-registry/example-product-service'
def IMAG_TAG = "${env.Build_TIMESTAMP}.${env.BUILD_ID}"
stage('clone repository') {
checkout([
$class: 'GitSCM',
branches: [[name: '*/master']],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'CleanBeforeCheckout']],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'github', url: 'https://github.com/amuguelove/example-product-service.git']]])
echo 'Checkout'
}
stage('build project') {
sh "LOCAL_ADDRESS=172.27.0.15 ./gradlew clean build -Dorg.gradle.daemon=false"
publishHTML target: [
allowMissing : false,
alwaysLinkToLastBuild: true,
keepAll : true,
reportDir : 'build/reports/jacoco/test/html',
reportFiles : 'index.html',
reportName : 'Jacoco Report'
]
publishHTML target: [
allowMissing : false,
alwaysLinkToLastBuild: true,
keepAll : true,
reportDir : 'build/reports/tests/integrationTest',
reportFiles : 'index.html',
reportName : 'Test Report'
]
}
stage('push image') {
withCredentials([[$class: 'UsernamePasswordMultiBinding',
credentialsId: 'DOCKER_CREDENTIALS',
usernameVariable: 'DOCKER_CREDENTIALS_USR',
passwordVariable: 'DOCKER_CREDENTIALS_PSW']]) {
sh """
docker login ccr.ccs.tencentyun.com -u ${DOCKER_CREDENTIALS_USR} -p ${DOCKER_CREDENTIALS_PSW}
docker build -t ${IMAGE_NAME}:${IMAG_TAG} .
docker push ${IMAGE_NAME}:${IMAG_TAG}
"""
}
}
stage('deploy to dev') {
deployToStage('dev', 'dev', IMAG_TAG)
}
}
}
def deployToStage(ns, stage, imageTag) {
withKubeConfig(
caCertificate: '',
contextName: '',
credentialsId: 'k8s-credentials',
serverUrl: 'https://cls-2vcqd9cl.ccs.tencent-cloud.com'
) {
sh "echo 172.27.0.5 cls-2vcqd9cl.ccs.tencent-cloud.com >> /etc/hosts"
sh """
sed -i '' -e \'s/example-product-service:latest/example-product-service:${imageTag}/g\' './deploy/tencent/app-${stage}.yaml'
kubectl -n ${ns ?: "default"} apply -f './deploy/tencent/app-${stage}.yaml' --force
"""
}
}