-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
73 lines (63 loc) · 1.86 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
pipeline {
agent any
triggers {
pollSCM('H/2 * * * *')
}
tools {
maven 'Maven_3'
}
environment {
DOCKERHUB_CREDS = 'docker_credentials'
DOCKER_IMAGE = 'mohammadrony/java-library-app'
REGISTRY = 'registry.hub.docker.com'
}
stages {
stage('Git Clone') {
steps {
echo 'Pulling repository'
git branch: 'main', url: 'https://github.com/mohammadrony/Java-CI-CD.git'
}
}
stage('Build Artifact') {
steps {
echo 'Constructing artifact'
sh 'mvn clean package'
}
}
stage('Build Container Image') {
steps {
script {
app_image = docker.build("${REGISTRY}/${DOCKER_IMAGE}")
}
}
}
stage('Publish Docker Image') {
steps {
script {
docker.withRegistry("https://${REGISTRY}", DOCKERHUB_CREDS) {
app_image.push("${BUILD_NUMBER}")
app_image.push('latest')
}
echo "Remove Image from Local"
sh "docker rmi ${REGISTRY}/${DOCKER_IMAGE}:latest"
sh "docker rmi ${REGISTRY}/${DOCKER_IMAGE}:${BUILD_NUMBER}"
}
}
}
stage('Deploy App in Kubernetes') {
steps {
withKubeConfig(credentialsId: 'kubeconfig', serverUrl: '') {
echo 'Creating config map and secrets'
sh '/usr/local/bin/kubectl apply -f 1-app-config-and-secret.yml'
echo 'Creating storage for mysql'
sh '/usr/local/bin/kubectl apply -f 2-mysql-pv-pvc.yml'
echo 'Creating mysql pod and service'
sh '/usr/local/bin/kubectl apply -f 3-mysql-deploy-service.yml'
echo 'Creating java app deployments'
sh 'sed -i "s/\\${BUILD_NUMBER}/${BUILD_NUMBER}/" 4-java-app-deploy.yml'
sh '/usr/local/bin/kubectl apply -f 4-java-app-deploy.yml'
}
}
}
}
}