forked from harman052/react-tutorial-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
126 lines (105 loc) · 3.44 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
121
122
123
124
125
126
pipeline {
agent { label 'controller-node' }
stages {
stage('unit tests') {
steps {
sh 'npm install'
sh 'npm test'
}
}
stage('formatting') {
steps {
sh 'npm run prettify'
}
}
stage('linting') {
steps {
sh 'npm run lint'
}
}
stage('audit') {
steps {
sh 'npm audit || true'
}
}
stage('analysis') {
steps {
sh 'npm run build'
sh 'mkdir reports || true'
sh 'npm run analyze > reports/analyzer.html'
// publish html
publishHTML (target: [
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: false,
reportDir: 'reports',
reportFiles: 'analyzer.html',
reportName: "Bundle Analysis"
])
}
}
stage('build image') {
steps {
sh 'docker build -t public.ecr.aws/u8c3s4x8/tic-tac-toe:latest .'
}
}
stage('push image to ECR') {
environment {
AWS_ACCESS_KEY_ID = credentials('jenkins-aws-secret-key-id')
AWS_SECRET_ACCESS_KEY = credentials('jenkins-aws-secret-access-key')
}
steps {
sh 'aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin public.ecr.aws/u8c3s4x8'
sh 'docker push public.ecr.aws/u8c3s4x8/tic-tac-toe:latest'
}
}
stage('deploy') {
steps {
sh 'docker run --name app-${GIT_COMMIT} -d --network siemens public.ecr.aws/u8c3s4x8/tic-tac-toe:latest'
script {
def containerHealth = sh(
script: "docker inspect --format='{{json .State.Running}}' app-${GIT_COMMIT} || echo 'false'",
returnStdout: true
).trim()
echo "container health: ${containerHealth}"
if(containerHealth == "true") {
sh "sed -i 's|^ proxy_pass http://app-.*:80;\$| proxy_pass http://app-${GIT_COMMIT}:80;|' /home/mariamfahmy2498/tic-tac-toe-devops-project/conf/nginx/default.conf"
sh 'docker stop app-${GIT_PREVIOUS_COMMIT} || true'
echo "green deployment: app-${GIT_PREVIOUS_COMMIT}"
echo "blue deployyment: app-${GIT_COMMIT}"
}
}
script {
def proxyHealth = sh(
script: "docker inspect --format='{{json .State.Running}}' proxy-server || echo 'false'",
returnStdout: true
).trim()
echo "proxy health: ${proxyHealth}"
if(proxyHealth == "false") {
//sh 'docker rm -f proxy-server || true'
sh 'docker run --name proxy-server -p 80:80 -v /home/mariamfahmy2498/tic-tac-toe-devops-project/conf/nginx/:/etc/nginx/conf.d/:ro -d --network siemens nginx:1.21.6-alpine'
} else {
sh 'docker kill -s HUP proxy-server'
}
}
}
}
stage('E2E tests') {
steps {
sh 'npm run test:e2e'
}
}
}
post {
failure {
mail to: "[email protected]",
subject: "jenkins build:${currentBuild.currentResult}: ${env.JOB_NAME}",
body: "${currentBuild.currentResult}: Job ${env.JOB_NAME}\nMore Info can be found here: ${env.BUILD_URL}"
}
success {
mail to: "[email protected]",
subject: "jenkins build:${currentBuild.currentResult}: ${env.JOB_NAME}",
body: "${currentBuild.currentResult}: Job ${env.JOB_NAME}\nMore Info can be found here: ${env.BUILD_URL}"
}
}
}