-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkins-docker.txt
85 lines (78 loc) · 2.48 KB
/
jenkins-docker.txt
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
pipeline {
agent any
environment {
DOCKER_CREDENTIALS_ID = 'dockerhub' // Jenkins ID for DockerHub credentials
DOCKER_IMAGE = 'basharulsiddike/abctechnologies'
DOCKER_REGISTRY = 'https://index.docker.io/v1/' // For DockerHub
CONTAINER_NAME = "abctechnologies"
DOCKER_TAG = "${BUILD_ID}" // Use the BUILD_ID as the tag
CONTAINER_PORT = "9191" // Use the BUILD_ID as the tag
}
stages {
stage('Code Checkout') {
steps {
git branch: 'master', url: 'https://github.com/basharul-siddike/lab-project1.git'
}
}
stage('Build') {
steps {
sh 'mvn clean compile'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
}
stage('Package') {
steps {
sh 'mvn package'
}
}
stage('Build Docker Image') {
steps {
script {
docker.build("${DOCKER_IMAGE}:${DOCKER_TAG}")
}
}
}
stage('Push to DockerHub') {
steps {
script {
docker.withRegistry("${DOCKER_REGISTRY}", "${DOCKER_CREDENTIALS_ID}") {
docker.image("${DOCKER_IMAGE}:${DOCKER_TAG}").push()
}
}
}
}
stage('Stopping and removing existing container...') {
steps {
script {
def containerExists = sh(
script: "docker inspect -f '{{.State.Running}}' ${CONTAINER_NAME}",
returnStatus: true
)
if (containerExists == 0) {
echo "Container ${CONTAINER_NAME} exists. Stopping and removing..."
sh "docker stop ${CONTAINER_NAME} || true"
sh "docker rm ${CONTAINER_NAME}"
} else {
echo "Container ${CONTAINER_NAME} does not exist. No need to stop or remove."
}
}
}
}
stage('Deploy as container')
{
steps
{
sh 'docker run -d -p ${CONTAINER_PORT}:8080 --name $CONTAINER_NAME ${DOCKER_IMAGE}:${DOCKER_TAG} || { echo "Failed to start Docker container! Exiting."; exit 1; }'
}
}
}
post {
always {
cleanWs()
}
}
}