forked from iam-veeramalla/MERN-docker-compose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjenkinsfile
72 lines (71 loc) · 2.63 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
pipeline {
agent {
docker {
image 'docker:stable-dind' // Using Docker-in-Docker for building
args '--privileged -v /var/run/docker.sock:/var/run/docker.sock' // Required for Docker-in-Docker
}
}
environment {
DOCKERHUB_CREDENTIALS_ID = '7828bad6-9e01-4862-a785-6c3ff1aa50ed' // Replace with your actual Jenkins credentials ID
DOCKER_IMAGE_NAME = 'proxxpa/mern-app' // Docker Hub repository name
DOCKER_IMAGE_TAG = 'latest'
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Install Docker Compose') {
steps {
script {
// Install Docker Compose
sh '''
apk add --no-cache curl
curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
'''
}
}
}
stage('Build Docker Image') {
steps {
script {
// Building the Docker Compose project
sh 'docker-compose -f docker-compose.yaml build '
}
}
}
stage('Push Docker Image') {
steps {
script {
// Logging in to Docker Hub
withCredentials([usernamePassword(credentialsId: "${DOCKERHUB_CREDENTIALS_ID}", passwordVariable: 'DOCKERHUB_PASSWORD', usernameVariable: 'DOCKERHUB_USERNAME')]) {
sh 'echo "$DOCKERHUB_PASSWORD" | docker login -u "$DOCKERHUB_USERNAME" --password-stdin'
// Tagging and pushing images to Docker Hub
sh "docker tag ${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG} ${DOCKERHUB_USERNAME}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}"
sh "docker push ${DOCKERHUB_USERNAME}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}"
}
}
}
}
stage('Deploy') {
steps {
script {
// Starting the services defined in the Docker Compose file
sh 'docker-compose -f docker-compose.yml up -d'
}
}
}
}
post {
always {
script {
// Clean up any dangling images
sh 'docker system prune -f'
// Log out of Docker Hub
sh 'docker logout'
}
}
}
}