forked from caicloud/cyclone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
184 lines (168 loc) · 7.77 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
def server_tag = "caicloud/cyclone-server:${params.imageTag}"
def worker_tag = "caicloud/cyclone-worker:${params.imageTag}"
def registry = "cargo.caicloudprivatetest.com"
podTemplate(
cloud: 'dev-cluster',
namespace: 'kube-system',
name: 'cyclone',
label: 'cyclone',
// idleMinutes: 1440,
containers: [
// jnlp with kubectl
containerTemplate(
name: 'jnlp',
alwaysPullImage: true,
image: 'cargo.caicloud.io/caicloud/jnlp-slave:3.14-1-alpine',
command: '',
args: '${computer.jnlpmac} ${computer.name}',
),
// docker in docker
containerTemplate(
name: 'dind',
image: 'cargo.caicloud.io/caicloud/docker:17.09-dind',
ttyEnabled: true,
command: '',
args: '--host=unix:///home/jenkins/docker.sock',
privileged: true,
),
// golang with docker client
containerTemplate(
name: 'golang',
image: 'cargo.caicloud.io/caicloud/golang-docker:1.9-17.09',
ttyEnabled: true,
command: '',
args: '',
envVars: [
containerEnvVar(key: 'DEBUG', value: 'true'),
containerEnvVar(key: 'MONGODB_HOST', value: '127.0.0.1:27017'),
containerEnvVar(key: 'KAFKA_HOST', value: '127.0.0.1:9092'),
containerEnvVar(key: 'REGISTRY_LOCATION', value: 'cargo.caicloud.io'),
containerEnvVar(key: 'REGISTRY_USERNAME', value: 'caicloudadmin'),
containerEnvVar(key: 'REGISTRY_PASSWORD', value: 'caicloudadmin'),
containerEnvVar(key: 'DOCKER_HOST', value: 'unix:///home/jenkins/docker.sock'),
containerEnvVar(key: 'DOCKER_API_VERSION', value: '1.26'),
containerEnvVar(key: 'WORKDIR', value: '/go/src/github.com/caicloud/cyclone')
],
),
containerTemplate(
name: 'zk',
image: 'cargo.caicloud.io/caicloud/zookeeper:3.4.6',
ttyEnabled: true,
command: "",
args: "",
),
containerTemplate(
name: 'kafka',
image: 'cargo.caicloud.io/caicloud/kafka:0.10.1.0',
ttyEnabled: true,
command: '',
args: '',
envVars: [
containerEnvVar(key: 'KAFKA_ADVERTISED_HOST_NAME', value: '0.0.0.0'),
containerEnvVar(key: 'KAFKA_ADVERTISED_PORT', value: '9092'),
containerEnvVar(key: 'KAFKA_ZOOKEEPER_CONNECT', value: 'localhost:2181'),
containerEnvVar(key: 'KAFKA_ZOOKEEPER_CONNECTION_TIMEOUT_MS', value: '60000'),
],
),
containerTemplate(
name: 'mongo',
image: 'cargo.caicloud.io/caicloud/mongo:3.0.5',
ttyEnabled: true,
command: 'mongod',
args: '--smallfiles',
),
]
) {
node('cyclone') {
stage('Checkout') {
checkout scm
}
container('golang') {
ansiColor('xterm') {
stage("Complie") {
sh('''
set -e
mkdir -p $(dirname ${WORKDIR})
# if you do not remove target dir manually
# ln will not work according to what you want
# ln link /home/jenkins/workspace/xxxx to /go/src/github.com/caicloud/cyclone at first time
# ln will link /home/jenkins/workspace/xxxx to /go/src/github.com/caicloud/cyclone/xxxx at second time
# so remove the target workdir before you link
rm -rf ${WORKDIR}
ln -sfv $(pwd) ${WORKDIR}
cd ${WORKDIR}
echo "buiding server"
go build -i -v -o cyclone-server github.com/caicloud/cyclone/cmd/server
echo "buiding worker"
go build -i -v -o cyclone-worker github.com/caicloud/cyclone/cmd/worker
docker build -t cargo.caicloudprivatetest.com/caicloud/cyclone-worker:e2e -f Dockerfile.worker .
''')
}
stage('Run e2e test') {
if (params.integration) {
echo "exec integration"
sh('''
set -e
cd ${WORKDIR}
# get host ip
HOST_IP=$(ifconfig eth0 | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
export CYCLONE_SERVER=http://${HOST_IP}:7099
export LOG_SERVER=ws://${HOST_IP}:8000/ws
# kill cyclone server process left by last build
cyclone_pid=$(ps -ef | grep cyclone-server | grep -v "grep" | awk '{print $1}')
if [[ -n "${cyclone_pid}" ]]; then
kill -9 ${cyclone_pid}
fi
echo "start server"
./cyclone-server --cloud-auto-discovery=false --log-force-color=true &
echo "testing ..."
# go test compile
go test -i ./tests/...
# go test
go test -v ./tests/service
go test -v ./tests/version
go test -v ./tests/yaml
''')
} else {
echo "skip integration"
}
}
}
stage("Build image and publish") {
if (params.publish) {
echo "exec publish"
sh "docker build -t ${server_tag} -f Dockerfile.server ."
sh "docker build -t ${worker_tag} -f Dockerfile.worker ."
docker.withRegistry("https://${registry}", "cargo-private-admin") {
docker.image(server_tag).push()
docker.image(worker_tag).push()
}
if (params.autoGitTag) {
echo "auto git tag: " + params.imageTag
withCredentials ([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'caicloud-bot', usernameVariable: 'GIT_USERNAME', passwordVariable: 'GIT_PASSWORD']]){
sh("git config --global user.email \"[email protected]\"")
sh("git tag -a $imageTag -m \"$tagDescribe\"")
sh("git push https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/caicloud/cyclone $imageTag")
}
}
} else {
echo "skip publish"
}
}
}
stage("deploy") {
if (params.deploy) {
echo "exec deploy"
echo "deploy to ${params.deployTarget} cluster"
def kubeconfig = "kubeconfig-${params.deployTarget}"
withCredentials([[$class: 'FileBinding', credentialsId: kubeconfig, variable: 'SECRET_FILE']]) {
sh("""
kubectl --kubeconfig=$SECRET_FILE --namespace cyclone get deploy circle-server-v0.1.1 -o yaml | sed 's/cyclone-server:.*\$/cyclone-server:${params.imageTag}/; s/cyclone-worker:.*\$/cyclone-worker:${params.imageTag}/' | kubectl --kubeconfig=$SECRET_FILE --namespace cyclone replace -f -
""")
}
} else {
echo "skip deploy"
}
}
}
}