@docupace/pipeline 1.0.56 โ†’ 1.0.57

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 (44) 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/eslint.config.js +26 -26
  6. package/next/changeset.sh +75 -75
  7. package/next/copy.sh +34 -34
  8. package/next/deploy.sh +40 -40
  9. package/next/execute.sh +123 -123
  10. package/next/sonar.sh +23 -23
  11. package/package.json +50 -50
  12. package/src/auth0/alias.js +72 -72
  13. package/src/auth0/cli.js +0 -0
  14. package/src/auth0/export.js +43 -43
  15. package/src/auth0/metadata.js +132 -132
  16. package/src/auth0/vault.js +107 -107
  17. package/src/changeset/changeset-config.js +34 -34
  18. package/src/changeset/cli.js +200 -200
  19. package/src/changeset/create.js +205 -205
  20. package/src/changeset/package-util.js +86 -86
  21. package/src/changeset/util.js +43 -43
  22. package/src/changeset/validate.js +84 -84
  23. package/src/changeset/version.js +102 -102
  24. package/src/jenkins/setParams.groovy +58 -58
  25. package/src/next/alias.js +53 -53
  26. package/src/next/apache.js +81 -81
  27. package/src/next/cli.js +226 -228
  28. package/src/next/config.js +137 -137
  29. package/src/next/connect.js +227 -227
  30. package/src/next/copy.js +61 -61
  31. package/src/next/deploy.js +600 -595
  32. package/src/next/haproxy.js +75 -75
  33. package/src/next/jira.js +84 -84
  34. package/src/next/logger.js +17 -17
  35. package/src/next/pipline.js +114 -114
  36. package/src/next/publish.js +71 -71
  37. package/src/next/pull.js +7 -7
  38. package/src/next/sonar.js +23 -23
  39. package/src/next/util.js +93 -93
  40. package/src/next/vault.js +45 -45
  41. package/terraform/apply.sh +37 -37
  42. package/terraform/init.sh +72 -72
  43. package/terraform/plan.sh +76 -76
  44. package/terraform/validate.sh +17 -17
