-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
111 lines (110 loc) · 3.54 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
pipeline {
environment {
IMAGE_NAME = "tonydja/static-website"
IMAGE_TAG = "latest"
CONTAINER = "website"
CONTAINER_DYNOS_HEROKU = "web"
STAGING = "tonydja-staging"
PRODCUTION = "tonydja-production"
LOCALHOST_DOCKER_NETWORK = "192.168.208.3"
}
agent none
stages {
stage('Build image') {
agent any
steps {
script {
sh 'docker build -t $IMAGE_NAME:$IMAGE_TAG .'
}
}
}
stage('Run docker container based on build image') {
agent any
steps {
script {
sh '''
docker run -d -p 3000:80 --name $CONTAINER ${IMAGE_NAME}:${IMAGE_TAG}
sleep 5
'''
}
}
}
/* Just Replace the IP address of ENV var "LOCALHOST_DOCKER_NETWORK" with your own local Docker container IP
stage('Test image') {
agent any
steps {
script {
sh 'curl http://$LOCALHOST_DOCKER_NETWORK:3000 | grep -q "Enjoy"'
}
}
}
*/
stage('Clean container') {
agent any
steps {
script {
sh '''
docker container stop $CONTAINER
docker container rm $CONTAINER
'''
}
}
}
stage('Push image in STAGING and Deploy') {
when {
expression { GIT_BRANCH == 'origin/main'}
}
agent {
docker {
image 'franela/dind'
args '-u root:root -v /var/run/docker.sock:/var/run/docker.sock'
}
}
environment {
HEROKU_API_KEY = credentials('HEROKU_API_KEY')
}
steps {
script {
sh '''
apk --no-cache add npm
npm install -g heroku
ls -l /var/run/docker.sock
docker ps
heroku container:login
heroku create $STAGING || echo "project already exist"
heroku container:push -a $STAGING $CONTAINER_DYNOS_HEROKU
heroku container:release -a $STAGING $CONTAINER_DYNOS_HEROKU
'''
}
}
}
stage('Push image in PRODUCTION and Deploy') {
when {
expression { GIT_BRANCH == 'origin/main'}
}
agent {
docker {
image 'franela/dind'
args '-u root:root -v /var/run/docker.sock:/var/run/docker.sock'
}
}
environment {
HEROKU_API_KEY = credentials('HEROKU_API_KEY')
}
steps {
script {
sh '''
apk --no-cache add npm
npm install -g heroku
ls -l /var/run/docker.sock
docker ps
heroku container:login
heroku create $PRODCUTION || echo "project already exist"
heroku container:push -a $PRODCUTION $CONTAINER_DYNOS_HEROKU
heroku container:release -a $PRODCUTION $CONTAINER_DYNOS_HEROKU
'''
}
}
}
}
}