-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
190 lines (170 loc) · 8.13 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
pipeline {
agent any
environment {
AWS_REGION = "eu-west-3"
AWS_ACCESS_KEY_ID = credentials('ACCESS_KEY_AWS')
AWS_SECRET_ACCESS_KEY = credentials('SECRET_KEY_AWS')
ECR_REGISTRY = "565393062704.dkr.ecr.eu-west-3.amazonaws.com"
}
stages {
stage("Git Checkout") {
steps {
git(
url: "https://github.com/Sri-Harsha-S/spring-petclinic-cloud",
branch: "master",
credentialsId: 'GITHUB_TOKEN'
)
}
}
stage("Setup Env Variable") {
steps {
script {
env.REPOSITORY_PREFIX = "${ECR_REGISTRY}/petclinic"
echo "Repository Prefix set to: ${env.REPOSITORY_PREFIX}"
}
}
}
stage("Login to ECR") {
steps {
script {
sh '''
aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REGISTRY
'''
}
}
}
stage("Build and Tag Images") {
steps {
withMaven() {
sh '''
docker --version
# Build images
mvn -X spring-boot:build-image -Pk8s -DREPOSITORY_PREFIX=$ECR_REGISTRY/petclinic
# Tag the images with simpler names
docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-api-gateway:latest $REPOSITORY_PREFIX/api-gateway:latest
docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-api-gateway:latest $REPOSITORY_PREFIX/api-gateway:1.0.$BUILD_NUMBER
docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-customers-service:latest $REPOSITORY_PREFIX/customers-service:latest
docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-customers-service:latest $REPOSITORY_PREFIX/customers-service:1.0.$BUILD_NUMBER
docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-vets-service:latest $REPOSITORY_PREFIX/vets-service:latest
docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-vets-service:latest $REPOSITORY_PREFIX/vets-service:1.0.$BUILD_NUMBER
docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-visits-service:latest $REPOSITORY_PREFIX/visits-service:latest
docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-visits-service:latest $REPOSITORY_PREFIX/visits-service:1.0.$BUILD_NUMBER
'''
}
}
}
// New stage to push images
stage("Push Images to ECR") {
steps {
script {
sh '''
# Push the images to ECR
docker push $REPOSITORY_PREFIX/api-gateway:latest
docker push $REPOSITORY_PREFIX/api-gateway:1.0.$BUILD_NUMBER
docker push $REPOSITORY_PREFIX/customers-service:latest
docker push $REPOSITORY_PREFIX/customers-service:1.0.$BUILD_NUMBER
docker push $REPOSITORY_PREFIX/vets-service:latest
docker push $REPOSITORY_PREFIX/vets-service:1.0.$BUILD_NUMBER
docker push $REPOSITORY_PREFIX/visits-service:latest
docker push $REPOSITORY_PREFIX/visits-service:1.0.$BUILD_NUMBER
'''
}
}
}
stage('Trigger ManifestUpdate') {
steps {
script {
echo "Triggering the Update Manifest Job"
build job: 'helm-chart-CI', parameters: [string(name: 'IMAGEVERSION', value: "1.0.${env.BUILD_NUMBER}")]
}
}
}
}
}
// pipeline {
// agent any
// stages {
// stage('Checkout') {
// steps {
// script {
// // Define your Git repository URL
// def gitRepo = 'https://github.com/Sri-Harsha-S/spring-petclinic-cloud.git'
// // Define your credentials ID (the ID of the Git token stored in Jenkins)
// def credentialsId = 'GITHUB_TOKEN'
// // Checkout the repository
// try {
// checkout([$class: 'GitSCM',
// branches: [[name: '*/master']], // Change this to your default branch if different
// userRemoteConfigs: [[
// url: gitRepo,
// credentialsId: credentialsId
// ]]
// ])
// // Print success message
// echo 'Successfully checked out'
// } catch (Exception e) {
// echo "Checkout failed: ${e.getMessage()}"
// currentBuild.result = 'FAILURE'
// }
// }
// }
// }
// }
// }
// pipeline {
// agent any
// environment {
// AWS_REGION = "eu-west-3"
// AWS_ACCESS_KEY_ID = credentials('ACCESS_KEY_AWS')
// AWS_SECRET_ACCESS_KEY = credentials('SECRET_KEY_AWS')
// ECR_REGISTRY = "565393062704.dkr.ecr.eu-west-3.amazonaws.com"
// }
// stages {
// stage("Git Checkout") {
// steps {
// git(
// url: "https://github.com/Sri-Harsha-S/spring-petclinic-cloud",
// branch: "master",
// credentialsId: 'GITHUB_TOKEN' // Removed the $ sign for credentialsId
// )
// }
// }
// stage("Setup Env Variable") {
// steps {
// script {
// env.REPOSITORY_PREFIX = "${ECR_REGISTRY}/petclinic"
// echo "Repository Prefix set to: ${env.REPOSITORY_PREFIX}"
// }
// }
// }
// stage("Login to ECR") {
// steps {
// script {
// sh '''
// aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ECR_REGISTRY
// '''
// }
// }
// }
// stage("Build and Tag Images") {
// steps {
// withMaven() {
// sh '''
// docker --version
// # Build images
// mvn -X spring-boot:build-image -Pk8s -DREPOSITORY_PREFIX=$ECR_REGISTRY/petclinic
// # Tag the images with simpler names
// docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-api-gateway:latest $REPOSITORY_PREFIX/api-gateway:latest
// docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-api-gateway:latest $REPOSITORY_PREFIX/api-gateway:1.0.$BUILD_NUMBER
// docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-customers-service:latest $REPOSITORY_PREFIX/customers-service:latest
// docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-customers-service:latest $REPOSITORY_PREFIX/customers-service:1.0.$BUILD_NUMBER
// docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-vets-service:latest $REPOSITORY_PREFIX/vets-service:latest
// docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-vets-service:latest $REPOSITORY_PREFIX/vets-service:1.0.$BUILD_NUMBER
// docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-visits-service:latest $REPOSITORY_PREFIX/visits-service:latest
// docker tag $ECR_REGISTRY/petclinic/spring-petclinic-cloud-visits-service:latest $REPOSITORY_PREFIX/visits-service:1.0.$BUILD_NUMBER
// '''
// }
// }
// }
// }
// }