-
Notifications
You must be signed in to change notification settings - Fork 1
/
Jenkinsfile
133 lines (133 loc) · 5.52 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
def changeddirs = []
def directories = []
def buildall = "false"
def deployments = [
[branch: 'oracle', dirName: 'manifests-oracle'],
[branch: 'azure', dirName: 'manifests-azure'],
]
pipeline {
agent {
docker {
image 'moodysan/gobaseimage:latest'
args '--user root -v /var/run/docker.sock:/var/run/docker.sock'
}
}
stages {
stage('Clean Workspace') {
when {
expression { currentBuild.number == 1 || "${buildall}"=="true"}
}
steps {
script {
sh "echo clean workspace"
sh "rm -rf ./*"
}
}
}
stage('Checkout Application Repo') {
when {
expression { currentBuild.number == 1 || "${buildall}"=="true"}
}
steps {
script {
dir("apps"){
sh "echo cloning application repository"
git branch: 'main', url: 'https://github.com/Moody-san/microservice-backend'
sh "echo adding all directories to built"
changeddirs = sh(script: "ls -1 -l | awk '/^d/ {print \$9}'",returnStdout: true).split('\n')
for(def dir in changeddirs){
if (!dir.contains('*tmp') && dir!=''){
directories.add(dir)
}
}
}
}
}
}
stage('Add Changed Directories to List'){
when {
expression { currentBuild.number != 1 }
}
steps {
script {
dir("apps"){
sh "echo update remote origin for application repo"
sh "git fetch origin main"
sh "echo adding directories that changed to list"
changeddirs = sh(script: "git diff --name-only main origin/main | grep '/' | cut -d/ -f1 | uniq",returnStdout: true).split('\n')
for(def dir in changeddirs){
if (!dir.contains('*tmp') && dir!=''){
directories.add(dir)
}
}
}
}
}
}
stage ('Build Docker Images'){
steps{
script{
dir("apps"){
if (directories.size()>0){
sh "echo update local app repo with changes from remote origin"
sh "git pull origin main:main"
sh "echo building dockerfile for directories ${directories}"
directories.each(){
dir("${it}") {
def image_name = "moodysan/${it}:${BUILD_NUMBER}"
sh "docker build -t ${image_name} ."
def dockerImage = docker.image("${image_name}")
docker.withRegistry('https://registry.hub.docker.com','docker-cred') {
dockerImage.push()
}
}
}
}
else{
sh "echo No changes in any directories"
}
}
}
}
}
stage('Checkout and Update Manifest Repo with newly create docker image') {
steps {
script {
deployments.each{ deployment ->
dir("${deployment.dirName}"){
sh "echo clone manifests repo"
git branch: '${deployment.branch}', url: 'https://github.com/Moody-san/k8s-manifests'
}
dir("${deployment.dirName}"){
sh "echo update deployment files in manifests repo"
withCredentials([usernamePassword(credentialsId: 'GITHUB_TOKEN', passwordVariable: 'PASSWORD', usernameVariable: 'USERNAME')]) {
directories.each(){
sh """
git config user.email "jenkins@gmail.com"
git config user.name "jenkins"
sed -i "s|moodysan/${it}.*|moodysan/${it}:${BUILD_NUMBER}|" manifests/${it}/deployment.yml
git add manifests/${it}/deployment.yml
git commit -m "Update ${it} deployment image to version ${BUILD_NUMBER}"
git push https://${PASSWORD}@github.com/${USERNAME}/k8s-manifests.git HEAD:main
"""
}
}
}
}
}
}
}
stage ('Remove tmp folders'){
steps{
script{
sh "echo remove tmp and manifest files generated recursively in workspace"
sh "rm -rf \$(find . -type d -name '*tmp*')"
}
}
}
}
options {
disableConcurrentBuilds()
skipDefaultCheckout()
}
}