@docupace/pipeline 1.0.47 โ†’ 1.0.48

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