@docupace/pipeline 1.0.81-alpha.9 → 1.0.82
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 +3 -2
- package/src/merge/cli-merge-all.js +2 -15
- package/src/merge/cli-merge.js +3 -82
- 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/terraform/import.sh +111 -0
- package/terraform/plan.sh +17 -4
- package/src/micro/deploy-microservice.js +0 -153
- package/src/micro/publish-ms.js +0 -188
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docupace/pipeline",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.82",
|
|
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"
|
|
@@ -226,10 +226,6 @@ async function runPreparePhase(
|
|
|
226
226
|
case 4:
|
|
227
227
|
console.log(`✅ ${configName}: No Changes`);
|
|
228
228
|
break;
|
|
229
|
-
case 5:
|
|
230
|
-
console.log(`❌ Stopping Merge`);
|
|
231
|
-
process.exit(1);
|
|
232
|
-
break;
|
|
233
229
|
default:
|
|
234
230
|
console.log(
|
|
235
231
|
`❌ ${configName}: Unknown error (exit code: ${result.exitCode})`,
|
|
@@ -386,7 +382,7 @@ async function mergeAll(verbose = false) {
|
|
|
386
382
|
config.repositories,
|
|
387
383
|
);
|
|
388
384
|
|
|
389
|
-
|
|
385
|
+
if (existingPRs.length > 0) {
|
|
390
386
|
console.log(
|
|
391
387
|
`❌ Found ${existingPRs.length} existing pull request(s) with "Automated merge with changeset" description. Exiting to prevent conflicts.`,
|
|
392
388
|
);
|
|
@@ -398,7 +394,7 @@ async function mergeAll(verbose = false) {
|
|
|
398
394
|
});
|
|
399
395
|
}
|
|
400
396
|
process.exit(1);
|
|
401
|
-
}
|
|
397
|
+
}
|
|
402
398
|
|
|
403
399
|
if (verbose)
|
|
404
400
|
console.log('✅ No existing automated merge PRs found. Proceeding...');
|
|
@@ -432,15 +428,6 @@ async function mergeAll(verbose = false) {
|
|
|
432
428
|
verbose,
|
|
433
429
|
);
|
|
434
430
|
|
|
435
|
-
if (
|
|
436
|
-
branchResult.success &&
|
|
437
|
-
branchResult.summary.successes > 0 &&
|
|
438
|
-
i < config.mergeBranches.length - 1
|
|
439
|
-
) {
|
|
440
|
-
console.log('Pausing before next merge');
|
|
441
|
-
await new Promise((res) => setTimeout(res, 90000));
|
|
442
|
-
}
|
|
443
|
-
|
|
444
431
|
if (!branchResult.success) {
|
|
445
432
|
console.log(`Exiting due to failure of one or more merges`);
|
|
446
433
|
process.exit(1);
|
package/src/merge/cli-merge.js
CHANGED
|
@@ -216,27 +216,9 @@ function mergeIntoTarget(
|
|
|
216
216
|
stdio: verbose ? 'inherit' : 'pipe',
|
|
217
217
|
});
|
|
218
218
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
stdio: 'pipe',
|
|
223
|
-
});
|
|
224
|
-
if (verbose)
|
|
225
|
-
console.log(`🗑️ Deleted local merge branch: ${mergeBranchName}`);
|
|
226
|
-
} catch {
|
|
227
|
-
// Branch doesn't exist, which is fine
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// Delete remote merge branch
|
|
231
|
-
try {
|
|
232
|
-
execSync(`git push origin --delete ${mergeBranchName}`, {
|
|
233
|
-
stdio: 'pipe',
|
|
234
|
-
});
|
|
235
|
-
if (verbose)
|
|
236
|
-
console.log(`🗑️ Deleted remote merge branch: ${mergeBranchName}`);
|
|
237
|
-
} catch (e) {
|
|
238
|
-
console.error('Failed to delete remote branch', e);
|
|
239
|
-
}
|
|
219
|
+
execSync(`git branch -d ${mergeBranchName}`, {
|
|
220
|
+
stdio: verbose ? 'inherit' : 'pipe',
|
|
221
|
+
});
|
|
240
222
|
|
|
241
223
|
if (verbose) console.log(`✅ Successfully merged into ${targetBranch}`);
|
|
242
224
|
} else {
|
|
@@ -287,62 +269,6 @@ function cleanupFailedMerge(verbose = false) {
|
|
|
287
269
|
}
|
|
288
270
|
}
|
|
289
271
|
|
|
290
|
-
/**
|
|
291
|
-
* Check if source branch has uncommitted changesets
|
|
292
|
-
*/
|
|
293
|
-
function checkForUncommittedChangesets(sourceBranch, verbose = false) {
|
|
294
|
-
if (verbose)
|
|
295
|
-
console.log(`🔍 Checking for uncommitted changesets in ${sourceBranch}`);
|
|
296
|
-
|
|
297
|
-
// Store current branch
|
|
298
|
-
const currentBranch = execSync('git branch --show-current', {
|
|
299
|
-
encoding: 'utf8',
|
|
300
|
-
stdio: 'pipe',
|
|
301
|
-
}).trim();
|
|
302
|
-
|
|
303
|
-
try {
|
|
304
|
-
// Checkout source branch
|
|
305
|
-
|
|
306
|
-
execSync(`git checkout ${sourceBranch}`, {
|
|
307
|
-
stdio: 'pipe',
|
|
308
|
-
});
|
|
309
|
-
|
|
310
|
-
execSync(`git pull`, {
|
|
311
|
-
stdio: verbose ? 'inherit' : 'pipe',
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
// Check for .md files in .changeset directory (excluding README.md)
|
|
315
|
-
try {
|
|
316
|
-
const changesetFiles = execSync(
|
|
317
|
-
'find .changeset -name "*.md" -not -name "README.md" 2>/dev/null | head -1',
|
|
318
|
-
{
|
|
319
|
-
encoding: 'utf8',
|
|
320
|
-
stdio: 'pipe',
|
|
321
|
-
},
|
|
322
|
-
).trim();
|
|
323
|
-
|
|
324
|
-
if (changesetFiles) {
|
|
325
|
-
console.log(
|
|
326
|
-
`❌ Cannot merge a branch where version has not been bumped. Found changesets in ${sourceBranch}`,
|
|
327
|
-
);
|
|
328
|
-
process.exit(5);
|
|
329
|
-
}
|
|
330
|
-
} catch (error) {
|
|
331
|
-
if (error.message.includes('Cannot merge')) {
|
|
332
|
-
throw error;
|
|
333
|
-
}
|
|
334
|
-
// If find command fails (no .changeset directory), that's fine
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
if (verbose) console.log('✅ No uncommitted changesets found');
|
|
338
|
-
} finally {
|
|
339
|
-
// Return to original branch
|
|
340
|
-
execSync(`git checkout ${currentBranch}`, {
|
|
341
|
-
stdio: 'pipe',
|
|
342
|
-
});
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
|
|
346
272
|
export function setupMerge(program) {
|
|
347
273
|
program
|
|
348
274
|
.command('merge')
|
|
@@ -380,11 +306,6 @@ export function setupMerge(program) {
|
|
|
380
306
|
);
|
|
381
307
|
}
|
|
382
308
|
|
|
383
|
-
// Check for uncommitted changesets in source branch
|
|
384
|
-
if (!mergeFail && !mergeSuccess) {
|
|
385
|
-
checkForUncommittedChangesets(sourceBranch, verbose);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
309
|
// 1. Create merge branch from target branch
|
|
389
310
|
const mergeBranchName = createMergeBranch(
|
|
390
311
|
sourceBranch,
|
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
|
+
}
|