-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
78 lines (66 loc) · 1.97 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
pipeline {
agent any
environment {
DOCKER_IMAGE_NAME = "excalidraw-clone"
DOCKER_CONTAINER_NAME = "excalidraw"
}
stages {
stage('checking the Source Control Manager') {
steps{
checkout scm
}
}
stage('setting the version commit hash for image...'){
steps {
script{
env.VERSION_COMMIT_HASH = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim() // versioning the build images
}
}
}
// build a docker image of the repository
stage('building docker image...'){
steps{
sh "docker build -t ${DOCKER_IMAGE_NAME}:${VERSION_COMMIT_HASH} ./frontend"
}
}
// remove the existing docker container if exists
stage('checking for existing docker container'){
steps{
script{
// search for existing docker container
def existingContainer = sh(script: "docker ps -a -q -f name=${DOCKER_CONTAINER_NAME}", returnStdout: true).trim()
if(existingContainer){
echo "removing existing docker container"
try{
sh "docker stop ${DOCKER_CONTAINER_NAME}"
}catch(Exception e){
echo "Failed to stop existing container: ${e.getMessage()}"
}
sh "docker rm ${DOCKER_CONTAINER_NAME}"
}else{
echo "No existing docker container found"
}
}
}
}
// spin up the docker container
stage('running the docker container'){
steps{
sh "docker run -d --restart unless-stopped --name ${DOCKER_CONTAINER_NAME} -p 3000:3000 ${DOCKER_IMAGE_NAME}:${VERSION_COMMIT_HASH}"
}
}
}
post {
always {
sh """
echo "Docker container status: \$(docker inspect --format='{{.State.Status}}' ${DOCKER_CONTAINER_NAME})"
"""
}
success {
echo "pipeline completed successfully!"
}
failure {
echo "pipeline failed."
}
}
}