-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
executable file
·59 lines (43 loc) · 1.62 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
#!groovy
def isDeploymentBranch(branch) {
branch =~ /^master|develop_[0-9]$/
}
def PORT = 3001;
node {
def containerName = "tamagotchi_${env.BRANCH_NAME}"
def imageName = "tamagotchi-server:${env.BRANCH_NAME}"
def postgresHost = "172.17.0.3"
currentBuild.result = "SUCCESS"
try {
stage('Checkout') {
checkout scm
}
stage('Test') {
sh "make test TAG=${env.BUILD_ID}";
sh "make stop";
}
if (isDeploymentBranch(env.BRANCH_NAME)) {
stage('Create image') {
sh "make image TAG=${env.BRANCH_NAME} PORT=${PORT}"
}
stage('Run container') {
def containerExists = sh (script: "docker ps -f \"name=${containerName}\" --format '{{.Names}}'", returnStdout: true).trim()
if (containerExists) {
sh "docker stop ${containerName}"
sh "docker rm ${containerName}"
}
sh "docker run --network bridge -e NODE_ENV=production -e PORT=${PORT} -e POSTGRES_HOST=${postgresHost} -e POSTGRES_DB=\"tamagotchi_${env.BRANCH_NAME}\" --name ${containerName} -p ${PORT}:${PORT} -d ${imageName}"
}
}
stage('Cleanup') {
def imageExists = sh (script: "docker images --filter=reference='tamagotchi-*:${env.BUILD_ID}' --format '{{.Repository}}'", returnStdout: true).trim()
if (imageExists) {
sh "docker rmi tamagotchi-server:${env.BUILD_ID}"
}
}
}
catch (err) {
currentBuild.result = "FAILURE"
throw err
}
}