forked from omgnetwork/ewallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
120 lines (107 loc) · 4.74 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
podTemplate(
label: 'ewallet',
containers: [
containerTemplate(name: 'jnlp', image: 'gcr.io/omise-go/jenkins-slave', args: '${computer.jnlpmac} ${computer.name}'),
containerTemplate(name: 'postgresql', image: 'postgres:9.6'),
],
volumes: [
hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
hostPathVolume(mountPath: '/usr/bin/docker', hostPath: '/usr/bin/docker'),
]
) {
node('ewallet') {
Random random = new Random()
def tmpDir = pwd(tmp: true)
def project = 'omisego'
def appName = 'ewallet'
def imageName = "${project}/${appName}"
def nodeIP = getNodeIP()
def gitCommit
stage('Checkout') {
checkout scm
}
stage('Build') {
gitCommit = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
sh("docker build --pull . -t ${imageName}:${gitCommit}")
}
stage('Test') {
container('postgresql') {
sh("pg_isready -t 60 -h localhost -p 5432")
}
sh(
"""
docker run \
--rm \
--entrypoint /bin/execlineb \
-e DATABASE_URL="postgresql://postgres@${nodeIP}:5432/ewallet_${gitCommit}_ewallet" \
-e LOCAL_LEDGER_DATABASE_URL="postgresql://postgres@${nodeIP}:5432/ewallet_${gitCommit}_local_ledger" \
${imageName}:${gitCommit} \
-P -c " \
s6-setuidgid ewallet \
s6-env HOME=/tmp/ewallet \
s6-env MIX_ENV=test \
cd /app \
mix do format --check-formatted, credo, ecto.create, ecto.migrate, test \
" \
""".stripIndent()
)
}
if (env.BRANCH_NAME == 'develop') {
stage('Push') {
withCredentials([file(credentialsId: 'docker', variable: 'DOCKER_CONFIG')]) {
def configDir = sh(script: "dirname ${DOCKER_CONFIG}", returnStdout: true).trim()
sh("docker --config=${configDir} tag ${imageName}:${gitCommit} ${imageName}:latest")
sh("docker --config=${configDir} push ${imageName}:${gitCommit}")
sh("docker --config=${configDir} push ${imageName}:latest")
}
}
stage('Deploy') {
dir("${tmpDir}/deploy") {
checkout([
$class: 'GitSCM',
branches: [[name: '*/master']],
userRemoteConfigs: [
[
url: 'ssh://[email protected]/omisego/kube.git',
credentialsId: 'github',
],
]
])
sh("sed -i.bak 's#${imageName}:latest#${imageName}:${gitCommit}#' staging/k8s/ewallet/ewallet.yaml")
sh("kubectl apply -f staging/k8s/ewallet/ewallet.yaml")
sh("kubectl rollout status --namespace=staging deployment/ewallet")
def podID = getPodID('--namespace=staging -l app=ewallet')
sh(
"""
kubectl exec ${podID} --namespace=staging -- \
/bin/execlineb -P -c " \
s6-setuidgid ewallet \
s6-env HOME=/tmp/ewallet \
mix ecto.migrate \
" \
""".stripIndent()
)
}
}
} else if (env.BRANCH_NAME == 'master') {
stage('Push') {
withCredentials([file(credentialsId: 'docker', variable: 'DOCKER_CONFIG')]) {
def configDir = sh(script: "dirname ${DOCKER_CONFIG}", returnStdout: true).trim()
sh("docker --config=${configDir} tag ${imageName}:${gitCommit} ${imageName}:stable")
sh("docker --config=${configDir} push ${imageName}:${gitCommit}")
sh("docker --config=${configDir} push ${imageName}:stable")
}
}
}
}
}
String getNodeIP() {
def rawNodeIP = sh(script: 'ip -4 -o addr show scope global', returnStdout: true).trim()
def matched = (rawNodeIP =~ /inet (\d+\.\d+\.\d+\.\d+)/)
return "" + matched[0].getAt(1)
}
String getPodID(String opts) {
def pods = sh(script: "kubectl get pods ${opts} -o name", returnStdout: true).trim()
def matched = (pods.split()[0] =~ /pods\/(.+)/)
return "" + matched[0].getAt(1)
}