@@ -1,595 +1,600 @@
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
- function getDeploymentTargets(tag, config) {
278
- const semverRegex = /^v?(\d+)\.(\d+)\.(\d+)(?:-([\w.\-]+))?$/;
279
- const match = tag.match(semverRegex);
280
-
281
- if (!match) {
282
- throw new Error(`Invalid tag format: ${tag}`);
283
- }
284
-
285
- const [_, major, minor, patch, preRelease] = match;
286
- const isAlpha = preRelease && preRelease.toLowerCase().includes('alpha');
287
-
288
- return config
289
- .filter((entry) => {
290
- if (entry.version.toLowerCase() === 'alpha' && isAlpha) {
291
- return true;
292
- }
293
-
294
- const [entryMajor, entryMinor] = entry.version.split('.');
295
- return (
296
- !isAlpha &&
297
- parseInt(entryMajor) === parseInt(major) &&
298
- parseInt(entryMinor) === parseInt(minor)
299
- );
300
- })
301
- .map((entry) => ({ env: entry.env, site: entry.site }));
302
- }
303
-
304
- export async function getDefaultDeploySite(deployTag) {
305
- const repPath = await cloneEnvRepository('dev');
306
- const autoDeployConfigPath = path.resolve(repPath, 'auto-deploy.yaml');
307
- const autoDeployConfig = yaml.load(
308
- fs.readFileSync(autoDeployConfigPath, 'utf8'),
309
- );
310
- return getDeploymentTargets(deployTag, autoDeployConfig);
311
- }
312
-
313
- export async function deployTemplate(env, site, deployTag) {
314
- console.log(`Deploying standard template for site: ${site}`);
315
- console.log(`Using deployment tag: ${deployTag}`);
316
- const { repoPath, deployConfigFilePath, deployConfig } =
317
- await cloneAndReadDeployConfig('graphql-server', env, site);
318
-
319
- deployConfig.graphqlServer.standardConfigRemoteBranch = deployTag;
320
-
321
- await writeAndPushDeployConfig(
322
- repoPath,
323
- deployConfigFilePath,
324
- deployConfig,
325
- `Update template to ${deployConfig.graphqlServer.standardConfigRemoteBranch}`,
326
- );
327
- console.log('Deployment completed successfully.');
328
- }
329
-
330
- export async function deployAppWithTag(application, env, site, deployTag) {
331
- console.log(`Deploying application: ${application} for site: ${site}`);
332
- console.log(`Using deployment tag: ${deployTag}`);
333
- const appDetails = appInfo[application];
334
-
335
- if (env !== 'dev') {
336
- await copy2(
337
- appDetails.repositoryName,
338
- 'dev',
339
- env,
340
- deployTag,
341
- appDetails.imageName,
342
- );
343
- }
344
-
345
- const { repoPath, deployConfigFilePath, deployConfig } =
346
- await cloneAndReadDeployConfig(application, env, site);
347
- const updatedConfigData = setTag(deployConfig, deployTag);
348
-
349
- const {
350
- deployConfig: toDeployAppConfig,
351
- deployConfigFilePath: toDeployAppConfigFilePath,
352
- } = await readDeployConfig(application, env, site, 'app');
353
-
354
- toDeployAppConfig.revision = appDetails.getAppRevision
355
- ? appDetails.getAppRevision(updatedConfigData)
356
- : updatedConfigData.image.tag;
357
-
358
- writeDeployConfig(toDeployAppConfigFilePath, toDeployAppConfig);
359
-
360
- await writeAndPushDeployConfig(
361
- repoPath,
362
- deployConfigFilePath,
363
- updatedConfigData,
364
- `Update image tag to ${deployConfig.image.tag}`,
365
- );
366
-
367
- console.log('Deployment completed successfully.');
368
- }
369
-
370
- async function getAppInfo(coreUrl) {
371
- const url = `${coreUrl}/b2b/rest/v1/appinfo`;
372
- return await axios.get(url);
373
- }
374
-
375
- function getBranchNameFromVersion(version) {
376
- const match = version.match(/(\d+\.\d+\.[^\-]+)(?:-(\d+)-g([a-f0-9]+))?/);
377
-
378
- if (!match) {
379
- throw new Error(`Invalid version format: ${version}`);
380
- }
381
-
382
- const fullVersion = match[1]; // e.g., 24.19.19, 24.20.alpha3, etc.
383
- const customBranch = match[2]; // e.g., 4, 58, or 0
384
- const commitHash = match[3]; // e.g., 668edef58e, 634cf6c904, etc.
385
-
386
- // If "alpha" is in the version or customBranch is non-zero, use the commit hash
387
- if (fullVersion.includes('alpha') || (customBranch && customBranch !== '0')) {
388
- return commitHash;
389
- }
390
-
391
- // Otherwise, use the full version
392
- return 'v' + fullVersion;
393
- }
394
-
395
- async function getCoreBranchName(fromEnv, fromSite) {
396
- const envConfig = environmentConfig[fromEnv];
397
- const oldUrl = envConfig.oldUrl(fromSite);
398
- const newUrl = envConfig.newUrl(fromSite) + '/core';
399
-
400
- let appinfo;
401
- try {
402
- appinfo = await getAppInfo(oldUrl);
403
- } catch {
404
- try {
405
- appinfo = await getAppInfo(newUrl);
406
- } catch {
407
- throw new Error('Cannot get app info');
408
- }
409
- }
410
-
411
- const coreVersion = appinfo.data.core.version;
412
- const branchName = getBranchNameFromVersion(coreVersion);
413
- return branchName;
414
- }
415
-
416
- export async function deployApp(
417
- application,
418
- appDetails,
419
- fromEnv,
420
- fromSite,
421
- toEnv,
422
- toSite,
423
- ) {
424
- let changesMade = false;
425
- console.log(`Processing application: ${application}`);
426
-
427
- // Read the deployment configuration for the source environment and site
428
- const { deployConfig: fromDeployConfig } = await readDeployConfig(
429
- application,
430
- fromEnv,
431
- fromSite,
432
- 'values',
433
- );
434
-
435
- if (fromEnv !== toEnv) {
436
- await copy2(
437
- appDetails.repositoryName,
438
- fromEnv,
439
- toEnv,
440
- fromDeployConfig.image.tag,
441
- appDetails.imageName,
442
- );
443
- }
444
-
445
- // Read the deployment configuration for the target environment and site
446
- const {
447
- deployConfig: toDeployConfig,
448
- deployConfigFilePath: toDeployConfigFilePath,
449
- } = await readDeployConfig(application, toEnv, toSite, 'values');
450
-
451
- // Update target configuration using attributes specified in appInfo
452
- for (const attribute of appDetails.deployAttributes) {
453
- const sourceValue = lodash.get(fromDeployConfig, attribute);
454
-
455
- if (sourceValue !== undefined) {
456
- // Update the target configuration with the source value
457
- lodash.set(toDeployConfig, attribute, sourceValue);
458
- changesMade = true;
459
- }
460
- }
461
-
462
- if (appDetails.updateConfigFunction) {
463
- appDetails.updateConfigFunction(toDeployConfig);
464
- }
465
-
466
- const {
467
- deployConfig: toDeployAppConfig,
468
- deployConfigFilePath: toDeployAppConfigFilePath,
469
- } = await readDeployConfig(application, toEnv, toSite, 'app');
470
-
471
- toDeployAppConfig.revision = appDetails.getAppRevision
472
- ? appDetails.getAppRevision(fromDeployConfig)
473
- : fromDeployConfig.image.tag;
474
-
475
- // Write updated configuration to the target YAML file
476
- writeDeployConfig(toDeployConfigFilePath, toDeployConfig);
477
-
478
- writeDeployConfig(toDeployAppConfigFilePath, toDeployAppConfig);
479
- return changesMade;
480
- }
481
-
482
- export async function deployFromSite(
483
- fromEnv,
484
- fromSite,
485
- toEnv,
486
- toSite,
487
- isDeployCore,
488
- isDeployConnect,
489
- ) {
490
- if (isDeployCore === 'true') {
491
- const branchName = await getCoreBranchName(fromEnv, fromSite);
492
-
493
- const coreEnv = toEnv == 'qa' ? 'preprod' : toEnv;
494
- await deployCore(coreEnv, toSite, 'true', branchName);
495
-
496
- // To be added back when splitting between on prem and in cloud
497
- /*await runPipeline('site-deploy', 'deploy-core', 'main', {
498
- ENV: toEnv,
499
- SITE: toSite,
500
- LIQUIBASE: 'true',
501
- BRANCH: branchName,
502
- });*/
503
- }
504
-
505
- if (isDeployConnect === 'true') {
506
- const branch = '';
507
- const getComparedVersion = await compareGetConnectSiteVersions(
508
- fromEnv,
509
- fromSite,
510
- toEnv,
511
- toSite,
512
- );
513
- if (getComparedVersion) {
514
- console.log('Version to deploy is: ', getComparedVersion);
515
- const connectDeployPath = await cloneRepository('connect-deploy');
516
- // update target version set to git-commit-${getCommparedVersion}}
517
- await updateAndCommitConnectVersion(
518
- toEnv,
519
- toSite,
520
- getComparedVersion,
521
- connectDeployPath,
522
- );
523
- // save and deploy
524
- const runLiquibase = true;
525
- await deployConnect(toEnv, toSite, runLiquibase, branch);
526
- }
527
- }
528
-
529
- // Clone repositories for source and target environments
530
- const fromRepoPath = await cloneEnvRepository(fromEnv);
531
- const toRepoPath =
532
- toEnv === fromEnv ? fromRepoPath : await cloneEnvRepository(toEnv);
533
-
534
- // Initialize a flag to track changes
535
- let changesMade = false;
536
- const applications = await getAppTypesFromRepo(toEnv, toSite);
537
- console.log(applications);
538
- // Iterate over applications in appInfo
539
-
540
- let appInfoLocal = Object.fromEntries(
541
- applications.applications
542
- .filter((application) =>
543
- Object.prototype.hasOwnProperty.call(appInfo, application),
544
- )
545
- .map((application) => [application, appInfo[application]]),
546
- );
547
-
548
- // let appInfoLocal = {};
549
- // for (const application of applications.applications) {
550
- // if (Object.prototype.hasOwnProperty.call(appInfo, application)) {
551
- // appInfoLocal[application] = appInfo[application];
552
- // }
553
- // }
554
-
555
- if (Object.keys(appInfoLocal).length === 0) {
556
- const { deployConfig: toDeployAppConfig } = await readDeployConfig(
557
- fromSite,
558
- toEnv,
559
- toSite,
560
- 'app',
561
- );
562
-
563
- appInfoLocal[fromSite] = {
564
- repositoryName: toDeployAppConfig.repo,
565
- deployAttributes: ['image.tag'],
566
- };
567
- }
568
-
569
- console.log('appinfoLocal', appInfoLocal);
570
-
571
- for (const [application, appDetails] of Object.entries(appInfoLocal)) {
572
- let cm = await deployApp(
573
- application,
574
- appDetails,
575
- fromEnv,
576
- fromSite,
577
- toEnv,
578
- toSite,
579
- );
580
- if (cm) {
581
- changesMade = true;
582
- }
583
- }
584
-
585
- // Push all changes if any updates were made
586
- if (changesMade) {
587
- await pushDeployConfig(
588
- toRepoPath,
589
- `Updated deployment configurations from ${fromEnv}/${fromSite} to ${toEnv}/${toSite}`,
590
- );
591
- console.log('โœ… All configurations updated and pushed successfully.');
592
- } else {
593
- console.log('โœ… No changes detected. Nothing to push.');
594
- }
595
- }
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
+ function getDeploymentTargets(tag, config) {
278
+ const semverRegex = /^v?(\d+)\.(\d+)\.(\d+)(?:-([\w.\-]+))?$/;
279
+ const match = tag.match(semverRegex);
280
+
281
+ if (!match) {
282
+ throw new Error(`Invalid tag format: ${tag}`);
283
+ }
284
+
285
+ const [_, major, minor, patch, preRelease] = match;
286
+ const isAlpha = preRelease && preRelease.toLowerCase().includes('alpha');
287
+
288
+ return config
289
+ .filter((entry) => {
290
+ if (entry.version.toLowerCase() === 'alpha' && isAlpha) {
291
+ return true;
292
+ }
293
+
294
+ const [entryMajor, entryMinor] = entry.version.split('.');
295
+ return (
296
+ !isAlpha &&
297
+ parseInt(entryMajor) === parseInt(major) &&
298
+ parseInt(entryMinor) === parseInt(minor)
299
+ );
300
+ })
301
+ .map((entry) => ({ env: entry.env, site: entry.site }));
302
+ }
303
+
304
+ export async function getDefaultDeploySite(deployTag) {
305
+ const repPath = await cloneEnvRepository('dev');
306
+ const autoDeployConfigPath = path.resolve(repPath, 'auto-deploy.yaml');
307
+ const autoDeployConfig = yaml.load(
308
+ fs.readFileSync(autoDeployConfigPath, 'utf8'),
309
+ );
310
+ return getDeploymentTargets(deployTag, autoDeployConfig);
311
+ }
312
+
313
+ export async function deployTemplate(
314
+ env,
315
+ site,
316
+ deployTag,
317
+ templateType = 'standardConfigRemoteBranch',
318
+ ) {
319
+ console.log(`Deploying standard template for site: ${site}`);
320
+ console.log(`Using deployment tag: ${deployTag}`);
321
+ const { repoPath, deployConfigFilePath, deployConfig } =
322
+ await cloneAndReadDeployConfig('graphql-server', env, site);
323
+
324
+ deployConfig.graphqlServer[templateType] = deployTag;
325
+
326
+ await writeAndPushDeployConfig(
327
+ repoPath,
328
+ deployConfigFilePath,
329
+ deployConfig,
330
+ `Update template to ${deployConfig.graphqlServer.standardConfigRemoteBranch}`,
331
+ );
332
+ console.log('Deployment completed successfully.');
333
+ }
334
+
335
+ export async function deployAppWithTag(application, env, site, deployTag) {
336
+ console.log(`Deploying application: ${application} for site: ${site}`);
337
+ console.log(`Using deployment tag: ${deployTag}`);
338
+ const appDetails = appInfo[application];
339
+
340
+ if (env !== 'dev') {
341
+ await copy2(
342
+ appDetails.repositoryName,
343
+ 'dev',
344
+ env,
345
+ deployTag,
346
+ appDetails.imageName,
347
+ );
348
+ }
349
+
350
+ const { repoPath, deployConfigFilePath, deployConfig } =
351
+ await cloneAndReadDeployConfig(application, env, site);
352
+ const updatedConfigData = setTag(deployConfig, deployTag);
353
+
354
+ const {
355
+ deployConfig: toDeployAppConfig,
356
+ deployConfigFilePath: toDeployAppConfigFilePath,
357
+ } = await readDeployConfig(application, env, site, 'app');
358
+
359
+ toDeployAppConfig.revision = appDetails.getAppRevision
360
+ ? appDetails.getAppRevision(updatedConfigData)
361
+ : updatedConfigData.image.tag;
362
+
363
+ writeDeployConfig(toDeployAppConfigFilePath, toDeployAppConfig);
364
+
365
+ await writeAndPushDeployConfig(
366
+ repoPath,
367
+ deployConfigFilePath,
368
+ updatedConfigData,
369
+ `Update image tag to ${deployConfig.image.tag}`,
370
+ );
371
+
372
+ console.log('Deployment completed successfully.');
373
+ }
374
+
375
+ async function getAppInfo(coreUrl) {
376
+ const url = `${coreUrl}/b2b/rest/v1/appinfo`;
377
+ return await axios.get(url);
378
+ }
379
+
380
+ function getBranchNameFromVersion(version) {
381
+ const match = version.match(/(\d+\.\d+\.[^\-]+)(?:-(\d+)-g([a-f0-9]+))?/);
382
+
383
+ if (!match) {
384
+ throw new Error(`Invalid version format: ${version}`);
385
+ }
386
+
387
+ const fullVersion = match[1]; // e.g., 24.19.19, 24.20.alpha3, etc.
388
+ const customBranch = match[2]; // e.g., 4, 58, or 0
389
+ const commitHash = match[3]; // e.g., 668edef58e, 634cf6c904, etc.
390
+
391
+ // If "alpha" is in the version or customBranch is non-zero, use the commit hash
392
+ if (fullVersion.includes('alpha') || (customBranch && customBranch !== '0')) {
393
+ return commitHash;
394
+ }
395
+
396
+ // Otherwise, use the full version
397
+ return 'v' + fullVersion;
398
+ }
399
+
400
+ async function getCoreBranchName(fromEnv, fromSite) {
401
+ const envConfig = environmentConfig[fromEnv];
402
+ const oldUrl = envConfig.oldUrl(fromSite);
403
+ const newUrl = envConfig.newUrl(fromSite) + '/core';
404
+
405
+ let appinfo;
406
+ try {
407
+ appinfo = await getAppInfo(oldUrl);
408
+ } catch {
409
+ try {
410
+ appinfo = await getAppInfo(newUrl);
411
+ } catch {
412
+ throw new Error('Cannot get app info');
413
+ }
414
+ }
415
+
416
+ const coreVersion = appinfo.data.core.version;
417
+ const branchName = getBranchNameFromVersion(coreVersion);
418
+ return branchName;
419
+ }
420
+
421
+ export async function deployApp(
422
+ application,
423
+ appDetails,
424
+ fromEnv,
425
+ fromSite,
426
+ toEnv,
427
+ toSite,
428
+ ) {
429
+ let changesMade = false;
430
+ console.log(`Processing application: ${application}`);
431
+
432
+ // Read the deployment configuration for the source environment and site
433
+ const { deployConfig: fromDeployConfig } = await readDeployConfig(
434
+ application,
435
+ fromEnv,
436
+ fromSite,
437
+ 'values',
438
+ );
439
+
440
+ if (fromEnv !== toEnv) {
441
+ await copy2(
442
+ appDetails.repositoryName,
443
+ fromEnv,
444
+ toEnv,
445
+ fromDeployConfig.image.tag,
446
+ appDetails.imageName,
447
+ );
448
+ }
449
+
450
+ // Read the deployment configuration for the target environment and site
451
+ const {
452
+ deployConfig: toDeployConfig,
453
+ deployConfigFilePath: toDeployConfigFilePath,
454
+ } = await readDeployConfig(application, toEnv, toSite, 'values');
455
+
456
+ // Update target configuration using attributes specified in appInfo
457
+ for (const attribute of appDetails.deployAttributes) {
458
+ const sourceValue = lodash.get(fromDeployConfig, attribute);
459
+
460
+ if (sourceValue !== undefined) {
461
+ // Update the target configuration with the source value
462
+ lodash.set(toDeployConfig, attribute, sourceValue);
463
+ changesMade = true;
464
+ }
465
+ }
466
+
467
+ if (appDetails.updateConfigFunction) {
468
+ appDetails.updateConfigFunction(toDeployConfig);
469
+ }
470
+
471
+ const {
472
+ deployConfig: toDeployAppConfig,
473
+ deployConfigFilePath: toDeployAppConfigFilePath,
474
+ } = await readDeployConfig(application, toEnv, toSite, 'app');
475
+
476
+ toDeployAppConfig.revision = appDetails.getAppRevision
477
+ ? appDetails.getAppRevision(fromDeployConfig)
478
+ : fromDeployConfig.image.tag;
479
+
480
+ // Write updated configuration to the target YAML file
481
+ writeDeployConfig(toDeployConfigFilePath, toDeployConfig);
482
+
483
+ writeDeployConfig(toDeployAppConfigFilePath, toDeployAppConfig);
484
+ return changesMade;
485
+ }
486
+
487
+ export async function deployFromSite(
488
+ fromEnv,
489
+ fromSite,
490
+ toEnv,
491
+ toSite,
492
+ isDeployCore,
493
+ isDeployConnect,
494
+ ) {
495
+ if (isDeployCore === 'true') {
496
+ const branchName = await getCoreBranchName(fromEnv, fromSite);
497
+
498
+ const coreEnv = toEnv == 'qa' ? 'preprod' : toEnv;
499
+ await deployCore(coreEnv, toSite, 'true', branchName);
500
+
501
+ // To be added back when splitting between on prem and in cloud
502
+ /*await runPipeline('site-deploy', 'deploy-core', 'main', {
503
+ ENV: toEnv,
504
+ SITE: toSite,
505
+ LIQUIBASE: 'true',
506
+ BRANCH: branchName,
507
+ });*/
508
+ }
509
+
510
+ if (isDeployConnect === 'true') {
511
+ const branch = '';
512
+ const getComparedVersion = await compareGetConnectSiteVersions(
513
+ fromEnv,
514
+ fromSite,
515
+ toEnv,
516
+ toSite,
517
+ );
518
+ if (getComparedVersion) {
519
+ console.log('Version to deploy is: ', getComparedVersion);
520
+ const connectDeployPath = await cloneRepository('connect-deploy');
521
+ // update target version set to git-commit-${getCommparedVersion}}
522
+ await updateAndCommitConnectVersion(
523
+ toEnv,
524
+ toSite,
525
+ getComparedVersion,
526
+ connectDeployPath,
527
+ );
528
+ // save and deploy
529
+ const runLiquibase = true;
530
+ await deployConnect(toEnv, toSite, runLiquibase, branch);
531
+ }
532
+ }
533
+
534
+ // Clone repositories for source and target environments
535
+ const fromRepoPath = await cloneEnvRepository(fromEnv);
536
+ const toRepoPath =
537
+ toEnv === fromEnv ? fromRepoPath : await cloneEnvRepository(toEnv);
538
+
539
+ // Initialize a flag to track changes
540
+ let changesMade = false;
541
+ const applications = await getAppTypesFromRepo(toEnv, toSite);
542
+ console.log(applications);
543
+ // Iterate over applications in appInfo
544
+
545
+ let appInfoLocal = Object.fromEntries(
546
+ applications.applications
547
+ .filter((application) =>
548
+ Object.prototype.hasOwnProperty.call(appInfo, application),
549
+ )
550
+ .map((application) => [application, appInfo[application]]),
551
+ );
552
+
553
+ // let appInfoLocal = {};
554
+ // for (const application of applications.applications) {
555
+ // if (Object.prototype.hasOwnProperty.call(appInfo, application)) {
556
+ // appInfoLocal[application] = appInfo[application];
557
+ // }
558
+ // }
559
+
560
+ if (Object.keys(appInfoLocal).length === 0) {
561
+ const { deployConfig: toDeployAppConfig } = await readDeployConfig(
562
+ fromSite,
563
+ toEnv,
564
+ toSite,
565
+ 'app',
566
+ );
567
+
568
+ appInfoLocal[fromSite] = {
569
+ repositoryName: toDeployAppConfig.repo,
570
+ deployAttributes: ['image.tag'],
571
+ };
572
+ }
573
+
574
+ console.log('appinfoLocal', appInfoLocal);
575
+
576
+ for (const [application, appDetails] of Object.entries(appInfoLocal)) {
577
+ let cm = await deployApp(
578
+ application,
579
+ appDetails,
580
+ fromEnv,
581
+ fromSite,
582
+ toEnv,
583
+ toSite,
584
+ );
585
+ if (cm) {
586
+ changesMade = true;
587
+ }
588
+ }
589
+
590
+ // Push all changes if any updates were made
591
+ if (changesMade) {
592
+ await pushDeployConfig(
593
+ toRepoPath,
594
+ `Updated deployment configurations from ${fromEnv}/${fromSite} to ${toEnv}/${toSite}`,
595
+ );
596
+ console.log('โœ… All configurations updated and pushed successfully.');
597
+ } else {
598
+ console.log('โœ… No changes detected. Nothing to push.');
599
+ }
600
+ }