-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
73 lines (59 loc) · 1.92 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
def CONTAINER_NAME="jenkins-pipeline"
def CONTAINER_TAG="latest"
def DOCKER_HUB_USER="hakdogan"
def HTTP_PORT="8090"
node {
stage('Initialize'){
def dockerHome = tool 'myDocker'
def mavenHome = tool 'myMaven'
env.PATH = "${dockerHome}/bin:${mavenHome}/bin:${env.PATH}"
}
stage('Checkout') {
checkout scm
}
stage('Build'){
sh "mvn clean install"
}
stage('Sonar'){
try {
sh "mvn sonar:sonar"
} catch(error){
echo "The sonar server could not be reached ${error}"
}
}
stage("Image Prune"){
imagePrune(CONTAINER_NAME)
}
stage('Image Build'){
imageBuild(CONTAINER_NAME, CONTAINER_TAG)
}
stage('Push to Docker Registry'){
withCredentials([usernamePassword(credentialsId: 'dockerHubAccount', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
pushToImage(CONTAINER_NAME, CONTAINER_TAG, USERNAME, PASSWORD)
}
}
stage('Run App'){
runApp(CONTAINER_NAME, CONTAINER_TAG, DOCKER_HUB_USER, HTTP_PORT)
}
}
def imagePrune(containerName){
try {
sh "docker image prune -f"
sh "docker stop $containerName"
} catch(error){}
}
def imageBuild(containerName, tag){
sh "docker build -t $containerName:$tag -t $containerName --pull --no-cache ."
echo "Image build complete"
}
def pushToImage(containerName, tag, dockerUser, dockerPassword){
sh "docker login -u $dockerUser -p $dockerPassword"
sh "docker tag $containerName:$tag $dockerUser/$containerName:$tag"
sh "docker push $dockerUser/$containerName:$tag"
echo "Image push complete"
}
def runApp(containerName, tag, dockerHubUser, httpPort){
sh "docker pull $dockerHubUser/$containerName"
sh "docker run -d --rm -p $httpPort:$httpPort --name $containerName $dockerHubUser/$containerName:$tag"
echo "Application started on port: ${httpPort} (http)"
}