-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
46 lines (46 loc) · 2.02 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
pipeline {
agent { label 'master' }
parameters {
string(name: 'Name', defaultValue: 'jenkins', description: 'The name of the container that will run Jenkins')
string(name: 'Port', defaultValue: '49000', description: 'The localhost port that will be attached to the container')
booleanParam(name: 'Stop_Existing', defaultValue: true, description: 'Stop any containers that are running with the same name')
booleanParam(name: 'Pull_Latest', defaultValue: true, description: 'Pull the lastest version of the Jenkins container image')
}
stages {
stage('Stop Existing') {
when {
expression { params.Stop_Existing == true }
}
steps {
echo "Stopping exisiting container with name ${params.Name} ..."
sh "/usr/local/bin/docker stop ${params.Name} || echo 'Nothing to stop'"
sh "/usr/local/bin/docker rm ${params.Name} || echo 'Nothing to remove'"
}
}
stage('Pull Latest') {
when {
expression { params.Pull_Latest == true }
}
steps {
echo "Pulling lastet Jenkins container image"
sh "/usr/local/bin/docker pull jenkins/jenkins:lts"
}
}
stage('Run') {
steps {
echo "Running jenkins container ..."
sh "/usr/local/bin/docker run --detach --publish ${params.Port}:8080 --name ${params.Name} jenkins"
}
}
stage('Finalize') {
steps {
echo "Waiting for jenkins process to start ..."
sleep 15
script {
def initialAdminPassword = sh(script: "/usr/local/bin/docker exec ${params.Name} cat /var/jenkins_home/secrets/initialAdminPassword", returnStdout: true).trim()
echo "Password is ${initialAdminPassword}\nBrowse to http://localhost:${params.Port}"
}
}
}
}
}