-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkins-pipeline.groovy
142 lines (136 loc) · 6.76 KB
/
jenkins-pipeline.groovy
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
pipeline {
tools {
maven 'Maven3'
}
agent any
environment {
userServicePort = "9001"
productServicePort = "9002"
orderServicePort = "9003"
userRegistryName = "marketplace/user-service-image"
productRegistryName = "marketplace/product-service-image"
orderRegistryName = "marketplace/order-service-image"
awsAccountId = "<AWS_ACCOUNT_ID>"
awsRegion = "<AWS_REGION>"
jwtIssuerUri = "<JWT_ISSUER_URI>"
cognitoUserPoolId = "<COGNITO_USER_POOL_ID>"
cognitoUserPoolClientId = "<COGNITO_USER_POOL_CLIENT_ID>"
cognitoUserPoolClientSecret = "<COGNITO_USER_POOL_CLIENT_SECRET>"
awsS3BucketName = "<AWS_S3_BUCKET_NAME>"
awsUserAccessKey = "<AWS_USER_ACCESS_KEY>"
awsUserSecretKey = "<AWS_USER_SECRET_KEY>"
gitHubRepoUrl = "<GITHUB_REPO_SSH_URL>"
gitHubBranch = "*/<GITHUB_BRANCH>"
}
triggers {
// Polling the Git repository every 1 minute
pollSCM('*/1 * * * *')
}
stages {
// Cloning the Git repository
stage('Cloning Git') {
steps {
checkout([$class: 'GitSCM', branches: [[name: "*/${gitHubBranch}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: "${gitHubRepoUrl}"]]])
}
}
// Building the application
stage ('Build') {
steps {
withEnv([
"ISSUER_URI=${jwtIssuerUri}",
"S3_BUCKET_NAME=${awsS3BucketName}",
"AWS_ACCESS_KEY=${awsUserAccessKey}",
"AWS_SECRET_KEY=${awsUserSecretKey}"
]) {
script {
withEnv([
"PORT=${userServicePort}",
"USER_POOL_ID=${cognitoUserPoolId}",
"USER_POOL_CLIENT_ID=${cognitoUserPoolClientId}",
"USER_POOL_CLIENT_SECRET=${cognitoUserPoolClientSecret}"
]) {
dir ('user-service') {
sh 'mv src/main/resources/application-prod.yml src/main/resources/application.yml'
sh 'mvn clean package'
}
}
withEnv([
"PORT=${productServicePort}",
"USER_SERVICE_HOST=localhost",
"USER_SERVICE_PORT=${userServicePort}"
]) {
dir ('product-service') {
sh 'mv src/main/resources/application-prod.yml src/main/resources/application.yml'
sh 'mvn clean package'
}
}
withEnv([
"PORT=${orderServicePort}",
"USER_SERVICE_HOST=localhost",
"USER_SERVICE_PORT=${userServicePort}",
"PRODUCT_SERVICE_HOST=localhost",
"PRODUCT_SERVICE_PORT=${productServicePort}"
]) {
dir ('order-service') {
sh 'mv src/main/resources/application-prod.yml src/main/resources/application.yml'
sh 'mvn clean package'
}
}
}
}
}
}
// Building Docker images
stage('Building image') {
steps{
script {
dir ('user-service') {
sh "mv src/main/resources/application.yml src/main/resources/application-prod.yml"
sh "docker build -t ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${userRegistryName}:latest ."
}
dir ('product-service') {
sh "mv src/main/resources/application.yml src/main/resources/application-prod.yml"
sh "docker build -t ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${productRegistryName}:latest ."
}
dir ('order-service') {
sh "mv src/main/resources/application.yml src/main/resources/application-prod.yml"
sh "docker build -t ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${orderRegistryName}:latest ."
}
sh 'docker images'
}
}
}
// Uploading Docker images into AWS ECR
stage('Pushing to ECR') {
steps{
script {
sh "aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 058264124182.dkr.ecr.us-east-1.amazonaws.com"
sh "docker push ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${userRegistryName}:latest"
sh "docker push ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${productRegistryName}:latest"
sh "docker push ${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${orderRegistryName}:latest"
}
}
}
// Deploying the application into AWS EKS
stage('K8S Deploy') {
steps{
script {
sh "sed -i 's|<USER_SERVICE_IMAGE>|${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${userRegistryName}:latest|g' eks-deploy.yml"
sh "sed -i 's|<PRODUCT_SERVICE_IMAGE>|${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${productRegistryName}:latest|g' eks-deploy.yml"
sh "sed -i 's|<ORDER_SERVICE_IMAGE>|${awsAccountId}.dkr.ecr.${awsRegion}.amazonaws.com/${orderRegistryName}:latest|g' eks-deploy.yml"
sh "sed -i 's|<JWT_ISSUER_URI>|${jwtIssuerUri}|g' eks-deploy.yml"
sh "sed -i 's|<COGNITO_USER_POOL_ID>|${cognitoUserPoolId}|g' eks-deploy.yml"
sh "sed -i 's|<COGNITO_USER_POOL_CLIENT_ID>|${cognitoUserPoolClientId}|g' eks-deploy.yml"
sh "sed -i 's|<COGNITO_USER_POOL_CLIENT_SECRET>|${cognitoUserPoolClientSecret}|g' eks-deploy.yml"
sh "sed -i 's|<AWS_S3_BUCKET_NAME>|${awsS3BucketName}|g' eks-deploy.yml"
sh "sed -i 's|<AWS_ACCESS_KEY>|${awsUserAccessKey}|g' eks-deploy.yml"
sh "sed -i 's|<AWS_SECRET_KEY>|${awsUserSecretKey}|g' eks-deploy.yml"
sh "cat eks-deploy.yml"
withKubeConfig([credentialsId: 'K8S']) {
sh ('kubectl apply -f eks-deploy.yml')
}
}
}
}
}
}