-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPipeline-script.txt
More file actions
75 lines (63 loc) · 2.73 KB
/
Copy pathPipeline-script.txt
File metadata and controls
75 lines (63 loc) · 2.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def img
pipeline {
environment {
registry = "kss7/python-jenkins" //To push an image to Docker Hub, you must first name your local image using your Docker Hub username and the repository name that you created through Docker Hub on the web.
registryCredential = 'docker-hub-login'
dockerImage = ''
}
agent any
stages {
stage('checkout') {
steps {
git 'https://github.com/kss7/SimpleFlaskUI.git'
}
}
stage ('Stop previous running container'){
steps{
sh returnStatus: true, script: 'docker stop $(docker ps -a | grep ${JOB_NAME} | awk \'{print $1}\')'
sh returnStatus: true, script: 'docker rmi $(docker images | grep ${registry} | awk \'{print $3}\') --force' //this will delete all images
sh returnStatus: true, script: 'docker rm ${JOB_NAME}'
}
}
stage('Build Image') {
steps {
script {
img = registry + ":${env.BUILD_ID}"
println ("${img}")
dockerImage = docker.build("${img}")
}
}
}
stage('Test - Run Docker Container on Jenkins node') {
steps {
sh label: '', script: "docker run -d --name ${JOB_NAME} -p 5000:5000 ${img}"
}
}
stage('Push To DockerHub') {
steps {
script {
docker.withRegistry( 'https://registry.hub.docker.com ', registryCredential ) {
dockerImage.push()
}
}
}
}
stage('Deploy to Test Server') {
steps {
script {
def stopcontainer = "docker stop ${JOB_NAME}"
def delcontName = "docker rm ${JOB_NAME}"
def delimages = 'docker image prune -a --force'
def drun = "docker run -d --name ${JOB_NAME} -p 5000:5000 ${img}"
println "${drun}"
sshagent(['docker-test']) {
sh returnStatus: true, script: "ssh -o StrictHostKeyChecking=no docker@192.168.1.16 ${stopcontainer} "
sh returnStatus: true, script: "ssh -o StrictHostKeyChecking=no docker@192.168.1.16 ${delcontName}"
sh returnStatus: true, script: "ssh -o StrictHostKeyChecking=no docker@192.168.1.16 ${delimages}"
// some block
sh "ssh -o StrictHostKeyChecking=no docker@192.168.1.16 ${drun}"
}
}
}
}
}