forked from akshu20791/apachewebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile
More file actions
58 lines (52 loc) · 1.73 KB
/
Jenkinsfile
File metadata and controls
58 lines (52 loc) · 1.73 KB
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
pipeline {
agent any
environment {
IMAGE_NAME = 'prabhatsanu1990/prabhatnewimg9june'
TAG = 'v1'
FULL_IMAGE = "${IMAGE_NAME}:${TAG}"
CONTAINER_NAME = 'My-apache'
REMOTE_USER = 'ubuntu'
REMOTE_HOST = '172.31.6.94'
}
stages {
stage('Clone Repo') {
steps {
git url: 'https://github.com/Prabhatsanu/apachewebsite/', branch: 'master'
}
}
stage('Build Docker Image') {
steps {
script {
sh "docker build -t ${FULL_IMAGE} ."
sh "docker images"
}
}
}
stage('Docker Login & Push') {
steps {
withCredentials([
usernamePassword(
credentialsId: 'dockerhub-pwd',
passwordVariable: 'PASS',
usernameVariable: 'USER'
)
]) {
sh "echo $PASS | docker login -u $USER --password-stdin"
sh "docker push ${FULL_IMAGE}"
}
}
}
stage('Remote Deploy') {
steps {
script {
def removeCmd = "sudo docker rm -f ${CONTAINER_NAME} || true"
def runCmd = "sudo docker run -itd --name ${CONTAINER_NAME} -p 8081:80 ${FULL_IMAGE}"
sshagent(['sshkeypair']) {
sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} '${removeCmd}'"
sh "ssh -o StrictHostKeyChecking=no ${REMOTE_USER}@${REMOTE_HOST} '${runCmd}'"
}
}
}
}
}
}