forked from superstreamlabs/memphis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
238 lines (206 loc) · 8.59 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
def gitBranch = env.BRANCH_NAME
def imageName = "memphis"
def gitURL = "git@github.com:Memphisdev/memphis.git"
def repoUrlPrefix = "memphisos"
def test_suffix = "test"
String unique_id = org.apache.commons.lang.RandomStringUtils.random(4, false, true)
node {
git credentialsId: 'main-github', url: gitURL, branch: gitBranch
def versionTag = readFile "./version.conf"
try{
stage('Login to Docker Hub') {
withCredentials([usernamePassword(credentialsId: 'docker-hub', usernameVariable: 'DOCKER_HUB_CREDS_USR', passwordVariable: 'DOCKER_HUB_CREDS_PSW')]) {
sh 'docker login -u $DOCKER_HUB_CREDS_USR -p $DOCKER_HUB_CREDS_PSW'
}
}
/*
stage('UI build'){
dir ('ui_src'){
sh """
npm install
CI=false npm run build
"""
}
}
*/
stage('Create memphis namespace in Kubernetes'){
sh """
kubectl config use-context minikube
kubectl create namespace memphis-$unique_id --dry-run=client -o yaml | kubectl apply -f -
aws s3 cp s3://memphis-jenkins-backup-bucket/regcred.yaml .
kubectl apply -f regcred.yaml -n memphis-$unique_id
kubectl patch serviceaccount default -p '{\"imagePullSecrets\": [{\"name\": \"regcred\"}]}' -n memphis-$unique_id
"""
}
stage('Build and push docker image to Docker Hub') {
sh "docker buildx build --push -t ${repoUrlPrefix}/${imageName}-${gitBranch}-${test_suffix} ."
}
stage('Tests - Install/upgrade Memphis cli') {
sh """
sudo npm uninstall memphis-dev-cli
sudo npm i memphis-dev-cli -g
"""
}
////////////////////////////////////////
//////////// Docker-Compose ////////////
////////////////////////////////////////
stage('Tests - Docker compose install') {
sh "rm -rf memphis-docker"
dir ('memphis-docker'){
git credentialsId: 'main-github', url: 'git@github.com:memphisdev/memphis-docker.git', branch: gitBranch
}
sh "docker-compose -f ./memphis-docker/docker-compose-dev-tests-broker.yml -p memphis up -d"
}
stage('Tests - Run e2e tests over Docker') {
sh "rm -rf memphis-e2e-tests"
dir ('memphis-e2e-tests'){
git credentialsId: 'main-github', url: 'git@github.com:memphisdev/memphis-e2e-tests.git', branch: 'master'
}
sh "npm install --prefix ./memphis-e2e-tests"
sh "node ./memphis-e2e-tests/index.js docker"
}
stage('Tests - Remove Docker compose') {
sh """
docker-compose -f ./memphis-docker/docker-compose-dev-tests-broker.yml -p memphis down
docker volume prune -f
"""
}
////////////////////////////////////////
//////////// Kubernetes ////////////
////////////////////////////////////////
stage('Tests - Install memphis with helm') {
sh "rm -rf memphis-k8s"
dir ('memphis-k8s'){
git credentialsId: 'main-github', url: 'git@github.com:memphisdev/memphis-k8s.git', branch: gitBranch
sh "helm install memphis-tests memphis --set analytics='false',teston='cp' --create-namespace --namespace memphis-$unique_id --wait"
}
}
stage('Open port forwarding to memphis service') {
sh """
until kubectl get pods --selector=app.kubernetes.io/name=memphis -o=jsonpath="{.items[*].status.phase}" -n memphis-$unique_id | grep -q "Running" ; do sleep 1; done
nohup kubectl port-forward service/memphis 6666:6666 9000:9000 7770:7770 --namespace memphis-$unique_id &
"""
}
stage('Tests - Run e2e tests over kubernetes') {
sh """
npm install --prefix ./memphis-e2e-tests
node ./memphis-e2e-tests/index.js kubernetes memphis-$unique_id
"""
}
stage('Tests - Uninstall helm') {
sh """
helm uninstall memphis-tests -n memphis-$unique_id
kubectl delete ns memphis-$unique_id &
/usr/sbin/lsof -i :6666,9000 | grep kubectl | awk '{print \"kill -9 \"\$2}' | sh
"""
}
////////////////////////////////////////
//////////// Build & Push ////////////
////////////////////////////////////////
stage('Build and push image to Docker Hub') {
sh "docker buildx use builder"
if (env.BRANCH_NAME ==~ /(master)/) { //NEW TAG
sh "docker buildx build --push --tag ${repoUrlPrefix}/${imageName}-${gitBranch} --platform linux/amd64,linux/arm64 ."
}
else{
sh "docker buildx build --push --tag ${repoUrlPrefix}/${imageName}:${versionTag} --tag ${repoUrlPrefix}/${imageName} --platform linux/amd64,linux/arm64 ."
}
}
//////////////////////////////////////
////////////// MASTER //////////////
//////////////////////////////////////
if (env.BRANCH_NAME ==~ /(master)/) {
stage('Push to staging'){
sh """
aws eks --region eu-central-1 update-kubeconfig --name staging-cluster
helm uninstall my-memphis --kubeconfig ~/.kube/config -n memphis
kubectl get pvc -n memphis | grep -v NAME| awk '{print\$1}' | while read vol; do kubectl delete pvc \$vol -n memphis; done
"""
dir ('memphis-k8s'){
git credentialsId: 'main-github', url: 'git@github.com:memphisdev/memphis-k8s.git', branch: gitBranch
sh 'helm install my-memphis memphis --set analytics="false",cluster.enabled="true",exporter.enabled="true",websocket.tls.cert="tls.crt",websocket.tls.key="tls.key",websocket.tls.secret.name="ws-tls-certs" --create-namespace --namespace memphis --wait'
}
sh "rm -rf memphis-k8s"
}
stage('Open port forwarding to memphis service') {
sh """
until kubectl get pods --selector=app.kubernetes.io/name=memphis -o=jsonpath="{.items[*].status.phase}" -n memphis | grep -q "Running" ; do sleep 1; done
nohup kubectl port-forward service/memphis 6666:6666 9000:9000 7770:7770 --namespace memphis &
"""
}
stage('Tests - Run e2e tests over memphis cluster') {
sh """
npm install --prefix ./memphis-e2e-tests
node ./memphis-e2e-tests/index.js kubernetes memphis
"""
}
stage('Install memphis CLI') {
sh """
sudo npm i memphis-dev-cli -g
"""
}
stage('Create staging user') {
sh """
mem connect -s localhost -u root -p \$(kubectl get secret memphis-creds -n memphis -o jsonpath="{.data.ROOT_PASSWORD}" | base64 --decode)
mem user add -u staging -p memphis
"""
}
stage('Tests - remove port-forwarding') {
sh(script: """/usr/sbin/lsof -i :6666,9000 | grep kubectl | awk '{print \"kill -9 \"\$2}' | sh""", returnStdout: true)
}
stage('Tests - Remove used directories') {
sh "rm -rf memphis-e2e-tests"
}
}
//////////////////////////////////////////////////////////
////////////// Checkout to version branch //////////////
//////////////////////////////////////////////////////////
if (env.BRANCH_NAME ==~ /(latest)/) {
stage('checkout to version branch'){
withCredentials([sshUserPrivateKey(keyFileVariable:'check',credentialsId: 'main-github')]) {
sh """
git reset --hard origin/latest
GIT_SSH_COMMAND='ssh -i $check' git checkout -b ${versionTag}
GIT_SSH_COMMAND='ssh -i $check' git push --set-upstream origin ${versionTag}
"""
}
}
stage('Install gh'){
sh """
sudo yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
sudo yum install gh -y
"""
}
stage('Create new release') {
withCredentials([string(credentialsId: 'gh_token', variable: 'GH_TOKEN')]) {
sh "gh release create v${versionTag}-beta --generate-notes"
}
}
}
notifySuccessful()
} catch (e) {
currentBuild.result = "FAILED"
sh(script: """docker ps | grep memphisos/ | awk '{print \"docker rm -f \"\$1}' | sh""", returnStdout: true)
sh "docker volume prune -f"
sh "kubectl delete ns memphis-$unique_id &"
cleanWs()
notifyFailed()
throw e
}
}
def notifySuccessful() {
emailext (
subject: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """<p>SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Check console output at "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""",
recipientProviders: [requestor()]
)
}
def notifyFailed() {
emailext (
subject: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
body: """<p>FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
<p>Check console output at "<a href='${env.BUILD_URL}'>${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""",
recipientProviders: [requestor()]
)
}