forked from BretFisher/node-docker-good-defaults
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
67 lines (65 loc) · 2.88 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
pipeline {
agent any
environment {
git_repo = "https://github.com/Filip3Kx/node-app-kubernetes"
git_branch = "dev"
registry_url = "europe-west1-docker.pkg.dev"
registry_path = "/node-app-399813/node-app/"
deployment_name = "deployment.apps/app-node-deployment"
bucket_name = "gs://node-app-bucket2/"
}
stages{
stage("Clone repo to local") {
steps{
git branch: "${git_branch}", url: "${git_repo}"
}
}
stage("Run unit tests") {
steps {
sh 'docker compose up -d'
sh 'sleep 10'
sh 'docker compose exec node npm test > docker_test.txt'
sh 'docker compose logs > docker_logs.txt'
sh 'docker compose down'
}
}
stage("Push image to Artifact Registry") {
steps {
//one image as latest and other as BUILD_ID for refrenece
sh "gcloud auth print-access-token | docker login -u oauth2accesstoken --password-stdin https://${registry_url}"
sh "docker tag node-app-node:latest ${registry_url}${registry_path}node-app-node:latest"
sh "docker push ${registry_url}${registry_path}node-app-node:latest"
sh "docker tag node-app-node:latest ${registry_url}${registry_path}node-app-node:${BUILD_ID}"
sh "docker push ${registry_url}${registry_path}node-app-node:${BUILD_ID}"
}
}
stage("Deploy to GKE cluster") {
steps{
sh "gcloud container clusters get-credentials node-app-cluster --region europe-west1 --project node-app-399813"
sh "kubectl set image ${deployment_name} app-node-pod=${registry_url}${registry_path}node-app-node:${BUILD_ID} -n node-app"
sh "sleep 10"
sh "kubectl describe ${deployment_name} -n node-app > k8s_logs.txt"
sh "kubectl get pods -l app=node -o custom-columns=:metadata.name --no-headers -n node-app | xargs -I {} kubectl describe pod {} -n node-app >> k8s_logs.txt"
}
}
stage("Upload logs to GCS Bucket") {
steps{
sh "gsutil cp docker_test.txt ${bucket_name}${BUILD_ID}/"
sh "gsutil cp docker_logs.txt ${bucket_name}${BUILD_ID}/"
sh "gsutil cp k8s_logs.txt ${bucket_name}${BUILD_ID}/"
}
}
stage("Images Cleanup") {
steps{
sh "docker rmi node-app-node"
sh "docker rmi ${registry_url}${registry_path}node-app-node:latest"
sh "docker rmi ${registry_url}${registry_path}node-app-node:${BUILD_ID}"
}
}
}
post{
always {
cleanWs(cleanWhenSuccess: true, deleteDirs: true, notFailBuild: true)
}
}
}