This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
204 lines (181 loc) · 7.5 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
191
192
193
194
195
196
197
198
199
200
201
202
//returns the deployment env name according to branch name
//otherwise it raises an exception if the branch name is not recognized
def getDeployEnv(git_branch) {
if(!git_branch){
throw new Error("branch ${git_branch} is not valid.")
}
String bname = parseBranchName(git_branch)
switch(bname) {
case ~/dev/ : return "DEV";
case ~/qa/: return "QA";
case ~/uat/: return "UAT";
default: throw new Exception ("branch ${git_branch} not recognized.");
}
}
//returns the type of the environment according to the branch name e.g sandbox or production
def getEnvType (git_branch) {
if(!git_branch){
throw new Error("branch ${git_branch} is not valid.")
}
String bname = parseBranchName(git_branch)
switch(bname) {
case ~/(dev)|(qa)|(uat)/: return 'sandbox';
default: throw new Exception ("branch ${git_branch} not recognized.");
}
}
//returns environment name mapped to the git branch
def getMappedEnv (git_branch) {
if(!git_branch){
throw new Error("branch ${git_branch} is not valid.")
}
String bname = parseBranchName(git_branch)
String name;
switch(bname) {
case ~/dev/ : name = "dev"; break;
case ~/qa/: name = "qa"; break;
case ~/uat/: name = "uat"; break;
default: throw new Exception ("branch ${git_branch} not recognized.");
}
return name
}
//removes the origin/ from the branch name
def parseBranchName (git_branch) {
if(!git_branch){
throw new Error("branch ${git_branch} is not valid.")
}
def (_,name) = (git_branch =~ /^origin\/(.+)$/)[0]
return name
}
//parses the git url to extract repo name
def parseRepoName (git_url) {
if(!git_url){
throw new Error("git url ${git_url} is not valid.")
}
def (_,head,name) = (git_url =~ /^(git@|https:\/\/).*\/(.*)(\.git)?$/)[0]
return name
}
//returns the appropriate number of workers depending on the environment
def getNbWorkers (env_name) {
def workers;
if(env_name == "PROD"){
workers = "2"
} else {
workers = "1"
}
return workers
}
/*
PIPELINE
*** CREDENTIALS REQUIREMENTS ***
following is a list of nomenclature for credentials used in this pipeline.
** anypoint platform environment credentials. should be provided for each environment as "Secret text".
The name is generated from the git branch:
- anypoint.{env}.client_id
- anypoint.{env}.secret_id
** anypoint connected app credentials. should be provided for each environment as "Secret text".
All environments below production use a single user and production has its own user.
The cred key is generated from the git branch. see the "getEnvType" for the implementation of env descrimination:
- anypoint.app.{sandbox/production}.client_id
- anypoint.app.{sandbox/production}.client_secret
** mule vault key credential. should be provided for each project as "Secret text".
the project name is extracted from the git url. See "parseRepoName" for project name extraction:
- anypoint.vault.{project_name}.{env}.key
********************************************************
*/
pipeline {
agent any
tools {
maven 'Maven 3.6.3'
jdk 'jdk8'
}
environment {
PROJECT_NAME = parseRepoName(GIT_URL)
ENV_TYPE = getEnvType(GIT_BRANCH)
ENV = getMappedEnv(GIT_BRANCH)
ANYPOINT_ENV = getDeployEnv(GIT_BRANCH)
ANYPOINT_REGION = "{{REGION}}"
ANYPOINT_BUSINESS_GROUP = "{{GROUP_NAME}}"
ANYPOINT_WORKER_TYPE = "MICRO"
ANYPOINT_WORKERS = getNbWorkers("$ANYPOINT_ENV")
ANYPOINT_HOST = "https://{{ANYPOINT_HOST}}"
ANYPOINT_ANALYTICS_HOST = "https://analytics-ingest.{{ANYPOINT_HOST}}"
ANYPOINT_VAULT_CRED_KEY = "anypoint.vault.${PROJECT_NAME}.${ENV}.key"
ANYPOINT_ENV_CLIENT_ID_KEY = "anypoint.${ENV}.client_id"
ANYPOINT_ENV_CLIENT_SECRET_KEY = "anypoint.${ENV}.client_secret"
ANYPOINT_APP_CLIENT_ID_KEY = "anypoint.app.${ENV_TYPE}.client_id"
ANYPOINT_APP_CLIENT_SECRET_KEY = "anypoint.app.${ENV_TYPE}.client_secret"
MVN_SETTING_FILE_ID = "{{MVN_GLBL_SETT_ID}}"
}
stages {
stage ('Initialization') {
steps {
echo "PROJECT_NAME = $PROJECT_NAME"
echo "ENV_TYPE = $ENV_TYPE"
echo "ENV = $ENV"
echo "ANYPOINT_ENV = $ANYPOINT_ENV"
echo "ANYPOINT_ENV_CLIENT_ID_KEY = $ANYPOINT_ENV_CLIENT_ID_KEY"
echo "ANYPOINT_ENV_CLIENT_SECRET_KEY = $ANYPOINT_ENV_CLIENT_SECRET_KEY"
echo "ANYPOINT_APP_CLIENT_ID_KEY = $ANYPOINT_APP_CLIENT_ID_KEY"
echo "ANYPOINT_APP_CLIENT_SECRET_KEY = $ANYPOINT_APP_CLIENT_SECRET_KEY"
}
}
stage('MULE TEST') {
environment{
ANYPOINT_VAULT_KEY = credentials("${ANYPOINT_VAULT_CRED_KEY}")
ANYPOINT_APP_CLIENT_ID = credentials("${ANYPOINT_APP_CLIENT_ID_KEY}")
ANYPOINT_APP_CLIENT_SECRET = credentials("${ANYPOINT_APP_CLIENT_SECRET_KEY}")
ACCESS_TOKEN = sh (script: "curl -s 'https://{{ANYPOINT_HOST}}/accounts/api/v2/oauth2/token' \
-X POST -H 'Content-Type: application/json' \
-d '{\"grant_type\": \"client_credentials\", \"client_id\": \"${ANYPOINT_APP_CLIENT_ID}\", \"client_secret\": \"${ANYPOINT_APP_CLIENT_SECRET}\"}' \
| sed -n 's|.*\"access_token\":\"\\([^\"]*\\)\".*|\\1|p'", returnStdout: true).trim()
}
steps {
echo 'Testing ...'
configFileProvider([configFile(fileId: "${MVN_SETTING_FILE_ID}", variable: 'MAVEN_SETTINGS_XML')]) {
sh '''
mvn -s $MAVEN_SETTINGS_XML clean test \
-Denv=${ENV} \
-Dmule.env=${ENV} \
-Danypoint.base_uri=${ANYPOINT_HOST} \
-DauthToken=${ACCESS_TOKEN} \
-Dmule.vault.key=${ANYPOINT_VAULT_KEY} \
'''
}
}
}
stage ('MULE DEPLOY') {
environment {
ANYPOINT_VAULT_KEY = credentials("${ANYPOINT_VAULT_CRED_KEY}")
ANYPOINT_ENV_CLIENT_ID = credentials("${ANYPOINT_ENV_CLIENT_ID_KEY}")
ANYPOINT_ENV_CLIENT_SECRET = credentials("${ANYPOINT_ENV_CLIENT_SECRET_KEY}")
ANYPOINT_APP_CLIENT_ID = credentials("${ANYPOINT_APP_CLIENT_ID_KEY}")
ANYPOINT_APP_CLIENT_SECRET = credentials("${ANYPOINT_APP_CLIENT_SECRET_KEY}")
ACCESS_TOKEN = sh (script: "curl -s 'https://{{ANYPOINT_HOST}}/accounts/api/v2/oauth2/token' \
-X POST -H 'Content-Type: application/json' \
-d '{\"grant_type\": \"client_credentials\", \"client_id\": \"${ANYPOINT_APP_CLIENT_ID}\", \"client_secret\": \"${ANYPOINT_APP_CLIENT_SECRET}\"}' \
| sed -n 's|.*\"access_token\":\"\\([^\"]*\\)\".*|\\1|p'", returnStdout: true).trim()
}
steps {
echo 'Deploying ...'
configFileProvider([configFile(fileId: "${MVN_SETTING_FILE_ID}", variable: 'MAVEN_SETTINGS_XML')]) {
sh '''
mvn -s $MAVEN_SETTINGS_XML deploy -DmuleDeploy \
-Denv=${ENV} \
-Dmule.env=${ENV} \
-Dmule.vault.key=${ANYPOINT_VAULT_KEY} \
-Danypoint.base_uri=${ANYPOINT_HOST} \
-Danypoint.analytics_base_uri=${ANYPOINT_ANALYTICS_HOST} \
-Danypoint.environment=${ANYPOINT_ENV} \
-Danypoint.businessgroup=${ANYPOINT_BUSINESS_GROUP} \
-Danypoint.workers=${ANYPOINT_WORKERS} \
-Danypoint.workertype=${ANYPOINT_WORKER_TYPE} \
-Danypoint.region=${ANYPOINT_REGION} \
-DauthToken=${ACCESS_TOKEN} \
-Dplatform.client_id=${ANYPOINT_ENV_CLIENT_ID} \
-Dplatform.client_secret=${ANYPOINT_ENV_CLIENT_SECRET} \
'''
}
}
}
}
}