-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile01
112 lines (97 loc) · 3.79 KB
/
Jenkinsfile01
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
pipeline {
agent {
docker {
image 'docker:19.03.12-dind'
args '-u root --privileged -v /var/run/docker.sock:/var/run/docker.sock' // Docker socket and privileges
}
}
environment {
DOCKERHUB_CRED_ID = 'dockerhub-token'
}
stages {
stage('Checkout') {
steps {
// Checkout the code from GitHub
git url: 'https://github.com/ahmvdshafiq/realtimeChatAppCicd', branch: 'master'
}
}
stage('Install Node.js') {
steps {
script {
// Install Node.js v16.20.2 along with required dependencies
sh '''
apk add --no-cache curl libstdc++ gcc g++
curl -fsSL https://unofficial-builds.nodejs.org/download/release/v16.20.2/node-v16.20.2-linux-x64-musl.tar.xz | tar -xJ -C /usr/local --strip-components=1
'''
}
}
}
stage('Check Node.js version') {
steps {
script {
// Check Node.js in the Docker container
sh 'node -v' // Using Alpine package manager since Docker base is Alpine
}
}
}
stage('Install Dependencies') {
steps {
script {
// Install project dependencies including Mocha and mocha-junit-reporter
sh 'npm install'
}
}
}
stage('Run Mocha Tests') {
steps {
script {
// Run the Mocha tests and generate results in JUnit format for Jenkins
sh 'npx mocha --reporter xunit --reporter-options output=./test-results.xml'
}
}
}
stage('Publish Test Results') {
steps {
junit 'test-results/results.xml' // Publish the Mocha test results
}
}
stage('Build') {
steps {
script {
docker.build("madbakoyoyo/node-chat-app:${env.BUILD_NUMBER}")
}
}
}
stage('Push Docker Image') {
steps {
script {
withCredentials([string(credentialsId: 'dh_pass', variable: 'dockethub_pass')]) {
docker.withRegistry('https://index.docker.io/v1/', DOCKERHUB_CRED_ID) {
sh 'echo $dockethub_pass | docker login -u madbakoyoyo --password-stdin'
docker.image("madbakoyoyo/node-chat-app:${env.BUILD_NUMBER}").push()
}
}
}
}
}
stage('Deploy to EC2') {
steps {
script {
withCredentials([sshUserPrivateKey(credentialsId: '77fc5aed-0859-4073-9928-cb379b73c0ae', keyFileVariable: 'keyfile')]) {
sh """
ssh -o StrictHostKeyChecking=no -i ${keyfile} [email protected] <<EOF
# Stop and remove any existing container
docker stop node-chat-app || true
docker rm node-chat-app || true
# Pull the latest Docker image
docker pull madbakoyoyo/node-chat-app:${env.BUILD_NUMBER}
# Run the container
docker run -d -p 3000:3000 --name node-chat-app madbakoyoyo/node-chat-app:${env.BUILD_NUMBER}
EOF
"""
}
}
}
}
}
}