@docupace/pipeline 1.0.2-4.1 → 1.0.2-5.1

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