forked from shobhans/cicd_gitops_k8s
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJenkinsfile
108 lines (101 loc) · 3.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
pipeline {
agent {
kubernetes {
yamlFile 'builder.yaml'
}
}
environment {
DOCKERHUB_USERNAME = "shobhan"
APP_NAME = "docker-spring"
IMAGE_TAG = "${BUILD_NUMBER}"
IMAGE_NAME = "${DOCKERHUB_USERNAME}" + "/" + "${APP_NAME}"
}
stages {
stage('Build Maven project & Archive Artifact') {
steps {
container('maven') {
script {
sh '''
mvn clean install -Dmaven.test.skip
'''
}
}
archiveArtifacts '**/target/*.jar'
}
}
stage('Kaniko Image Build & Push Image') {
when {
branch 'main'
}
steps {
container('kaniko') {
script {
sh '''
/kaniko/executor --dockerfile `pwd`/Dockerfile \
--context `pwd` \
--destination=${IMAGE_NAME}:${BUILD_NUMBER} \
--destination=${IMAGE_NAME}:latest
'''
}
}
}
}
stage('Checkout K8S manifest SCM'){
when {
branch 'main'
}
steps {
git credentialsId: 'gh_shobhan_key',
url: 'https://github.com/shobhans/argocd_app_k8s_manifests.git',
branch: 'main'
}
}
stage('Staging Deploy - Update K8S manifest & push to Repo'){
when {
branch 'main'
}
steps {
milestone(1)
script{
withCredentials([usernamePassword(credentialsId: 'gh_shobhan_key', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
sh '''
cd staging
cat deployment.yaml
sed -i "s/${APP_NAME}.*/${APP_NAME}:${BUILD_NUMBER}/g" deployment.yaml
cat deployment.yaml
git config --global user.name "shobhan"
git config --global user.email "[email protected]"
git add deployment.yaml
git commit -m 'Updated the Staging App deployment | Jenkins Pipeline'
git push https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${GIT_USERNAME}/argocd_app_k8s_manifests.git HEAD:main
'''
}
}
}
}
stage('Prod Deploy - Update K8S manifest & push to Repo'){
when {
branch 'main'
}
steps {
input 'Deploy to Production ?'
milestone(2)
script{
withCredentials([usernamePassword(credentialsId: 'gh_shobhan_key', passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
sh '''
cd prod
cat deployment.yaml
sed -i "s/${APP_NAME}.*/${APP_NAME}:${BUILD_NUMBER}/g" deployment.yaml
cat deployment.yaml
git config --global user.name "shobhan"
git config --global user.email "[email protected]"
git add deployment.yaml
git commit -m 'Updated the Prod App deployment | Jenkins Pipeline'
git push https://${GIT_USERNAME}:${GIT_PASSWORD}@github.com/${GIT_USERNAME}/argocd_app_k8s_manifests.git HEAD:main
'''
}
}
}
}
}
}