@docupace/pipeline 1.0.81 → 1.0.83
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/README.md +1 -1
- package/package.json +3 -2
- package/src/merge/cli-merge-all.js +18 -4
- package/src/merge/cli-merge.js +87 -7
- package/src/merge/common.js +7 -0
- package/src/micro/cli.js +51 -0
- package/src/micro/ms-deploy.js +451 -0
- package/src/micro/ms-publish.js +186 -0
- package/src/next/cli.js +1 -10
- package/src/micro/deploy-microservice.js +0 -153
- package/src/micro/publish-ms.js +0 -188
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docupace/pipeline",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.83",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
7
7
|
"dp-changeset-cli": "src/changeset/cli.js",
|
|
8
8
|
"dp-next-cli": "src/next/cli.js",
|
|
9
9
|
"dp-auth0-cli": "src/auth0/cli.js",
|
|
10
|
-
"dp-merge-cli": "src/merge/cli.js"
|
|
10
|
+
"dp-merge-cli": "src/merge/cli.js",
|
|
11
|
+
"dp-micro-cli": "src/micro/cli.js"
|
|
11
12
|
},
|
|
12
13
|
"keywords": [
|
|
13
14
|
"bash"
|
|
@@ -3,6 +3,7 @@ import { readFileSync, existsSync, mkdirSync } from 'fs';
|
|
|
3
3
|
import { join, dirname } from 'path';
|
|
4
4
|
import { fileURLToPath } from 'url';
|
|
5
5
|
import { getExistingPRsWithDescription } from './pr.js';
|
|
6
|
+
import { BuildResults } from './common.js';
|
|
6
7
|
|
|
7
8
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
8
9
|
|
|
@@ -214,18 +215,22 @@ async function runPreparePhase(
|
|
|
214
215
|
|
|
215
216
|
// Log result based on exit code
|
|
216
217
|
switch (result.exitCode) {
|
|
217
|
-
case
|
|
218
|
+
case BuildResults.success:
|
|
218
219
|
console.log(`✅ ${configName}: Merge and build successful`);
|
|
219
220
|
break;
|
|
220
|
-
case
|
|
221
|
+
case BuildResults.unresolvedConflicts:
|
|
221
222
|
console.log(`⚠️ ${configName}: Merge has unresolved conflicts`);
|
|
222
223
|
break;
|
|
223
|
-
case
|
|
224
|
+
case BuildResults.buildFailed:
|
|
224
225
|
console.log(`⚠️ ${configName}: Build failed`);
|
|
225
226
|
break;
|
|
226
|
-
case
|
|
227
|
+
case BuildResults.noChanges:
|
|
227
228
|
console.log(`✅ ${configName}: No Changes`);
|
|
228
229
|
break;
|
|
230
|
+
case BuildResults.changeLogsExist:
|
|
231
|
+
console.log(`❌ Stopping Merge`);
|
|
232
|
+
process.exit(1);
|
|
233
|
+
break;
|
|
229
234
|
default:
|
|
230
235
|
console.log(
|
|
231
236
|
`❌ ${configName}: Unknown error (exit code: ${result.exitCode})`,
|
|
@@ -428,6 +433,15 @@ async function mergeAll(verbose = false) {
|
|
|
428
433
|
verbose,
|
|
429
434
|
);
|
|
430
435
|
|
|
436
|
+
if (
|
|
437
|
+
branchResult.success &&
|
|
438
|
+
branchResult.summary.successes > 0 &&
|
|
439
|
+
i < config.mergeBranches.length - 1
|
|
440
|
+
) {
|
|
441
|
+
console.log('Pausing before next merge');
|
|
442
|
+
await new Promise((res) => setTimeout(res, 90000));
|
|
443
|
+
}
|
|
444
|
+
|
|
431
445
|
if (!branchResult.success) {
|
|
432
446
|
console.log(`Exiting due to failure of one or more merges`);
|
|
433
447
|
process.exit(1);
|
package/src/merge/cli-merge.js
CHANGED
|
@@ -3,6 +3,7 @@ import { createBitbucketPR } from './pr.js';
|
|
|
3
3
|
import { autoResolveConflicts } from './cli-resolve.js';
|
|
4
4
|
import { getPackageNames } from '../changeset/package-util.js';
|
|
5
5
|
import { writeChangesetFile } from '../changeset/create.js';
|
|
6
|
+
import { BuildResults } from './common.js';
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* Create and checkout merge branch from target branch
|
|
@@ -216,9 +217,27 @@ function mergeIntoTarget(
|
|
|
216
217
|
stdio: verbose ? 'inherit' : 'pipe',
|
|
217
218
|
});
|
|
218
219
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
220
|
+
// Delete local merge branch
|
|
221
|
+
try {
|
|
222
|
+
execSync(`git branch -d ${mergeBranchName}`, {
|
|
223
|
+
stdio: 'pipe',
|
|
224
|
+
});
|
|
225
|
+
if (verbose)
|
|
226
|
+
console.log(`🗑️ Deleted local merge branch: ${mergeBranchName}`);
|
|
227
|
+
} catch {
|
|
228
|
+
// Branch doesn't exist, which is fine
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// Delete remote merge branch
|
|
232
|
+
try {
|
|
233
|
+
execSync(`git push origin --delete ${mergeBranchName}`, {
|
|
234
|
+
stdio: 'pipe',
|
|
235
|
+
});
|
|
236
|
+
if (verbose)
|
|
237
|
+
console.log(`🗑️ Deleted remote merge branch: ${mergeBranchName}`);
|
|
238
|
+
} catch (e) {
|
|
239
|
+
console.error('Failed to delete remote branch', e);
|
|
240
|
+
}
|
|
222
241
|
|
|
223
242
|
if (verbose) console.log(`✅ Successfully merged into ${targetBranch}`);
|
|
224
243
|
} else {
|
|
@@ -269,6 +288,62 @@ function cleanupFailedMerge(verbose = false) {
|
|
|
269
288
|
}
|
|
270
289
|
}
|
|
271
290
|
|
|
291
|
+
/**
|
|
292
|
+
* Check if source branch has uncommitted changesets
|
|
293
|
+
*/
|
|
294
|
+
function checkForUncommittedChangesets(sourceBranch, verbose = false) {
|
|
295
|
+
if (verbose)
|
|
296
|
+
console.log(`🔍 Checking for uncommitted changesets in ${sourceBranch}`);
|
|
297
|
+
|
|
298
|
+
// Store current branch
|
|
299
|
+
const currentBranch = execSync('git branch --show-current', {
|
|
300
|
+
encoding: 'utf8',
|
|
301
|
+
stdio: 'pipe',
|
|
302
|
+
}).trim();
|
|
303
|
+
|
|
304
|
+
try {
|
|
305
|
+
// Checkout source branch
|
|
306
|
+
|
|
307
|
+
execSync(`git checkout ${sourceBranch}`, {
|
|
308
|
+
stdio: 'pipe',
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
execSync(`git pull`, {
|
|
312
|
+
stdio: verbose ? 'inherit' : 'pipe',
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
// Check for .md files in .changeset directory (excluding README.md)
|
|
316
|
+
try {
|
|
317
|
+
const changesetFiles = execSync(
|
|
318
|
+
'find .changeset -name "*.md" -not -name "README.md" 2>/dev/null | head -1',
|
|
319
|
+
{
|
|
320
|
+
encoding: 'utf8',
|
|
321
|
+
stdio: 'pipe',
|
|
322
|
+
},
|
|
323
|
+
).trim();
|
|
324
|
+
|
|
325
|
+
if (changesetFiles) {
|
|
326
|
+
console.log(
|
|
327
|
+
`❌ Cannot merge a branch where version has not been bumped. Found changesets in ${sourceBranch}`,
|
|
328
|
+
);
|
|
329
|
+
process.exit(BuildResults.changeLogsExist);
|
|
330
|
+
}
|
|
331
|
+
} catch (error) {
|
|
332
|
+
if (error.message.includes('Cannot merge')) {
|
|
333
|
+
throw error;
|
|
334
|
+
}
|
|
335
|
+
// If find command fails (no .changeset directory), that's fine
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (verbose) console.log('✅ No uncommitted changesets found');
|
|
339
|
+
} finally {
|
|
340
|
+
// Return to original branch
|
|
341
|
+
execSync(`git checkout ${currentBranch}`, {
|
|
342
|
+
stdio: 'pipe',
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
272
347
|
export function setupMerge(program) {
|
|
273
348
|
program
|
|
274
349
|
.command('merge')
|
|
@@ -306,6 +381,11 @@ export function setupMerge(program) {
|
|
|
306
381
|
);
|
|
307
382
|
}
|
|
308
383
|
|
|
384
|
+
// Check for uncommitted changesets in source branch
|
|
385
|
+
if (!mergeFail && !mergeSuccess) {
|
|
386
|
+
checkForUncommittedChangesets(sourceBranch, verbose);
|
|
387
|
+
}
|
|
388
|
+
|
|
309
389
|
// 1. Create merge branch from target branch
|
|
310
390
|
const mergeBranchName = createMergeBranch(
|
|
311
391
|
sourceBranch,
|
|
@@ -327,7 +407,7 @@ export function setupMerge(program) {
|
|
|
327
407
|
console.log('✅ No changes to merge');
|
|
328
408
|
execSync(`git checkout ${targetBranch}`, { stdio: 'pipe' });
|
|
329
409
|
execSync(`git branch -d ${mergeBranchName}`, { stdio: 'pipe' });
|
|
330
|
-
process.exit(
|
|
410
|
+
process.exit(BuildResults.noChanges);
|
|
331
411
|
}
|
|
332
412
|
|
|
333
413
|
// 4. If conflicts, try auto-resolve
|
|
@@ -378,7 +458,7 @@ export function setupMerge(program) {
|
|
|
378
458
|
console.log(
|
|
379
459
|
`⚠️ Merge completed with unresolved conflicts on branch: ${mergeBranchName}`,
|
|
380
460
|
);
|
|
381
|
-
process.exit(
|
|
461
|
+
process.exit(BuildResults.unresolvedConflicts);
|
|
382
462
|
}
|
|
383
463
|
|
|
384
464
|
console.log(
|
|
@@ -420,9 +500,9 @@ export function setupMerge(program) {
|
|
|
420
500
|
});
|
|
421
501
|
}
|
|
422
502
|
if (buildPassed) {
|
|
423
|
-
process.exit(
|
|
503
|
+
process.exit(BuildResults.success);
|
|
424
504
|
} else {
|
|
425
|
-
process.exit(
|
|
505
|
+
process.exit(BuildResults.buildFailed);
|
|
426
506
|
}
|
|
427
507
|
} else {
|
|
428
508
|
// 6e) Build failed, dev mode, or prepare-only - create PR
|
package/src/micro/cli.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { Command } from 'commander';
|
|
4
|
+
//import { publishMicroservice } from './ms-publish.js';
|
|
5
|
+
|
|
6
|
+
import dotenv from 'dotenv';
|
|
7
|
+
import {
|
|
8
|
+
microserviceDeployTagVersion,
|
|
9
|
+
microserviceCopyImageToEnv,
|
|
10
|
+
microserviceBuildPushImage,
|
|
11
|
+
} from './ms-deploy.js';
|
|
12
|
+
|
|
13
|
+
dotenv.config({ path: ['.env.pipeline.local', 'config/.env.pipeline.local'] });
|
|
14
|
+
|
|
15
|
+
const program = new Command();
|
|
16
|
+
|
|
17
|
+
//const GIT_SHA = (await $`git log --pretty=format:'%h' -n 1`).stdout.trim();
|
|
18
|
+
|
|
19
|
+
program
|
|
20
|
+
.command('publish-microservice <serviceName>')
|
|
21
|
+
.description('Publish a microservice Docker image with version tags')
|
|
22
|
+
.option('-t, --tag <tag>', 'Version tag (defaults to BITBUCKET_TAG)')
|
|
23
|
+
.action(async (repoName, options) => {
|
|
24
|
+
await microserviceBuildPushImage(repoName, options);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
program
|
|
28
|
+
.command('deploy-microservice <repoName>')
|
|
29
|
+
.description('Deploy microservice end-to-end (build + publish + deploy)')
|
|
30
|
+
.option('-e, --env <environment>', 'Target environment', 'dev')
|
|
31
|
+
.option('-t, --tag <tag>', 'Version tag (defaults to BITBUCKET_TAG)')
|
|
32
|
+
.action(async (repoName, options) => {
|
|
33
|
+
await microserviceDeployTagVersion(repoName, options.env, options.tag);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
program
|
|
37
|
+
.command('promote-microservice <repoName>')
|
|
38
|
+
.description('Promote microservice image between environments')
|
|
39
|
+
.requiredOption('-f, --from <fromEnv>', 'Source environment')
|
|
40
|
+
.requiredOption('-t, --to <toEnv>', 'Target environment')
|
|
41
|
+
.requiredOption('-g, --tag <tag>', 'Image tag to promote')
|
|
42
|
+
.action(async (repoName, options) => {
|
|
43
|
+
await microserviceCopyImageToEnv(
|
|
44
|
+
repoName,
|
|
45
|
+
options.from,
|
|
46
|
+
options.to,
|
|
47
|
+
options.tag,
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
program.parse(process.argv);
|
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
import { $ } from '../next/logger.js';
|
|
2
|
+
//mport { publishMicroservice } from './ms-publish.js';
|
|
3
|
+
import { copy2 } from '../next/copy.js';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
/**
|
|
7
|
+
* Deploy microservice end-to-end: build, publish, and deploy
|
|
8
|
+
* @param {string} repoName - Name of microservice (e.g., 'swp', 'fidelity', 'esp')
|
|
9
|
+
* @param {string} environment - Target environment (dev, qa, uat, stage, prod)
|
|
10
|
+
* @param {string} tag - Version tag (optional, defaults to BITBUCKET_TAG)
|
|
11
|
+
*/
|
|
12
|
+
// export async function deployMicroserviceEndToEnd(
|
|
13
|
+
// repoName,
|
|
14
|
+
// environment = '',
|
|
15
|
+
// tag = null,
|
|
16
|
+
// ) {
|
|
17
|
+
// const serviceName = String(repoName || '')
|
|
18
|
+
// .split('-')
|
|
19
|
+
// .pop()
|
|
20
|
+
// .trim();
|
|
21
|
+
// const registryUrl = process.env.JF_REGISTRY_URL || '';
|
|
22
|
+
// const deployTag = tag || process.env.BITBUCKET_TAG || process.env.VERSION_TAG;
|
|
23
|
+
// if (!deployTag) {
|
|
24
|
+
// throw new Error(
|
|
25
|
+
// 'No tag provided. Set tag parameter or BITBUCKET_TAG/VERSION_TAG environment variable.',
|
|
26
|
+
// );
|
|
27
|
+
// }
|
|
28
|
+
// console.log(`Starting ${serviceName} microservice deployment...`);
|
|
29
|
+
// console.log(`Environment: ${environment}`);
|
|
30
|
+
// console.log(`Tag: ${deployTag}`);
|
|
31
|
+
// console.log('==========================================');
|
|
32
|
+
// try {
|
|
33
|
+
// // Step 1: Get Git SHA for image building
|
|
34
|
+
// const gitSha = (
|
|
35
|
+
// await $`git log ${deployTag} --pretty=format:'%h' -n 1`
|
|
36
|
+
// ).stdout.trim();
|
|
37
|
+
// console.log(`Git SHA: ${gitSha}`);
|
|
38
|
+
// // Step 2: Publish Docker image with version tags
|
|
39
|
+
// console.log('\nPublishing Docker image...');
|
|
40
|
+
// await publishMicroservice(serviceName, {
|
|
41
|
+
// tag: deployTag,
|
|
42
|
+
// });
|
|
43
|
+
// console.log('Docker image published successfully!');
|
|
44
|
+
// // Step 3: Deploy to GitOps repository
|
|
45
|
+
// console.log(`\nDeploying to ${environment} environment...`);
|
|
46
|
+
// await deployMicroservice(serviceName, serviceName, environment, deployTag);
|
|
47
|
+
// console.log(`Deployment to ${environment} completed successfully!`);
|
|
48
|
+
// console.log(
|
|
49
|
+
// `\n${serviceName} microservice deployment completed successfully!`,
|
|
50
|
+
// );
|
|
51
|
+
// console.log(
|
|
52
|
+
// `Image: ${registryUrl}/${repoName}-${environment}-docker-local/${serviceName}:${deployTag}`,
|
|
53
|
+
// );
|
|
54
|
+
// } catch (error) {
|
|
55
|
+
// console.error('\nDeployment failed:', error.message);
|
|
56
|
+
// throw error;
|
|
57
|
+
// }
|
|
58
|
+
// }
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Test Docker build and publish only (no deployment)
|
|
62
|
+
* @param {string} serviceName - Name of microservice
|
|
63
|
+
*/
|
|
64
|
+
// export async function testMicroservicePublish(serviceName) {
|
|
65
|
+
// console.log(`Starting ${serviceName} test publish...`);
|
|
66
|
+
// console.log('==========================================');
|
|
67
|
+
// try {
|
|
68
|
+
// const gitSha = (await $`git log --pretty=format:'%h' -n 1`).stdout.trim();
|
|
69
|
+
// console.log(`Git SHA: ${gitSha}`);
|
|
70
|
+
// console.log('\nTesting Docker build and publish...');
|
|
71
|
+
// await publishMicroservice(gitSha, {
|
|
72
|
+
// imageName: serviceName,
|
|
73
|
+
// });
|
|
74
|
+
// console.log('Test publish completed successfully!');
|
|
75
|
+
// } catch (error) {
|
|
76
|
+
// console.error('\nTest publish failed:', error.message);
|
|
77
|
+
// throw error;
|
|
78
|
+
// }
|
|
79
|
+
// }
|
|
80
|
+
|
|
81
|
+
export async function deployMicroservice(
|
|
82
|
+
appName,
|
|
83
|
+
helmPath,
|
|
84
|
+
environment = '',
|
|
85
|
+
tag = null,
|
|
86
|
+
) {
|
|
87
|
+
if (!appName || !helmPath) {
|
|
88
|
+
throw new Error(
|
|
89
|
+
'Missing required parameters. Usage: deployMicroservice(appName, helmPath, [environment], [tag])',
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let deployTag = tag;
|
|
94
|
+
if (!deployTag) {
|
|
95
|
+
deployTag =
|
|
96
|
+
process.env.VERSION_TAG ||
|
|
97
|
+
process.env.BITBUCKET_TAG ||
|
|
98
|
+
(process.env.BITBUCKET_BUILD_NUMBER
|
|
99
|
+
? `build-${process.env.BITBUCKET_BUILD_NUMBER}`
|
|
100
|
+
: null);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (!deployTag) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
'No tag specified and no default tag available from environment variables',
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const repoName = `gitops-${environment}`;
|
|
110
|
+
const filePath = `dp-svc/${helmPath}/${appName}.values.yaml`;
|
|
111
|
+
|
|
112
|
+
const gitopsBranch =
|
|
113
|
+
environment === 'dev' || environment === 'uat'
|
|
114
|
+
? process.env.GITOPS_BRANCH || 'master'
|
|
115
|
+
: process.env.GITOPS_BRANCH || 'main';
|
|
116
|
+
|
|
117
|
+
let repoUrl;
|
|
118
|
+
if (environment === 'dev' && process.env.GITOPS_REP_TOKEN) {
|
|
119
|
+
repoUrl = `https://x-token-auth:${process.env.GITOPS_REP_TOKEN}@bitbucket.org/dp-cloud/${repoName}.git`;
|
|
120
|
+
} else if (
|
|
121
|
+
process.env.BITBUCKET_USERNAME &&
|
|
122
|
+
process.env.BITBUCKET_APP_PASSWORD
|
|
123
|
+
) {
|
|
124
|
+
repoUrl = `https://${process.env.BITBUCKET_USERNAME}:${process.env.BITBUCKET_APP_PASSWORD}@bitbucket.org/dp-cloud/${repoName}.git`;
|
|
125
|
+
} else {
|
|
126
|
+
throw new Error(
|
|
127
|
+
'No authentication credentials found for GitOps repository',
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
console.log('=========================================');
|
|
132
|
+
console.log('Microservice Deployment Configuration');
|
|
133
|
+
console.log('=========================================');
|
|
134
|
+
console.log(`Application: ${appName}`);
|
|
135
|
+
console.log(`Environment: ${environment}`);
|
|
136
|
+
console.log(`Tag: ${deployTag}`);
|
|
137
|
+
console.log(`GitOps Repo: ${repoName}`);
|
|
138
|
+
console.log(`Values File: ${filePath}`);
|
|
139
|
+
console.log(`Branch: ${gitopsBranch}`);
|
|
140
|
+
console.log('=========================================');
|
|
141
|
+
|
|
142
|
+
try {
|
|
143
|
+
console.log('Cloning GitOps repository...');
|
|
144
|
+
await $`git clone --depth 1 ${repoUrl} ${repoName}`;
|
|
145
|
+
|
|
146
|
+
console.log('Checking if directory exists...');
|
|
147
|
+
await $`ls -la ${repoName}`;
|
|
148
|
+
|
|
149
|
+
console.log(`Changing to directory: ${repoName}`);
|
|
150
|
+
const originalDir = process.cwd();
|
|
151
|
+
process.chdir(repoName);
|
|
152
|
+
|
|
153
|
+
console.log('Verifying directory change...');
|
|
154
|
+
const newDir = process.cwd();
|
|
155
|
+
console.log(`Original: ${originalDir}`);
|
|
156
|
+
console.log(`New: ${newDir}`);
|
|
157
|
+
|
|
158
|
+
await $`cd ${repoName} && git config user.email "pipeline@bitbucket.org"`;
|
|
159
|
+
await $`cd ${repoName} && git config user.name "Bitbucket Pipeline"`;
|
|
160
|
+
|
|
161
|
+
console.log(`Attempting to checkout branch: ${gitopsBranch}`);
|
|
162
|
+
await $`cd ${repoName} && git checkout ${gitopsBranch}`;
|
|
163
|
+
|
|
164
|
+
const fullFilePath = path.resolve(filePath);
|
|
165
|
+
console.log(`Looking for file at: ${fullFilePath}`);
|
|
166
|
+
if (!fs.existsSync(fullFilePath)) {
|
|
167
|
+
throw new Error(
|
|
168
|
+
`Values file ${filePath} does not exist in ${repoName} on branch ${gitopsBranch}`,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
console.log(`Updating image tag to ${deployTag}...`);
|
|
173
|
+
|
|
174
|
+
let fileContent = fs.readFileSync(fullFilePath, 'utf8');
|
|
175
|
+
|
|
176
|
+
const tagRegex = /^ {2}tag: .*/gm;
|
|
177
|
+
const newContent = fileContent.replace(tagRegex, ` tag: ${deployTag}`);
|
|
178
|
+
|
|
179
|
+
if (fileContent === newContent) {
|
|
180
|
+
console.log(`No changes detected - tag is already ${deployTag}`);
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
fs.writeFileSync(fullFilePath, newContent, 'utf8');
|
|
185
|
+
|
|
186
|
+
console.log('Changes to be committed:');
|
|
187
|
+
await $`cd ${repoName} && git diff ${filePath}`;
|
|
188
|
+
|
|
189
|
+
await $`cd ${repoName} && git add ${filePath}`;
|
|
190
|
+
await $`cd ${repoName} && git commit -m "Update ${appName} image tag to ${deployTag} in ${environment} environment"`;
|
|
191
|
+
|
|
192
|
+
console.log('Pushing changes to GitOps repository...');
|
|
193
|
+
await $`cd ${repoName} && git push origin ${gitopsBranch}`;
|
|
194
|
+
|
|
195
|
+
console.log('=========================================');
|
|
196
|
+
console.log(
|
|
197
|
+
`✅ Successfully deployed ${appName}:${deployTag} to ${environment}`,
|
|
198
|
+
);
|
|
199
|
+
console.log('=========================================');
|
|
200
|
+
} catch (error) {
|
|
201
|
+
console.error('Error during microservice deployment:', error.message);
|
|
202
|
+
throw error;
|
|
203
|
+
} finally {
|
|
204
|
+
try {
|
|
205
|
+
process.chdir('..');
|
|
206
|
+
await $`rm -rf ${repoName}`;
|
|
207
|
+
} catch (cleanupError) {
|
|
208
|
+
console.warn(
|
|
209
|
+
'Warning: Failed to clean up temporary directory:',
|
|
210
|
+
cleanupError.message,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
export default deployMicroservice;
|
|
217
|
+
|
|
218
|
+
export async function microserviceDeployTagVersion(
|
|
219
|
+
repoName,
|
|
220
|
+
environment = '',
|
|
221
|
+
tag = null,
|
|
222
|
+
) {
|
|
223
|
+
const serviceName = String(repoName || '')
|
|
224
|
+
.split('-')
|
|
225
|
+
.pop()
|
|
226
|
+
.trim();
|
|
227
|
+
console.log(`Starting ${serviceName} test deployment...`);
|
|
228
|
+
console.log(`Environment: ${environment}`);
|
|
229
|
+
console.log(`Tag: ${tag}`);
|
|
230
|
+
console.log('==========================================');
|
|
231
|
+
try {
|
|
232
|
+
console.log(`\nTesting deployment to ${environment}...`);
|
|
233
|
+
await deployMicroservice(serviceName, serviceName, environment, tag);
|
|
234
|
+
console.log(`Test deployment to ${environment} completed successfully!`);
|
|
235
|
+
} catch (error) {
|
|
236
|
+
console.error('\nTest deployment failed:', error.message);
|
|
237
|
+
throw error;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export async function microserviceCopyImageToEnv(
|
|
242
|
+
repoName,
|
|
243
|
+
fromEnv,
|
|
244
|
+
toEnv,
|
|
245
|
+
tag,
|
|
246
|
+
) {
|
|
247
|
+
const serviceName = String(repoName || '')
|
|
248
|
+
.split('-')
|
|
249
|
+
.pop()
|
|
250
|
+
.trim();
|
|
251
|
+
console.log(`Starting ${serviceName} image promotion...`);
|
|
252
|
+
console.log(`From: ${fromEnv} → To: ${toEnv}`);
|
|
253
|
+
console.log(`Tag: ${tag}`);
|
|
254
|
+
console.log('==========================================');
|
|
255
|
+
try {
|
|
256
|
+
// Step 1: Copy Docker image between registries
|
|
257
|
+
console.log('\nCopying Docker image...');
|
|
258
|
+
const repositoryName = `${repoName}`;
|
|
259
|
+
await copy2(repositoryName, fromEnv, toEnv, tag, serviceName);
|
|
260
|
+
console.log('Docker image copied successfully!');
|
|
261
|
+
} catch (error) {
|
|
262
|
+
console.error('\nImage promotion failed:', error.message);
|
|
263
|
+
throw error;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export async function microserviceBuildPushImage(repoName, options = {}) {
|
|
268
|
+
// Use environment variables as defaults, but allow overrides
|
|
269
|
+
|
|
270
|
+
const serviceName = String(repoName || '')
|
|
271
|
+
.split('-')
|
|
272
|
+
.pop()
|
|
273
|
+
.trim();
|
|
274
|
+
const config = {
|
|
275
|
+
imageName: serviceName,
|
|
276
|
+
registryUrl: options.registryUrl || process.env.JF_REGISTRY_URL,
|
|
277
|
+
repoName: repoName,
|
|
278
|
+
username: options.username || process.env.JF_REGISTRY_DEV_USERNAME,
|
|
279
|
+
password: options.password || process.env.JF_REGISTRY_DEV_PASSWORD,
|
|
280
|
+
buildNumber: options.buildNumber || process.env.BITBUCKET_BUILD_NUMBER,
|
|
281
|
+
tag: options.tag || process.env.VERSION_TAG,
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
// Validate required fields
|
|
285
|
+
if (!config.imageName) {
|
|
286
|
+
throw new Error(
|
|
287
|
+
'Image name is required. Set IMAGE_NAME or APP_NAME environment variable.',
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
console.log('=== Docker Image Publishing Configuration ===');
|
|
291
|
+
console.log(`Version Tag: ${config.tag || 'none'}`);
|
|
292
|
+
console.log(`Registry: ${config.registryUrl}/${repoName}`);
|
|
293
|
+
console.log('==========================================');
|
|
294
|
+
|
|
295
|
+
const registryPath = `${config.registryUrl}/${repoName}-dev-docker-local`;
|
|
296
|
+
// Docker login
|
|
297
|
+
await $`echo ${config.password} | docker login --username ${config.username} --password-stdin ${config.registryUrl}`;
|
|
298
|
+
|
|
299
|
+
try {
|
|
300
|
+
// Build the image if it doesn't exist locally
|
|
301
|
+
const TAG_IMAGE_NAME = `${registryPath}/${serviceName}:${options.tag}`;
|
|
302
|
+
const imageExists = await checkLocalImage(TAG_IMAGE_NAME);
|
|
303
|
+
let SHA_IMAGE_NAME = '';
|
|
304
|
+
if (!imageExists) {
|
|
305
|
+
const repoUrl = `https://${process.env.BITBUCKET_USERNAME}:${process.env.BITBUCKET_APP_PASSWORD}@bitbucket.org/dp-cloud/${repoName}.git`;
|
|
306
|
+
await $`git clone --branch ${options.tag} --depth 1 ${repoUrl} ${repoName}`;
|
|
307
|
+
|
|
308
|
+
console.log('Checking if directory exists...');
|
|
309
|
+
await $`ls -la ${repoName}`;
|
|
310
|
+
|
|
311
|
+
console.log(`Changing to directory: ${repoName}`);
|
|
312
|
+
process.chdir(repoName);
|
|
313
|
+
|
|
314
|
+
await $`cd ${repoName} && git config user.email "pipeline@bitbucket.org"`;
|
|
315
|
+
await $`cd ${repoName} && git config user.name "Bitbucket Pipeline"`;
|
|
316
|
+
|
|
317
|
+
console.log(`Attempting to checkout branch: ${options.tag}`);
|
|
318
|
+
|
|
319
|
+
const gitSha = (
|
|
320
|
+
await $`cd ${repoName} && git log ${options.tag} --pretty=format:'%h' -n 1 `
|
|
321
|
+
).stdout.trim();
|
|
322
|
+
SHA_IMAGE_NAME = `${config.imageName}:sha-${gitSha}`;
|
|
323
|
+
|
|
324
|
+
console.log(`Building Docker image ${SHA_IMAGE_NAME}...`);
|
|
325
|
+
await $`cd ${repoName} && docker build -t ${SHA_IMAGE_NAME} .`;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
// Array to track all tags we're pushing
|
|
329
|
+
const tagsToPush = [];
|
|
330
|
+
|
|
331
|
+
// 1. Push SHA tag
|
|
332
|
+
const shaTag = `${registryPath}/${SHA_IMAGE_NAME}`;
|
|
333
|
+
console.log(`Tagging and pushing SHA: ${shaTag}`);
|
|
334
|
+
await $`docker tag ${SHA_IMAGE_NAME} ${shaTag}`;
|
|
335
|
+
await $`docker push ${shaTag}`;
|
|
336
|
+
tagsToPush.push(SHA_IMAGE_NAME);
|
|
337
|
+
|
|
338
|
+
// 2. Push build number tag
|
|
339
|
+
if (config.buildNumber) {
|
|
340
|
+
const BUILD_IMAGE_NAME = `${config.imageName}:${config.buildNumber}`;
|
|
341
|
+
const buildTag = `${registryPath}/${BUILD_IMAGE_NAME}`;
|
|
342
|
+
console.log(`Tagging and pushing build: ${buildTag}`);
|
|
343
|
+
await $`docker tag ${SHA_IMAGE_NAME} ${buildTag}`;
|
|
344
|
+
await $`docker push ${buildTag}`;
|
|
345
|
+
tagsToPush.push(BUILD_IMAGE_NAME);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// 3. Push version tag (this is what ArgoCD needs!)
|
|
349
|
+
if (config.tag) {
|
|
350
|
+
// Handle various tag formats
|
|
351
|
+
let version = config.tag;
|
|
352
|
+
|
|
353
|
+
// If tag contains @, extract version after it
|
|
354
|
+
if (version.includes('@')) {
|
|
355
|
+
version = 'v' + version.split('@')[1];
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Ensure version starts with 'v' if it's a semver without it
|
|
359
|
+
if (/^\d+\.\d+\.\d+/.test(version) && !version.startsWith('v')) {
|
|
360
|
+
version = 'v' + version;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const VERSION_IMAGE_NAME = `${config.imageName}:${version}`;
|
|
364
|
+
const versionTag = `${registryPath}/${VERSION_IMAGE_NAME}`;
|
|
365
|
+
|
|
366
|
+
console.log(`Tagging and pushing version: ${versionTag}`);
|
|
367
|
+
await $`docker tag ${SHA_IMAGE_NAME} ${versionTag}`;
|
|
368
|
+
await $`docker push ${versionTag}`;
|
|
369
|
+
tagsToPush.push(VERSION_IMAGE_NAME);
|
|
370
|
+
|
|
371
|
+
// Also push 'latest' tag when we have a version
|
|
372
|
+
const LATEST_IMAGE_NAME = `${config.imageName}:latest`;
|
|
373
|
+
const latestTag = `${registryPath}/${LATEST_IMAGE_NAME}`;
|
|
374
|
+
console.log(`Tagging and pushing latest: ${latestTag}`);
|
|
375
|
+
await $`docker tag ${SHA_IMAGE_NAME} ${latestTag}`;
|
|
376
|
+
await $`docker push ${latestTag}`;
|
|
377
|
+
tagsToPush.push(LATEST_IMAGE_NAME);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// 4. Check for git tags at current commit
|
|
381
|
+
try {
|
|
382
|
+
const gitTags = (await $`git tag --points-at HEAD`).stdout
|
|
383
|
+
.trim()
|
|
384
|
+
.split('\n')
|
|
385
|
+
.filter(Boolean);
|
|
386
|
+
|
|
387
|
+
if (gitTags.length > 0) {
|
|
388
|
+
console.log(`Found git tags at HEAD: ${gitTags.join(', ')}`);
|
|
389
|
+
|
|
390
|
+
for (const gitTag of gitTags) {
|
|
391
|
+
// Skip if we already handled this tag above
|
|
392
|
+
if (gitTag === config.tag) continue;
|
|
393
|
+
|
|
394
|
+
let version = gitTag;
|
|
395
|
+
if (version.includes('@')) {
|
|
396
|
+
version = 'v' + version.split('@')[1];
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const TAG_IMAGE_NAME = `${config.imageName}:${version}`;
|
|
400
|
+
const gitTagPath = `${registryPath}/${TAG_IMAGE_NAME}`;
|
|
401
|
+
|
|
402
|
+
console.log(`Tagging and pushing git tag: ${gitTagPath}`);
|
|
403
|
+
await $`docker tag ${SHA_IMAGE_NAME} ${gitTagPath}`;
|
|
404
|
+
await $`docker push ${gitTagPath}`;
|
|
405
|
+
tagsToPush.push(TAG_IMAGE_NAME);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
} catch (e) {
|
|
409
|
+
console.log('No git tags found at HEAD or error checking tags', e);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
console.log('\n✅ Docker image published successfully!');
|
|
413
|
+
console.log('=== Summary of pushed tags ===');
|
|
414
|
+
tagsToPush.forEach((tag) => console.log(` ✓ ${tag}`));
|
|
415
|
+
console.log('==============================\n');
|
|
416
|
+
|
|
417
|
+
// Verify the version tag if we expect one
|
|
418
|
+
if (config.tag) {
|
|
419
|
+
const version = config.tag.includes('@')
|
|
420
|
+
? 'v' + config.tag.split('@')[1]
|
|
421
|
+
: config.tag;
|
|
422
|
+
const expectedTag = `${registryPath}/${config.imageName}:${version}`;
|
|
423
|
+
console.log(`Verifying version tag exists: ${expectedTag}`);
|
|
424
|
+
try {
|
|
425
|
+
await $`docker manifest inspect ${expectedTag}`;
|
|
426
|
+
console.log('✅ Version tag verified in registry!');
|
|
427
|
+
} catch (e) {
|
|
428
|
+
console.error('❌ WARNING: Could not verify version tag in registry!');
|
|
429
|
+
console.error('ArgoCD may fail to pull this image.', e);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
} catch (error) {
|
|
433
|
+
console.error('Error during image publishing:', error);
|
|
434
|
+
throw error;
|
|
435
|
+
} finally {
|
|
436
|
+
// Always logout
|
|
437
|
+
await $`docker logout ${config.registryUrl}`;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
async function checkLocalImage(imageName) {
|
|
442
|
+
try {
|
|
443
|
+
$.verbose = false;
|
|
444
|
+
await $`docker image inspect ${imageName}`;
|
|
445
|
+
return true;
|
|
446
|
+
} catch {
|
|
447
|
+
return false;
|
|
448
|
+
} finally {
|
|
449
|
+
$.verbose = true;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
//import { $ } from '../next/logger.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Publish Docker images for microservices with flexible naming
|
|
5
|
+
* @param {string} gitSha - Git commit SHA
|
|
6
|
+
* @param {Object} options - Configuration options
|
|
7
|
+
* @param {string} options.imageName - Docker image name (e.g., 'swp')
|
|
8
|
+
* @param {string} options.registryUrl - JFrog registry URL
|
|
9
|
+
* @param {string} options.repoName - Repository name in JFrog
|
|
10
|
+
* @param {string} options.username - JFrog username
|
|
11
|
+
* @param {string} options.password - JFrog password
|
|
12
|
+
* @param {string} options.buildNumber - Build number
|
|
13
|
+
*/
|
|
14
|
+
// export async function publishMicroservice(serviceName, options = {}) {
|
|
15
|
+
// // Use environment variables as defaults, but allow overrides
|
|
16
|
+
|
|
17
|
+
// const gitSha = (await $`git log --pretty=format:'%h' -n 1 `).stdout.trim();
|
|
18
|
+
|
|
19
|
+
// const config = {
|
|
20
|
+
// imageName: serviceName,
|
|
21
|
+
// registryUrl: options.registryUrl || process.env.JF_REGISTRY_URL,
|
|
22
|
+
// repoName: options.repoName || process.env.JF_REGISTRY_REPO_NAME,
|
|
23
|
+
// username: options.username || process.env.JF_REGISTRY_USER_DEV,
|
|
24
|
+
// password: options.password || process.env.JF_REGISTRY_PASSWORD_DEV,
|
|
25
|
+
// buildNumber: options.buildNumber || process.env.BITBUCKET_BUILD_NUMBER,
|
|
26
|
+
// tag: options.tag || process.env.VERSION_TAG,
|
|
27
|
+
// };
|
|
28
|
+
|
|
29
|
+
// // Validate required fields
|
|
30
|
+
// if (!config.imageName) {
|
|
31
|
+
// throw new Error(
|
|
32
|
+
// 'Image name is required. Set IMAGE_NAME or APP_NAME environment variable.',
|
|
33
|
+
// );
|
|
34
|
+
// }
|
|
35
|
+
|
|
36
|
+
// console.log('=== Docker Image Publishing Configuration ===');
|
|
37
|
+
// console.log(`Image Name: ${config.imageName}`);
|
|
38
|
+
// console.log(`Git SHA: ${gitSha}`);
|
|
39
|
+
// console.log(`Build Number: ${config.buildNumber}`);
|
|
40
|
+
// console.log(`Version Tag: ${config.tag || 'none'}`);
|
|
41
|
+
// console.log(`Registry: ${config.registryUrl}/${config.repoName}`);
|
|
42
|
+
// console.log('==========================================');
|
|
43
|
+
|
|
44
|
+
// const registryPath = `${config.registryUrl}/${config.repoName}`;
|
|
45
|
+
// const SHA_IMAGE_NAME = `${config.imageName}:sha-${gitSha}`;
|
|
46
|
+
|
|
47
|
+
// // Docker login
|
|
48
|
+
// await $`echo ${config.password} | docker login --username ${config.username} --password-stdin ${config.registryUrl}`;
|
|
49
|
+
|
|
50
|
+
// try {
|
|
51
|
+
// // Build the image if it doesn't exist locally
|
|
52
|
+
// const imageExists = await checkLocalImage(SHA_IMAGE_NAME);
|
|
53
|
+
// if (!imageExists) {
|
|
54
|
+
// console.log(`Building Docker image ${SHA_IMAGE_NAME}...`);
|
|
55
|
+
// await $`docker build -t ${SHA_IMAGE_NAME} .`;
|
|
56
|
+
// }
|
|
57
|
+
|
|
58
|
+
// // Array to track all tags we're pushing
|
|
59
|
+
// const tagsToPush = [];
|
|
60
|
+
|
|
61
|
+
// // 1. Push SHA tag
|
|
62
|
+
// const shaTag = `${registryPath}/${SHA_IMAGE_NAME}`;
|
|
63
|
+
// console.log(`Tagging and pushing SHA: ${shaTag}`);
|
|
64
|
+
// await $`docker tag ${SHA_IMAGE_NAME} ${shaTag}`;
|
|
65
|
+
// await $`docker push ${shaTag}`;
|
|
66
|
+
// tagsToPush.push(SHA_IMAGE_NAME);
|
|
67
|
+
|
|
68
|
+
// // 2. Push build number tag
|
|
69
|
+
// if (config.buildNumber) {
|
|
70
|
+
// const BUILD_IMAGE_NAME = `${config.imageName}:build-${config.buildNumber}`;
|
|
71
|
+
// const buildTag = `${registryPath}/${BUILD_IMAGE_NAME}`;
|
|
72
|
+
// console.log(`Tagging and pushing build: ${buildTag}`);
|
|
73
|
+
// await $`docker tag ${SHA_IMAGE_NAME} ${buildTag}`;
|
|
74
|
+
// await $`docker push ${buildTag}`;
|
|
75
|
+
// tagsToPush.push(BUILD_IMAGE_NAME);
|
|
76
|
+
// }
|
|
77
|
+
|
|
78
|
+
// // 3. Push version tag (this is what ArgoCD needs!)
|
|
79
|
+
// if (config.tag) {
|
|
80
|
+
// // Handle various tag formats
|
|
81
|
+
// let version = config.tag;
|
|
82
|
+
|
|
83
|
+
// // If tag contains @, extract version after it
|
|
84
|
+
// if (version.includes('@')) {
|
|
85
|
+
// version = 'v' + version.split('@')[1];
|
|
86
|
+
// }
|
|
87
|
+
|
|
88
|
+
// // Ensure version starts with 'v' if it's a semver without it
|
|
89
|
+
// if (/^\d+\.\d+\.\d+/.test(version) && !version.startsWith('v')) {
|
|
90
|
+
// version = 'v' + version;
|
|
91
|
+
// }
|
|
92
|
+
|
|
93
|
+
// const VERSION_IMAGE_NAME = `${config.imageName}:${version}`;
|
|
94
|
+
// const versionTag = `${registryPath}/${VERSION_IMAGE_NAME}`;
|
|
95
|
+
|
|
96
|
+
// console.log(`Tagging and pushing version: ${versionTag}`);
|
|
97
|
+
// await $`docker tag ${SHA_IMAGE_NAME} ${versionTag}`;
|
|
98
|
+
// await $`docker push ${versionTag}`;
|
|
99
|
+
// tagsToPush.push(VERSION_IMAGE_NAME);
|
|
100
|
+
|
|
101
|
+
// // Also push 'latest' tag when we have a version
|
|
102
|
+
// const LATEST_IMAGE_NAME = `${config.imageName}:latest`;
|
|
103
|
+
// const latestTag = `${registryPath}/${LATEST_IMAGE_NAME}`;
|
|
104
|
+
// console.log(`Tagging and pushing latest: ${latestTag}`);
|
|
105
|
+
// await $`docker tag ${SHA_IMAGE_NAME} ${latestTag}`;
|
|
106
|
+
// await $`docker push ${latestTag}`;
|
|
107
|
+
// tagsToPush.push(LATEST_IMAGE_NAME);
|
|
108
|
+
// }
|
|
109
|
+
|
|
110
|
+
// // 4. Check for git tags at current commit
|
|
111
|
+
// try {
|
|
112
|
+
// const gitTags = (await $`git tag --points-at HEAD`).stdout
|
|
113
|
+
// .trim()
|
|
114
|
+
// .split('\n')
|
|
115
|
+
// .filter(Boolean);
|
|
116
|
+
|
|
117
|
+
// if (gitTags.length > 0) {
|
|
118
|
+
// console.log(`Found git tags at HEAD: ${gitTags.join(', ')}`);
|
|
119
|
+
|
|
120
|
+
// for (const gitTag of gitTags) {
|
|
121
|
+
// // Skip if we already handled this tag above
|
|
122
|
+
// if (gitTag === config.tag) continue;
|
|
123
|
+
|
|
124
|
+
// let version = gitTag;
|
|
125
|
+
// if (version.includes('@')) {
|
|
126
|
+
// version = 'v' + version.split('@')[1];
|
|
127
|
+
// }
|
|
128
|
+
|
|
129
|
+
// const TAG_IMAGE_NAME = `${config.imageName}:${version}`;
|
|
130
|
+
// const gitTagPath = `${registryPath}/${TAG_IMAGE_NAME}`;
|
|
131
|
+
|
|
132
|
+
// console.log(`Tagging and pushing git tag: ${gitTagPath}`);
|
|
133
|
+
// await $`docker tag ${SHA_IMAGE_NAME} ${gitTagPath}`;
|
|
134
|
+
// await $`docker push ${gitTagPath}`;
|
|
135
|
+
// tagsToPush.push(TAG_IMAGE_NAME);
|
|
136
|
+
// }
|
|
137
|
+
// }
|
|
138
|
+
// } catch (e) {
|
|
139
|
+
// console.log('No git tags found at HEAD or error checking tags', e);
|
|
140
|
+
// }
|
|
141
|
+
|
|
142
|
+
// console.log('\n✅ Docker image published successfully!');
|
|
143
|
+
// console.log('=== Summary of pushed tags ===');
|
|
144
|
+
// tagsToPush.forEach((tag) => console.log(` ✓ ${tag}`));
|
|
145
|
+
// console.log('==============================\n');
|
|
146
|
+
|
|
147
|
+
// // Verify the version tag if we expect one
|
|
148
|
+
// if (config.tag) {
|
|
149
|
+
// const version = config.tag.includes('@')
|
|
150
|
+
// ? 'v' + config.tag.split('@')[1]
|
|
151
|
+
// : config.tag;
|
|
152
|
+
// const expectedTag = `${registryPath}/${config.imageName}:${version}`;
|
|
153
|
+
// console.log(`Verifying version tag exists: ${expectedTag}`);
|
|
154
|
+
// try {
|
|
155
|
+
// await $`docker manifest inspect ${expectedTag}`;
|
|
156
|
+
// console.log('✅ Version tag verified in registry!');
|
|
157
|
+
// } catch (e) {
|
|
158
|
+
// console.error('❌ WARNING: Could not verify version tag in registry!');
|
|
159
|
+
// console.error('ArgoCD may fail to pull this image.', e);
|
|
160
|
+
// }
|
|
161
|
+
// }
|
|
162
|
+
// } catch (error) {
|
|
163
|
+
// console.error('Error during image publishing:', error);
|
|
164
|
+
// throw error;
|
|
165
|
+
// } finally {
|
|
166
|
+
// // Always logout
|
|
167
|
+
// await $`docker logout ${config.registryUrl}`;
|
|
168
|
+
// }
|
|
169
|
+
// }
|
|
170
|
+
|
|
171
|
+
// async function checkLocalImage(imageName) {
|
|
172
|
+
// try {
|
|
173
|
+
// $.verbose = false;
|
|
174
|
+
// await $`docker image inspect ${imageName}`;
|
|
175
|
+
// return true;
|
|
176
|
+
// } catch {
|
|
177
|
+
// return false;
|
|
178
|
+
// } finally {
|
|
179
|
+
// $.verbose = true;
|
|
180
|
+
// }
|
|
181
|
+
// }
|
|
182
|
+
|
|
183
|
+
// // Backward compatibility wrapper
|
|
184
|
+
// export async function publishWithDefaults(gitSha) {
|
|
185
|
+
// return publishMicroservice(gitSha);
|
|
186
|
+
// }
|
package/src/next/cli.js
CHANGED
|
@@ -4,7 +4,6 @@ import { Command } from 'commander';
|
|
|
4
4
|
import { pull } from './pull.js';
|
|
5
5
|
import { sonar } from './sonar.js';
|
|
6
6
|
import { publish } from './publish.js';
|
|
7
|
-
import { publishMicroservice } from '../micro/publish-ms.js';
|
|
8
7
|
import {
|
|
9
8
|
deployAppWithTag,
|
|
10
9
|
deployFromSite,
|
|
@@ -12,7 +11,7 @@ import {
|
|
|
12
11
|
getDefaultDeploySite,
|
|
13
12
|
getDeployTag,
|
|
14
13
|
} from './deploy.js';
|
|
15
|
-
import {
|
|
14
|
+
import { copy2 } from './copy.js';
|
|
16
15
|
import { vaultAddSiteData } from './vault.js';
|
|
17
16
|
import { addApacheSite } from './apache.js';
|
|
18
17
|
import { haproxySync } from './haproxy.js';
|
|
@@ -83,14 +82,6 @@ program
|
|
|
83
82
|
await publish(GIT_SHA);
|
|
84
83
|
});
|
|
85
84
|
|
|
86
|
-
// Add the new publish-microservice command
|
|
87
|
-
program
|
|
88
|
-
.command('publish-microservice')
|
|
89
|
-
.description('Publish a microservice Docker image with version tags')
|
|
90
|
-
.action(async () => {
|
|
91
|
-
await publishMicroservice(GIT_SHA);
|
|
92
|
-
});
|
|
93
|
-
|
|
94
85
|
program
|
|
95
86
|
.command('deploy-from-site')
|
|
96
87
|
.description('Deploy an application for a specific site')
|
|
@@ -1,153 +0,0 @@
|
|
|
1
|
-
import { $ } from '../next/logger.js';
|
|
2
|
-
import fs from 'fs';
|
|
3
|
-
import path from 'path';
|
|
4
|
-
import axios from 'axios';
|
|
5
|
-
|
|
6
|
-
export async function deployMicroservice(
|
|
7
|
-
appName,
|
|
8
|
-
helmPath,
|
|
9
|
-
environment = 'dev',
|
|
10
|
-
tag = null,
|
|
11
|
-
) {
|
|
12
|
-
if (!appName || !helmPath) {
|
|
13
|
-
throw new Error(
|
|
14
|
-
'Missing required parameters. Usage: deployMicroservice(appName, helmPath, [environment], [tag])',
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
let deployTag = tag;
|
|
19
|
-
if (!deployTag) {
|
|
20
|
-
deployTag =
|
|
21
|
-
process.env.VERSION_TAG ||
|
|
22
|
-
process.env.BITBUCKET_TAG ||
|
|
23
|
-
(process.env.BITBUCKET_BUILD_NUMBER
|
|
24
|
-
? `build-${process.env.BITBUCKET_BUILD_NUMBER}`
|
|
25
|
-
: null);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
if (!deployTag) {
|
|
29
|
-
throw new Error(
|
|
30
|
-
'No tag specified and no default tag available from environment variables',
|
|
31
|
-
);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const repoName = `gitops-${environment}`;
|
|
35
|
-
const filePath = `dp/${helmPath}/${appName}.values.yaml`;
|
|
36
|
-
|
|
37
|
-
const gitopsBranch =
|
|
38
|
-
environment === 'dev' || environment === 'uat'
|
|
39
|
-
? process.env.GITOPS_BRANCH || 'master'
|
|
40
|
-
: process.env.GITOPS_BRANCH || 'main';
|
|
41
|
-
|
|
42
|
-
let repoUrl;
|
|
43
|
-
if (environment === 'dev' && process.env.GITOPS_REP_TOKEN) {
|
|
44
|
-
repoUrl = `https://x-token-auth:${process.env.GITOPS_REP_TOKEN}@bitbucket.org/dp-cloud/${repoName}.git`;
|
|
45
|
-
} else if (
|
|
46
|
-
process.env.BITBUCKET_USERNAME &&
|
|
47
|
-
process.env.BITBUCKET_APP_PASSWORD
|
|
48
|
-
) {
|
|
49
|
-
repoUrl = `https://${process.env.BITBUCKET_USERNAME}:${process.env.BITBUCKET_APP_PASSWORD}@bitbucket.org/dp-cloud/${repoName}.git`;
|
|
50
|
-
} else {
|
|
51
|
-
throw new Error(
|
|
52
|
-
'No authentication credentials found for GitOps repository',
|
|
53
|
-
);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
console.log('=========================================');
|
|
57
|
-
console.log('Microservice Deployment Configuration');
|
|
58
|
-
console.log('=========================================');
|
|
59
|
-
console.log(`Application: ${appName}`);
|
|
60
|
-
console.log(`Environment: ${environment}`);
|
|
61
|
-
console.log(`Tag: ${deployTag}`);
|
|
62
|
-
console.log(`GitOps Repo: ${repoName}`);
|
|
63
|
-
console.log(`Values File: ${filePath}`);
|
|
64
|
-
console.log(`Branch: ${gitopsBranch}`);
|
|
65
|
-
console.log('=========================================');
|
|
66
|
-
|
|
67
|
-
try {
|
|
68
|
-
console.log('Cloning GitOps repository...');
|
|
69
|
-
await $`git clone --depth 1 ${repoUrl} ${repoName}`;
|
|
70
|
-
|
|
71
|
-
console.log('Checking if directory exists...');
|
|
72
|
-
await $`ls -la ${repoName}`;
|
|
73
|
-
|
|
74
|
-
console.log(`Changing to directory: ${repoName}`);
|
|
75
|
-
const originalDir = process.cwd();
|
|
76
|
-
process.chdir(repoName);
|
|
77
|
-
|
|
78
|
-
console.log('Verifying directory change...');
|
|
79
|
-
const newDir = process.cwd();
|
|
80
|
-
console.log(`Original: ${originalDir}`);
|
|
81
|
-
console.log(`New: ${newDir}`);
|
|
82
|
-
|
|
83
|
-
await $`cd ${repoName} && git config user.email "pipeline@bitbucket.org"`;
|
|
84
|
-
|
|
85
|
-
if (process.env.BITBUCKET_STEP_TRIGGERER_UUID) {
|
|
86
|
-
try {
|
|
87
|
-
const response = await axios.get(
|
|
88
|
-
`https://api.bitbucket.org/2.0/users/${process.env.BITBUCKET_STEP_TRIGGERER_UUID}`,
|
|
89
|
-
);
|
|
90
|
-
const triggererUsername = response.data.display_name;
|
|
91
|
-
console.log(`📌 Run triggered by: ${triggererUsername}`);
|
|
92
|
-
await $`cd ${repoName} && git config user.name ${triggererUsername}`;
|
|
93
|
-
} catch (error) {
|
|
94
|
-
console.warn('Failed to fetch Bitbucket user info:', error.message);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
console.log(`Attempting to checkout branch: ${gitopsBranch}`);
|
|
99
|
-
await $`cd ${repoName} && git checkout ${gitopsBranch}`;
|
|
100
|
-
|
|
101
|
-
const fullFilePath = path.resolve(filePath);
|
|
102
|
-
console.log(`Looking for file at: ${fullFilePath}`);
|
|
103
|
-
if (!fs.existsSync(fullFilePath)) {
|
|
104
|
-
throw new Error(
|
|
105
|
-
`Values file ${filePath} does not exist in ${repoName} on branch ${gitopsBranch}`,
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
console.log(`Updating image tag to ${deployTag}...`);
|
|
110
|
-
|
|
111
|
-
let fileContent = fs.readFileSync(fullFilePath, 'utf8');
|
|
112
|
-
|
|
113
|
-
const tagRegex = /^ {2}tag: .*/gm;
|
|
114
|
-
const newContent = fileContent.replace(tagRegex, ` tag: ${deployTag}`);
|
|
115
|
-
|
|
116
|
-
if (fileContent === newContent) {
|
|
117
|
-
console.log(`No changes detected - tag is already ${deployTag}`);
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
fs.writeFileSync(fullFilePath, newContent, 'utf8');
|
|
122
|
-
|
|
123
|
-
console.log('Changes to be committed:');
|
|
124
|
-
await $`cd ${repoName} && git diff ${filePath}`;
|
|
125
|
-
|
|
126
|
-
await $`cd ${repoName} && git add ${filePath}`;
|
|
127
|
-
await $`cd ${repoName} && git commit -m "Update ${appName} image tag to ${deployTag} in ${environment} environment"`;
|
|
128
|
-
|
|
129
|
-
console.log('Pushing changes to GitOps repository...');
|
|
130
|
-
await $`cd ${repoName} && git push origin ${gitopsBranch}`;
|
|
131
|
-
|
|
132
|
-
console.log('=========================================');
|
|
133
|
-
console.log(
|
|
134
|
-
`✅ Successfully deployed ${appName}:${deployTag} to ${environment}`,
|
|
135
|
-
);
|
|
136
|
-
console.log('=========================================');
|
|
137
|
-
} catch (error) {
|
|
138
|
-
console.error('Error during microservice deployment:', error.message);
|
|
139
|
-
throw error;
|
|
140
|
-
} finally {
|
|
141
|
-
try {
|
|
142
|
-
process.chdir('..');
|
|
143
|
-
await $`rm -rf ${repoName}`;
|
|
144
|
-
} catch (cleanupError) {
|
|
145
|
-
console.warn(
|
|
146
|
-
'Warning: Failed to clean up temporary directory:',
|
|
147
|
-
cleanupError.message,
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
export default deployMicroservice;
|
package/src/micro/publish-ms.js
DELETED
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
import { $ } from '../next/logger.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Publish Docker images for microservices with flexible naming
|
|
5
|
-
* @param {string} gitSha - Git commit SHA
|
|
6
|
-
* @param {Object} options - Configuration options
|
|
7
|
-
* @param {string} options.imageName - Docker image name (e.g., 'swp')
|
|
8
|
-
* @param {string} options.registryUrl - JFrog registry URL
|
|
9
|
-
* @param {string} options.repoName - Repository name in JFrog
|
|
10
|
-
* @param {string} options.username - JFrog username
|
|
11
|
-
* @param {string} options.password - JFrog password
|
|
12
|
-
* @param {string} options.buildNumber - Build number
|
|
13
|
-
*/
|
|
14
|
-
export async function publishMicroservice(gitSha, options = {}) {
|
|
15
|
-
// Use environment variables as defaults, but allow overrides
|
|
16
|
-
const config = {
|
|
17
|
-
imageName:
|
|
18
|
-
options.imageName || process.env.IMAGE_NAME || process.env.APP_NAME,
|
|
19
|
-
registryUrl: options.registryUrl || process.env.JF_REGISTRY_URL,
|
|
20
|
-
repoName: options.repoName || process.env.JF_REGISTRY_REPO_NAME,
|
|
21
|
-
username: options.username || process.env.JF_REGISTRY_USER_DEV,
|
|
22
|
-
password: options.password || process.env.JF_REGISTRY_PASSWORD_DEV,
|
|
23
|
-
buildNumber: options.buildNumber || process.env.BITBUCKET_BUILD_NUMBER,
|
|
24
|
-
tag:
|
|
25
|
-
options.tag ||
|
|
26
|
-
process.env.TAG ||
|
|
27
|
-
process.env.BITBUCKET_TAG ||
|
|
28
|
-
process.env.VERSION_TAG,
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
// Validate required fields
|
|
32
|
-
if (!config.imageName) {
|
|
33
|
-
throw new Error(
|
|
34
|
-
'Image name is required. Set IMAGE_NAME or APP_NAME environment variable.',
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
console.log('=== Docker Image Publishing Configuration ===');
|
|
39
|
-
console.log(`Image Name: ${config.imageName}`);
|
|
40
|
-
console.log(`Git SHA: ${gitSha}`);
|
|
41
|
-
console.log(`Build Number: ${config.buildNumber}`);
|
|
42
|
-
console.log(`Version Tag: ${config.tag || 'none'}`);
|
|
43
|
-
console.log(`Registry: ${config.registryUrl}/${config.repoName}`);
|
|
44
|
-
console.log('==========================================');
|
|
45
|
-
|
|
46
|
-
const registryPath = `${config.registryUrl}/${config.repoName}`;
|
|
47
|
-
const SHA_IMAGE_NAME = `${config.imageName}:sha-${gitSha}`;
|
|
48
|
-
|
|
49
|
-
// Docker login
|
|
50
|
-
await $`echo ${config.password} | docker login --username ${config.username} --password-stdin ${config.registryUrl}`;
|
|
51
|
-
|
|
52
|
-
try {
|
|
53
|
-
// Build the image if it doesn't exist locally
|
|
54
|
-
const imageExists = await checkLocalImage(SHA_IMAGE_NAME);
|
|
55
|
-
if (!imageExists) {
|
|
56
|
-
console.log(`Building Docker image ${SHA_IMAGE_NAME}...`);
|
|
57
|
-
await $`docker build -t ${SHA_IMAGE_NAME} .`;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// Array to track all tags we're pushing
|
|
61
|
-
const tagsToPush = [];
|
|
62
|
-
|
|
63
|
-
// 1. Push SHA tag
|
|
64
|
-
const shaTag = `${registryPath}/${SHA_IMAGE_NAME}`;
|
|
65
|
-
console.log(`Tagging and pushing SHA: ${shaTag}`);
|
|
66
|
-
await $`docker tag ${SHA_IMAGE_NAME} ${shaTag}`;
|
|
67
|
-
await $`docker push ${shaTag}`;
|
|
68
|
-
tagsToPush.push(SHA_IMAGE_NAME);
|
|
69
|
-
|
|
70
|
-
// 2. Push build number tag
|
|
71
|
-
if (config.buildNumber) {
|
|
72
|
-
const BUILD_IMAGE_NAME = `${config.imageName}:build-${config.buildNumber}`;
|
|
73
|
-
const buildTag = `${registryPath}/${BUILD_IMAGE_NAME}`;
|
|
74
|
-
console.log(`Tagging and pushing build: ${buildTag}`);
|
|
75
|
-
await $`docker tag ${SHA_IMAGE_NAME} ${buildTag}`;
|
|
76
|
-
await $`docker push ${buildTag}`;
|
|
77
|
-
tagsToPush.push(BUILD_IMAGE_NAME);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// 3. Push version tag (this is what ArgoCD needs!)
|
|
81
|
-
if (config.tag) {
|
|
82
|
-
// Handle various tag formats
|
|
83
|
-
let version = config.tag;
|
|
84
|
-
|
|
85
|
-
// If tag contains @, extract version after it
|
|
86
|
-
if (version.includes('@')) {
|
|
87
|
-
version = 'v' + version.split('@')[1];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Ensure version starts with 'v' if it's a semver without it
|
|
91
|
-
if (/^\d+\.\d+\.\d+/.test(version) && !version.startsWith('v')) {
|
|
92
|
-
version = 'v' + version;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
const VERSION_IMAGE_NAME = `${config.imageName}:${version}`;
|
|
96
|
-
const versionTag = `${registryPath}/${VERSION_IMAGE_NAME}`;
|
|
97
|
-
|
|
98
|
-
console.log(`Tagging and pushing version: ${versionTag}`);
|
|
99
|
-
await $`docker tag ${SHA_IMAGE_NAME} ${versionTag}`;
|
|
100
|
-
await $`docker push ${versionTag}`;
|
|
101
|
-
tagsToPush.push(VERSION_IMAGE_NAME);
|
|
102
|
-
|
|
103
|
-
// Also push 'latest' tag when we have a version
|
|
104
|
-
const LATEST_IMAGE_NAME = `${config.imageName}:latest`;
|
|
105
|
-
const latestTag = `${registryPath}/${LATEST_IMAGE_NAME}`;
|
|
106
|
-
console.log(`Tagging and pushing latest: ${latestTag}`);
|
|
107
|
-
await $`docker tag ${SHA_IMAGE_NAME} ${latestTag}`;
|
|
108
|
-
await $`docker push ${latestTag}`;
|
|
109
|
-
tagsToPush.push(LATEST_IMAGE_NAME);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// 4. Check for git tags at current commit
|
|
113
|
-
try {
|
|
114
|
-
const gitTags = (await $`git tag --points-at HEAD`).stdout
|
|
115
|
-
.trim()
|
|
116
|
-
.split('\n')
|
|
117
|
-
.filter(Boolean);
|
|
118
|
-
|
|
119
|
-
if (gitTags.length > 0) {
|
|
120
|
-
console.log(`Found git tags at HEAD: ${gitTags.join(', ')}`);
|
|
121
|
-
|
|
122
|
-
for (const gitTag of gitTags) {
|
|
123
|
-
// Skip if we already handled this tag above
|
|
124
|
-
if (gitTag === config.tag) continue;
|
|
125
|
-
|
|
126
|
-
let version = gitTag;
|
|
127
|
-
if (version.includes('@')) {
|
|
128
|
-
version = 'v' + version.split('@')[1];
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const TAG_IMAGE_NAME = `${config.imageName}:${version}`;
|
|
132
|
-
const gitTagPath = `${registryPath}/${TAG_IMAGE_NAME}`;
|
|
133
|
-
|
|
134
|
-
console.log(`Tagging and pushing git tag: ${gitTagPath}`);
|
|
135
|
-
await $`docker tag ${SHA_IMAGE_NAME} ${gitTagPath}`;
|
|
136
|
-
await $`docker push ${gitTagPath}`;
|
|
137
|
-
tagsToPush.push(TAG_IMAGE_NAME);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
} catch (e) {
|
|
141
|
-
console.log('No git tags found at HEAD or error checking tags', e);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
console.log('\n✅ Docker image published successfully!');
|
|
145
|
-
console.log('=== Summary of pushed tags ===');
|
|
146
|
-
tagsToPush.forEach((tag) => console.log(` ✓ ${tag}`));
|
|
147
|
-
console.log('==============================\n');
|
|
148
|
-
|
|
149
|
-
// Verify the version tag if we expect one
|
|
150
|
-
if (config.tag) {
|
|
151
|
-
const version = config.tag.includes('@')
|
|
152
|
-
? 'v' + config.tag.split('@')[1]
|
|
153
|
-
: config.tag;
|
|
154
|
-
const expectedTag = `${registryPath}/${config.imageName}:${version}`;
|
|
155
|
-
console.log(`Verifying version tag exists: ${expectedTag}`);
|
|
156
|
-
try {
|
|
157
|
-
await $`docker manifest inspect ${expectedTag}`;
|
|
158
|
-
console.log('✅ Version tag verified in registry!');
|
|
159
|
-
} catch (e) {
|
|
160
|
-
console.error('❌ WARNING: Could not verify version tag in registry!');
|
|
161
|
-
console.error('ArgoCD may fail to pull this image.', e);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
} catch (error) {
|
|
165
|
-
console.error('Error during image publishing:', error);
|
|
166
|
-
throw error;
|
|
167
|
-
} finally {
|
|
168
|
-
// Always logout
|
|
169
|
-
await $`docker logout ${config.registryUrl}`;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
async function checkLocalImage(imageName) {
|
|
174
|
-
try {
|
|
175
|
-
$.verbose = false;
|
|
176
|
-
await $`docker image inspect ${imageName}`;
|
|
177
|
-
return true;
|
|
178
|
-
} catch {
|
|
179
|
-
return false;
|
|
180
|
-
} finally {
|
|
181
|
-
$.verbose = true;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// Backward compatibility wrapper
|
|
186
|
-
export async function publishWithDefaults(gitSha) {
|
|
187
|
-
return publishMicroservice(gitSha);
|
|
188
|
-
}
|