@docupace/pipeline 1.0.2-4.1 โ 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/next-changeset-validate.sh +8 -8
- package/next/changeset.sh +75 -75
- package/next/copy.sh +34 -34
- package/next/deploy.sh +40 -40
- package/next/execute.sh +117 -117
- package/next/sonar.sh +23 -23
- package/package.json +4 -20
- package/src/changeset/cli.js +235 -281
- package/src/changeset/validate.js +44 -47
- package/src/next/cli.js +59 -185
- package/src/next/copy.js +27 -52
- package/src/next/deploy.js +118 -517
- package/src/next/publish.js +46 -48
- package/src/next/pull.js +7 -7
- package/src/next/sonar.js +23 -23
- package/.prettierrc.cjs +0 -12
- package/eslint.config.js +0 -26
- package/src/auth0/alias.js +0 -75
- package/src/auth0/cli.js +0 -41
- package/src/auth0/configure.js +0 -715
- package/src/auth0/vault.js +0 -107
- package/src/jenkins/setParams.groovy +0 -59
- package/src/next/alias.js +0 -53
- package/src/next/apache.js +0 -40
- package/src/next/config.js +0 -84
- package/src/next/connect.js +0 -189
- package/src/next/haproxy.js +0 -50
- package/src/next/jenkins.js +0 -123
- package/src/next/pipline.js +0 -114
- package/src/next/vault.js +0 -43
- package/terraform/apply.sh +0 -26
- package/terraform/init.sh +0 -48
- package/terraform/plan.sh +0 -54
- package/terraform/validate.sh +0 -17
package/src/next/deploy.js
CHANGED
|
@@ -1,517 +1,118 @@
|
|
|
1
|
-
import fs from 'fs';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import yaml from 'js-yaml';
|
|
4
|
-
import { $ } from 'zx';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
*
|
|
13
|
-
* @param {string}
|
|
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
|
-
console.log(`Repository cloned to: ${repoPath}`);
|
|
120
|
-
return repoPath;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
function getConfigPath(application, env, site, name) {
|
|
124
|
-
const { repoPath } = getRepoLocalPath(env);
|
|
125
|
-
const deployConfigFilePath = path.join(
|
|
126
|
-
repoPath,
|
|
127
|
-
`dp/${site}/${application}.${name}.yaml`,
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
return { repoPath, deployConfigFilePath };
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function getAppTypesFromRepo(env, site) {
|
|
134
|
-
const { repoPath } = getRepoLocalPath(env);
|
|
135
|
-
const deployConfigFilePath = path.join(repoPath, `dp/${site}`);
|
|
136
|
-
const applications = [];
|
|
137
|
-
if (fs.existsSync(deployConfigFilePath)) {
|
|
138
|
-
const files = fs.readdirSync(deployConfigFilePath);
|
|
139
|
-
files.forEach((file) => {
|
|
140
|
-
if (file.includes('.values.yaml')) {
|
|
141
|
-
const appName = file.split('.')[0];
|
|
142
|
-
applications.push(appName);
|
|
143
|
-
}
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return { applications };
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export async function readDeployConfig(application, env, site, name) {
|
|
151
|
-
const { deployConfigFilePath } = getConfigPath(application, env, site, name);
|
|
152
|
-
|
|
153
|
-
let deployConfig;
|
|
154
|
-
try {
|
|
155
|
-
deployConfig = yaml.load(fs.readFileSync(deployConfigFilePath, 'utf8'));
|
|
156
|
-
console.log(`YAML file loaded from: ${deployConfigFilePath}`);
|
|
157
|
-
} catch (error) {
|
|
158
|
-
console.error(`Error reading YAML file: ${error.message}`);
|
|
159
|
-
process.exit(1);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
return { deployConfig, deployConfigFilePath };
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export async function cloneAndReadDeployConfig(application, env, site) {
|
|
166
|
-
const repoPath = await cloneEnvRepository(env);
|
|
167
|
-
|
|
168
|
-
const { deployConfig, deployConfigFilePath } = await readDeployConfig(
|
|
169
|
-
application,
|
|
170
|
-
env,
|
|
171
|
-
site,
|
|
172
|
-
'values',
|
|
173
|
-
);
|
|
174
|
-
|
|
175
|
-
return { repoPath, deployConfigFilePath, deployConfig };
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Update the `tag` field in the YAML data.
|
|
180
|
-
* @param {object} deployConfig - The parsed YAML data.
|
|
181
|
-
* @param {string} tag - The new tag to set.
|
|
182
|
-
* @returns {object} - The updated YAML data.
|
|
183
|
-
*/
|
|
184
|
-
export function setTag(deployConfig, tag) {
|
|
185
|
-
if (deployConfig.image && deployConfig.image.tag) {
|
|
186
|
-
console.log(`Current image tag: ${deployConfig.image.tag}`);
|
|
187
|
-
deployConfig.image.tag = tag;
|
|
188
|
-
console.log(`Updated image tag to: ${deployConfig.image.tag}`);
|
|
189
|
-
} else {
|
|
190
|
-
console.error('No "image.tag" field found in the YAML file.');
|
|
191
|
-
process.exit(1);
|
|
192
|
-
}
|
|
193
|
-
return deployConfig;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Write the updated YAML data back to the file, commit the changes, and push to the remote branch.
|
|
198
|
-
* @param {string} repoPath - The path to the cloned repository.
|
|
199
|
-
* @param {string} deployConfigFilePath - The path to the YAML file.
|
|
200
|
-
* @param {object} deployConfig - The updated YAML data.
|
|
201
|
-
*/
|
|
202
|
-
|
|
203
|
-
export async function pushDeployConfig(repoPath, commitMessage) {
|
|
204
|
-
console.log('๐ Checking for updates to commit...');
|
|
205
|
-
// Check if there are any changes to commit
|
|
206
|
-
const status = (
|
|
207
|
-
await $`cd ${repoPath} && git status --porcelain`
|
|
208
|
-
).stdout.trim();
|
|
209
|
-
if (!status) {
|
|
210
|
-
console.log('โ
No changes to commit. Exiting.');
|
|
211
|
-
await $`rm -rf ${repoPath}`;
|
|
212
|
-
console.log('๐งน Cleanup complete.');
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
console.log('๐ Committing changes to the repository...');
|
|
217
|
-
await $`cd ${repoPath} && git config user.email "gitops@docupace.com"`;
|
|
218
|
-
await $`cd ${repoPath} && git add .`;
|
|
219
|
-
await $`cd ${repoPath} && git commit -m ${commitMessage}`;
|
|
220
|
-
await $`cd ${repoPath} && git push`;
|
|
221
|
-
console.log('โ
Changes pushed to the remote repository.');
|
|
222
|
-
|
|
223
|
-
console.log('๐งน Cleaning up...');
|
|
224
|
-
await $`rm -rf ${repoPath}`;
|
|
225
|
-
console.log('โ
Cleanup complete.');
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function writeDeployConfig(deployConfigFilePath, deployConfig) {
|
|
229
|
-
try {
|
|
230
|
-
fs.writeFileSync(deployConfigFilePath, yaml.dump(deployConfig), 'utf8');
|
|
231
|
-
console.log(`โ
Updated YAML file saved at: ${deployConfigFilePath}`);
|
|
232
|
-
} catch (error) {
|
|
233
|
-
console.error(`โ Error writing YAML file: ${error.message}`);
|
|
234
|
-
process.exit(1);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
export async function writeAndPushDeployConfig(
|
|
239
|
-
repoPath,
|
|
240
|
-
deployConfigFilePath,
|
|
241
|
-
deployConfig,
|
|
242
|
-
commitMessage,
|
|
243
|
-
) {
|
|
244
|
-
writeDeployConfig(deployConfigFilePath, deployConfig);
|
|
245
|
-
await pushDeployConfig(repoPath, commitMessage);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Main deploy function that orchestrates reading, updating, and writing the deployment config.
|
|
250
|
-
* @param {string} application - The application name.
|
|
251
|
-
* @param {string} site - The site name.
|
|
252
|
-
*/
|
|
253
|
-
|
|
254
|
-
async function getDeployTag() {
|
|
255
|
-
const gitTag = (
|
|
256
|
-
await $`git tag --points-at HEAD | grep '^v' | head -n 1`
|
|
257
|
-
).stdout.trim();
|
|
258
|
-
const deployTag = gitTag || `build-${process.env.BITBUCKET_BUILD_NUMBER}`;
|
|
259
|
-
return deployTag;
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
export async function deployTemplate(site) {
|
|
263
|
-
const deployTag = await getDeployTag();
|
|
264
|
-
console.log(`Deploying standard template for site: ${site}`);
|
|
265
|
-
console.log(`Using deployment tag: ${deployTag}`);
|
|
266
|
-
const { repoPath, deployConfigFilePath, deployConfig } =
|
|
267
|
-
await cloneAndReadDeployConfig('graphql-server', 'dev', site);
|
|
268
|
-
|
|
269
|
-
deployConfig.graphqlServer.standardConfigRemoteBranch = deployTag;
|
|
270
|
-
|
|
271
|
-
await writeAndPushDeployConfig(
|
|
272
|
-
repoPath,
|
|
273
|
-
deployConfigFilePath,
|
|
274
|
-
deployConfig,
|
|
275
|
-
`Update template to ${deployConfig.graphqlServer.standardConfigRemoteBranch}`,
|
|
276
|
-
);
|
|
277
|
-
console.log('Deployment completed successfully.');
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
export async function deploy(application, site) {
|
|
281
|
-
const deployTag = getDeployTag();
|
|
282
|
-
|
|
283
|
-
console.log(`Deploying application: ${application} for site: ${site}`);
|
|
284
|
-
console.log(`Using deployment tag: ${deployTag}`);
|
|
285
|
-
|
|
286
|
-
const { repoPath, deployConfigFilePath, deployConfig } =
|
|
287
|
-
await cloneAndReadDeployConfig(application, 'dev', site);
|
|
288
|
-
const updatedConfigData = setTag(deployConfig, deployTag);
|
|
289
|
-
await writeAndPushDeployConfig(
|
|
290
|
-
repoPath,
|
|
291
|
-
deployConfigFilePath,
|
|
292
|
-
updatedConfigData,
|
|
293
|
-
`Update image tag to ${deployConfig.image.tag}`,
|
|
294
|
-
);
|
|
295
|
-
|
|
296
|
-
console.log('Deployment completed successfully.');
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
async function getAppInfo(coreUrl) {
|
|
300
|
-
const url = `${coreUrl}/b2b/rest/v1/appinfo`;
|
|
301
|
-
return await axios.get(url);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
function getBranchNameFromVersion(version) {
|
|
305
|
-
const match = version.match(/(\d+\.\d+\.[^\-]+)(?:-(\d+)-g([a-f0-9]+))?/);
|
|
306
|
-
|
|
307
|
-
if (!match) {
|
|
308
|
-
throw new Error(`Invalid version format: ${version}`);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
const fullVersion = match[1]; // e.g., 24.19.19, 24.20.alpha3, etc.
|
|
312
|
-
const customBranch = match[2]; // e.g., 4, 58, or 0
|
|
313
|
-
const commitHash = match[3]; // e.g., 668edef58e, 634cf6c904, etc.
|
|
314
|
-
|
|
315
|
-
// If "alpha" is in the version or customBranch is non-zero, use the commit hash
|
|
316
|
-
if (fullVersion.includes('alpha') || (customBranch && customBranch !== '0')) {
|
|
317
|
-
return commitHash;
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
// Otherwise, use the full version
|
|
321
|
-
return fullVersion;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
async function getCoreBranchName(fromEnv, fromSite) {
|
|
325
|
-
const envConfig = environmentConfig[fromEnv];
|
|
326
|
-
const oldUrl = envConfig.oldUrl(fromSite);
|
|
327
|
-
const newUrl = envConfig.newUrl(fromSite) + '/core';
|
|
328
|
-
|
|
329
|
-
let appinfo;
|
|
330
|
-
try {
|
|
331
|
-
appinfo = await getAppInfo(oldUrl);
|
|
332
|
-
} catch {
|
|
333
|
-
try {
|
|
334
|
-
appinfo = await getAppInfo(newUrl);
|
|
335
|
-
} catch {
|
|
336
|
-
throw new Error('Cannot get app info');
|
|
337
|
-
}
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
const coreVersion = appinfo.data.core.version;
|
|
341
|
-
const branchName = getBranchNameFromVersion(coreVersion);
|
|
342
|
-
return branchName;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
export async function deployApp(
|
|
346
|
-
application,
|
|
347
|
-
appDetails,
|
|
348
|
-
fromEnv,
|
|
349
|
-
fromSite,
|
|
350
|
-
toEnv,
|
|
351
|
-
toSite,
|
|
352
|
-
) {
|
|
353
|
-
let changesMade = false;
|
|
354
|
-
console.log(`Processing application: ${application}`);
|
|
355
|
-
|
|
356
|
-
// Read the deployment configuration for the source environment and site
|
|
357
|
-
const { deployConfig: fromDeployConfig } = await readDeployConfig(
|
|
358
|
-
application,
|
|
359
|
-
fromEnv,
|
|
360
|
-
fromSite,
|
|
361
|
-
'values',
|
|
362
|
-
);
|
|
363
|
-
|
|
364
|
-
if (fromEnv !== toEnv) {
|
|
365
|
-
await copy2(
|
|
366
|
-
appDetails.repositoryName,
|
|
367
|
-
fromEnv,
|
|
368
|
-
toEnv,
|
|
369
|
-
fromDeployConfig.image.tag,
|
|
370
|
-
);
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
// Read the deployment configuration for the target environment and site
|
|
374
|
-
const {
|
|
375
|
-
deployConfig: toDeployConfig,
|
|
376
|
-
deployConfigFilePath: toDeployConfigFilePath,
|
|
377
|
-
} = await readDeployConfig(application, toEnv, toSite, 'values');
|
|
378
|
-
|
|
379
|
-
// Update target configuration using attributes specified in appInfo
|
|
380
|
-
for (const attribute of appDetails.deployAttributes) {
|
|
381
|
-
const sourceValue = lodash.get(fromDeployConfig, attribute);
|
|
382
|
-
|
|
383
|
-
if (sourceValue !== undefined) {
|
|
384
|
-
// Update the target configuration with the source value
|
|
385
|
-
lodash.set(toDeployConfig, attribute, sourceValue);
|
|
386
|
-
changesMade = true;
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
const {
|
|
391
|
-
deployConfig: toDeployAppConfig,
|
|
392
|
-
deployConfigFilePath: toDeployAppConfigFilePath,
|
|
393
|
-
} = await readDeployConfig(application, toEnv, toSite, 'app');
|
|
394
|
-
|
|
395
|
-
toDeployAppConfig.revision = fromDeployConfig.image.tag;
|
|
396
|
-
|
|
397
|
-
// Write updated configuration to the target YAML file
|
|
398
|
-
writeDeployConfig(toDeployConfigFilePath, toDeployConfig);
|
|
399
|
-
|
|
400
|
-
writeDeployConfig(toDeployAppConfigFilePath, toDeployAppConfig);
|
|
401
|
-
return changesMade;
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
export async function deployFromSite(
|
|
405
|
-
fromEnv,
|
|
406
|
-
fromSite,
|
|
407
|
-
toEnv,
|
|
408
|
-
toSite,
|
|
409
|
-
isDeployCore,
|
|
410
|
-
isDeployConnect,
|
|
411
|
-
) {
|
|
412
|
-
if (isDeployCore === 'true') {
|
|
413
|
-
const branchName = await getCoreBranchName(fromEnv, fromSite);
|
|
414
|
-
|
|
415
|
-
const coreEnv = toEnv == 'qa' ? 'preprod' : toEnv;
|
|
416
|
-
await deployCore(coreEnv, toSite, 'true', branchName);
|
|
417
|
-
|
|
418
|
-
// To be added back when splitting between on prem and in cloud
|
|
419
|
-
/*await runPipeline('site-deploy', 'deploy-core', 'main', {
|
|
420
|
-
ENV: toEnv,
|
|
421
|
-
SITE: toSite,
|
|
422
|
-
LIQUIBASE: 'true',
|
|
423
|
-
BRANCH: branchName,
|
|
424
|
-
});*/
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
if (isDeployConnect === 'true') {
|
|
428
|
-
const branch = '';
|
|
429
|
-
const getComparedVersion = await compareGetConnectSiteVersions(
|
|
430
|
-
fromEnv,
|
|
431
|
-
fromSite,
|
|
432
|
-
toEnv,
|
|
433
|
-
toSite,
|
|
434
|
-
);
|
|
435
|
-
if (getComparedVersion) {
|
|
436
|
-
console.log('Version to deploy is: ', getComparedVersion);
|
|
437
|
-
const connectDeployPath = await cloneRepository('connect-deploy');
|
|
438
|
-
// update target version set to git-commit-${getCommparedVersion}}
|
|
439
|
-
await updateAndCommitConnectVersion(
|
|
440
|
-
toEnv,
|
|
441
|
-
toSite,
|
|
442
|
-
getComparedVersion,
|
|
443
|
-
connectDeployPath,
|
|
444
|
-
);
|
|
445
|
-
// save and deploy
|
|
446
|
-
const runLiquibase = true;
|
|
447
|
-
await deployConnect(toEnv, toSite, runLiquibase, branch);
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
// Clone repositories for source and target environments
|
|
452
|
-
const fromRepoPath = await cloneEnvRepository(fromEnv);
|
|
453
|
-
const toRepoPath =
|
|
454
|
-
toEnv === fromEnv ? fromRepoPath : await cloneEnvRepository(toEnv);
|
|
455
|
-
|
|
456
|
-
// Initialize a flag to track changes
|
|
457
|
-
let changesMade = false;
|
|
458
|
-
const applications = await getAppTypesFromRepo(toEnv, toSite);
|
|
459
|
-
console.log(applications);
|
|
460
|
-
// Iterate over applications in appInfo
|
|
461
|
-
|
|
462
|
-
let appInfoLocal = Object.fromEntries(
|
|
463
|
-
applications.applications
|
|
464
|
-
.filter((application) =>
|
|
465
|
-
Object.prototype.hasOwnProperty.call(appInfo, application),
|
|
466
|
-
)
|
|
467
|
-
.map((application) => [application, appInfo[application]]),
|
|
468
|
-
);
|
|
469
|
-
|
|
470
|
-
// let appInfoLocal = {};
|
|
471
|
-
// for (const application of applications.applications) {
|
|
472
|
-
// if (Object.prototype.hasOwnProperty.call(appInfo, application)) {
|
|
473
|
-
// appInfoLocal[application] = appInfo[application];
|
|
474
|
-
// }
|
|
475
|
-
// }
|
|
476
|
-
|
|
477
|
-
if (Object.keys(appInfoLocal).length === 0) {
|
|
478
|
-
const { deployConfig: toDeployAppConfig } = await readDeployConfig(
|
|
479
|
-
fromSite,
|
|
480
|
-
toEnv,
|
|
481
|
-
toSite,
|
|
482
|
-
'app',
|
|
483
|
-
);
|
|
484
|
-
|
|
485
|
-
appInfoLocal[fromSite] = {
|
|
486
|
-
repositoryName: toDeployAppConfig.repo,
|
|
487
|
-
deployAttributes: ['image.tag'],
|
|
488
|
-
};
|
|
489
|
-
}
|
|
490
|
-
|
|
491
|
-
console.log('appinfoLocal', appInfoLocal);
|
|
492
|
-
|
|
493
|
-
for (const [application, appDetails] of Object.entries(appInfoLocal)) {
|
|
494
|
-
let cm = await deployApp(
|
|
495
|
-
application,
|
|
496
|
-
appDetails,
|
|
497
|
-
fromEnv,
|
|
498
|
-
fromSite,
|
|
499
|
-
toEnv,
|
|
500
|
-
toSite,
|
|
501
|
-
);
|
|
502
|
-
if (cm) {
|
|
503
|
-
changesMade = true;
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
// Push all changes if any updates were made
|
|
508
|
-
if (changesMade) {
|
|
509
|
-
await pushDeployConfig(
|
|
510
|
-
toRepoPath,
|
|
511
|
-
`Updated deployment configurations from ${fromEnv}/${fromSite} to ${toEnv}/${toSite}`,
|
|
512
|
-
);
|
|
513
|
-
console.log('โ
All configurations updated and pushed successfully.');
|
|
514
|
-
} else {
|
|
515
|
-
console.log('โ
No changes detected. Nothing to push.');
|
|
516
|
-
}
|
|
517
|
-
}
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import yaml from 'js-yaml';
|
|
4
|
+
import { $ } from 'zx';
|
|
5
|
+
|
|
6
|
+
const REPO_NAME = 'gitops-dev';
|
|
7
|
+
const REPO_URL = `https://x-token-auth:${process.env.GITOPS_DEV_REP_TOKEN}@bitbucket.org/dp-cloud/${REPO_NAME}.git`;
|
|
8
|
+
const GITOPS_BRANCH = 'master';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Clone the repository, read the YAML file, and return the parsed data and file paths.
|
|
12
|
+
* @param {string} application - The application name.
|
|
13
|
+
* @param {string} site - The site name.
|
|
14
|
+
* @returns {object} - The repository path, file path, and YAML data.
|
|
15
|
+
*/
|
|
16
|
+
export async function readDeployConfig(application, site) {
|
|
17
|
+
const repoPath = path.join(process.cwd(), REPO_NAME);
|
|
18
|
+
const deployConfigFilePath = path.join(repoPath, `dp/${site}/${application}.values.yaml`);
|
|
19
|
+
|
|
20
|
+
console.log(`Cloning repository: ${REPO_NAME}`);
|
|
21
|
+
await $`git clone ${REPO_URL}`;
|
|
22
|
+
console.log(`Repository cloned to: ${repoPath}`);
|
|
23
|
+
|
|
24
|
+
let deployConfig;
|
|
25
|
+
try {
|
|
26
|
+
deployConfig = yaml.load(fs.readFileSync(deployConfigFilePath, 'utf8'));
|
|
27
|
+
console.log(`YAML file loaded from: ${deployConfigFilePath}`);
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.error(`Error reading YAML file: ${error.message}`);
|
|
30
|
+
process.exit(1);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return { repoPath, deployConfigFilePath, deployConfig };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Update the `tag` field in the YAML data.
|
|
38
|
+
* @param {object} deployConfig - The parsed YAML data.
|
|
39
|
+
* @param {string} tag - The new tag to set.
|
|
40
|
+
* @returns {object} - The updated YAML data.
|
|
41
|
+
*/
|
|
42
|
+
export function setTag(deployConfig, tag) {
|
|
43
|
+
if (deployConfig.image && deployConfig.image.tag) {
|
|
44
|
+
console.log(`Current image tag: ${deployConfig.image.tag}`);
|
|
45
|
+
deployConfig.image.tag = tag;
|
|
46
|
+
console.log(`Updated image tag to: ${deployConfig.image.tag}`);
|
|
47
|
+
} else {
|
|
48
|
+
console.error('No "image.tag" field found in the YAML file.');
|
|
49
|
+
process.exit(1);
|
|
50
|
+
}
|
|
51
|
+
return deployConfig;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Write the updated YAML data back to the file, commit the changes, and push to the remote branch.
|
|
56
|
+
* @param {string} repoPath - The path to the cloned repository.
|
|
57
|
+
* @param {string} deployConfigFilePath - The path to the YAML file.
|
|
58
|
+
* @param {object} deployConfig - The updated YAML data.
|
|
59
|
+
*/
|
|
60
|
+
export async function writeDeployConfig(repoPath, deployConfigFilePath, deployConfig) {
|
|
61
|
+
try {
|
|
62
|
+
fs.writeFileSync(deployConfigFilePath, yaml.dump(deployConfig), 'utf8');
|
|
63
|
+
console.log(`Updated YAML file saved at: ${deployConfigFilePath}`);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.error(`Error writing YAML file: ${error.message}`);
|
|
66
|
+
process.exit(1);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
console.log('Committing changes to the repository...');
|
|
70
|
+
await $`cd ${repoPath} && git config user.email "gitops@docupace.com"`;
|
|
71
|
+
await $`cd ${repoPath} && git add ${deployConfigFilePath}`;
|
|
72
|
+
await $`cd ${repoPath} && git commit -m "Update image tag to ${deployConfig.image.tag}"`;
|
|
73
|
+
await $`cd ${repoPath} && git push origin ${GITOPS_BRANCH}`;
|
|
74
|
+
console.log('Changes pushed to the remote repository.');
|
|
75
|
+
|
|
76
|
+
console.log('Cleaning up...');
|
|
77
|
+
await $`rm -rf ${repoPath}`;
|
|
78
|
+
console.log('Cleanup complete.');
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Main deploy function that orchestrates reading, updating, and writing the deployment config.
|
|
83
|
+
* @param {string} application - The application name.
|
|
84
|
+
* @param {string} site - The site name.
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
async function getDeployTag() {
|
|
88
|
+
const gitTag = (await $`git tag --points-at HEAD | grep '^v' | head -n 1`).stdout.trim()
|
|
89
|
+
const deployTag = gitTag || `build-${process.env.BITBUCKET_BUILD_NUMBER}`;
|
|
90
|
+
return deployTag;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export async function deployTemplate(site) {
|
|
94
|
+
const deployTag = await getDeployTag();
|
|
95
|
+
console.log(`Deploying standard template for site: ${site}`);
|
|
96
|
+
console.log(`Using deployment tag: ${deployTag}`);
|
|
97
|
+
const { repoPath, deployConfigFilePath, deployConfig } = await readDeployConfig('graphql-server', site);
|
|
98
|
+
|
|
99
|
+
deployConfig.graphqlServer.standardConfigRemoteBranch = deployTag;
|
|
100
|
+
|
|
101
|
+
await writeDeployConfig(repoPath, deployConfigFilePath, deployConfig);
|
|
102
|
+
console.log('Deployment completed successfully.');
|
|
103
|
+
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
export async function deploy(application, site) {
|
|
108
|
+
const deployTag = getDeployTag();
|
|
109
|
+
|
|
110
|
+
console.log(`Deploying application: ${application} for site: ${site}`);
|
|
111
|
+
console.log(`Using deployment tag: ${deployTag}`);
|
|
112
|
+
|
|
113
|
+
const { repoPath, deployConfigFilePath, deployConfig } = await readDeployConfig(application, site);
|
|
114
|
+
const updatedConfigData = setTag(deployConfig, deployTag);
|
|
115
|
+
await writeDeployConfig(repoPath, deployConfigFilePath, updatedConfigData);
|
|
116
|
+
|
|
117
|
+
console.log('Deployment completed successfully.');
|
|
118
|
+
}
|