forked from ravi2krishna/lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
81 lines (72 loc) · 2.56 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
pipeline {
agent any
environment{
DOCKER_IMAGE = 'claxmih/lms'
DOCKER_REGISTRY = 'docker.io'
DOCKER_CREDENTIALS_ID = '28716a9a-1996-49aa-9ecb-6aa380f09462'
}
stages {
stage('Clean Up Workspace') {
steps {
echo 'Cleaning Work Space'
}
}
stage('Cloning project') {
steps {
echo 'Cloning project..'
script {
git url: 'https://github.com/LaxmiHA/lms.git',
branch: 'main'
}
echo 'cloning completed'
}
}
stage('Copy version from package.json') {
steps {
echo 'Copying version..'
script{
def json = readJSON file: 'webapp/package.json'
def version = json.version
env.VERSION = version // Setting it as an environment variable
env.IMAGE_NAME = "${DOCKER_IMAGE}:${version}"
echo "Version: ${env.VERSION}"
}
}
}
stage('Run Docker Script') {
steps {
script {
// Ensure the script is executable
dir('./webapp'){
sh 'chmod +x docker_script.sh'
// Verify the script is executable
// sh 'ls -la docker_script.sh'
// Run the docker_script.sh to build the Docker image
sh './docker_script.sh'
}
}
}
}
stage('Push to Docker Registry') {
steps {
echo 'Pushing'
script {
withCredentials([usernamePassword(credentialsId: "${DOCKER_CREDENTIALS_ID}", usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
sh 'echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin'
}
sh "docker tag ${DOCKER_IMAGE} ${DOCKER_REGISTRY}/${env.IMAGE_NAME}"
sh "docker push ${DOCKER_REGISTRY}/${env.IMAGE_NAME}"
}
echo 'Pushed finally'
}
}
stage('Deploy Image') {
steps {
script {
sh "docker run -d -p 3000:3000 ${DOCKER_REGISTRY}/${env.IMAGE_NAME}"
}
echo "Image is now deployed"
}
}
}
}