forked from whiteblock/genesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
114 lines (111 loc) · 4.22 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
@Library('whiteblock-dev')_
def DEFAULT_BRANCH = 'master'
pipeline {
agent any
environment {
//General
APP_NAME = "genesis"
GCP_REGION = credentials('GCP_REGION')
APP_NAMESPACE = "apps"
CHART_PATH = "chart"
KUBEDOG_VERSION = "0.3.2"
REV_SHORT = sh(script: "git rev-parse HEAD", returnStdout: true).trim()
INFRA_REPO_URL = credentials('INFRA_REPO_URL')
CODECOV_TOKEN = credentials('CODECOV_TOKEN')
CI_ENV = sh(script: "curl -s https://codecov.io/env | bash", , returnStdout: true).trim()
// Dev
DEV_GCP_PROJECT_ID = credentials('DEV_GCP_PROJECT_ID')
DEV_GKE_CLUSTER_NAME = credentials('DEV_GKE_CLUSTER_NAME')
DEV_KUBE_CONTEXT = "gke_${DEV_GCP_PROJECT_ID}_${GCP_REGION}_${DEV_GKE_CLUSTER_NAME}"
IMAGE_REPO = "gcr.io/${DEV_GCP_PROJECT_ID}"
}
options {
disableConcurrentBuilds()
buildDiscarder(logRotator(numToKeepStr: '10', artifactNumToKeepStr: '10'))
}
stages {
stage('Run tests') {
agent {
docker {
image "golang:1.13.4-alpine"
args "-u root ${CI_ENV}"
}
}
when {
beforeAgent true
anyOf {
changeRequest target: DEFAULT_BRANCH
}
}
steps {
script {
sh "apk add git gcc libc-dev curl bash make"
sh "go get github.com/vektra/mockery/.../"
sh "go get -u golang.org/x/lint/golint"
sh "sh tests.sh"
}
}
post {
success {
sh ":"
}
cleanup {
sh "chmod -R 777 coverage.txt mocks || true"
deleteDir()
}
}
}
stage('Build docker image') {
when { branch DEFAULT_BRANCH }
steps {
script {
withCredentials([file(credentialsId: 'google-infra-dev-auth', variable: 'GOOGLE_APPLICATION_CREDENTIALS')]) {
sh """
gcloud auth activate-service-account --key-file ${GOOGLE_APPLICATION_CREDENTIALS}
gcloud auth configure-docker
"""
sh "docker pull ${IMAGE_REPO}/${APP_NAME}:${BRANCH_NAME} || true"
docker.build("${IMAGE_REPO}/${APP_NAME}",
"--cache-from ${IMAGE_REPO}/${APP_NAME}:${BRANCH_NAME} .")
docker.image("${IMAGE_REPO}/${APP_NAME}").push("${BRANCH_NAME}")
docker.image("${IMAGE_REPO}/${APP_NAME}").push("${REV_SHORT}")
}
}
}
}
stage('Deploy to dev cluster') {
when { branch DEFAULT_BRANCH }
steps {
script {
withCredentials([file(credentialsId: 'google-infra-dev-auth', variable: 'GOOGLE_APPLICATION_CREDENTIALS')]) {
sh "gcloud auth activate-service-account --key-file ${GOOGLE_APPLICATION_CREDENTIALS}"
sh "gcloud config set project ${DEV_GCP_PROJECT_ID}"
sh "gcloud container clusters get-credentials ${DEV_GKE_CLUSTER_NAME} --project ${DEV_GCP_PROJECT_ID} --region ${GCP_REGION}"
sh "curl -L https://dl.bintray.com/flant/kubedog/v${KUBEDOG_VERSION}/kubedog-linux-amd64-v${KUBEDOG_VERSION} -o kubedog"
sh "chmod +x kubedog"
sh "helm init --kube-context=${DEV_KUBE_CONTEXT} --tiller-namespace helm --service-account tiller --history-max 10 --client-only"
sh "helm upgrade ${APP_NAME} ${CHART_PATH}/${APP_NAME} --install --namespace ${APP_NAMESPACE} --tiller-namespace helm \
--set-string image.tag=${REV_SHORT} \
--set-string image.repository=${IMAGE_REPO}/${APP_NAME}"
sh "echo '{\"Deployments\": [{\"ResourceName\": \"'${APP_NAME}'\",\"Namespace\": \"'${APP_NAMESPACE}'\"}]}' | ./kubedog multitrack -t 60"
}
}
}
}
}
post {
failure {
sh "docker network prune --force"
sh "docker volume prune --force"
}
always {
// slack notify first to ignore teardown errors
slackNotify(env.BRANCH_NAME == DEFAULT_BRANCH)
sh "kubectl config delete-context ${DEV_KUBE_CONTEXT} || true"
sh "gcloud auth revoke || true"
deleteDir()
sh "/usr/bin/docker container prune --force --filter 'until=3h'"
sh "/usr/bin/docker image prune --force --filter 'until=72h'"
}
}
}