Skip to content

Commit e6050b8

Browse files
authored
ci: Publish charts package tarball (.tgz) for PRs (#2824)
Publish charts package tarball for PRs
1 parent 6c7f484 commit e6050b8

16 files changed

Lines changed: 773 additions & 18 deletions

File tree

.buildkite/pipelines/pull_request/pipeline.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
storybookStep,
2626
typeCheckStep,
2727
firebasePreDeployStep,
28+
chartsPackageStep,
2829
} from '../../steps';
2930
import type { Step, CustomCommandStep } from '../../utils';
3031
import { bkEnv, ChangeContext, uploadPipeline } from '../../utils';
@@ -49,6 +50,7 @@ void (async () => {
4950
docsStep(),
5051
storybookStep(),
5152
e2eServerStep(),
53+
chartsPackageStep(),
5254
firebasePreDeployStep(),
5355
ghpDeployStep(),
5456
playwrightVrtStep(),
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
import fs from 'fs';
10+
import path from 'path';
11+
12+
import {
13+
bkEnv,
14+
CHARTS_PACKAGE_MANIFEST_FILENAME,
15+
createChartsPackageManifest,
16+
exec,
17+
setChartsPackageMetadata,
18+
startGroup,
19+
uploadArtifacts,
20+
yarnInstall,
21+
} from '../../utils';
22+
23+
interface NpmPackResult {
24+
filename: string;
25+
version: string;
26+
}
27+
28+
void (async () => {
29+
const prNumber = bkEnv.pullRequestNumber;
30+
const commitSha = bkEnv.commit;
31+
32+
if (!prNumber || !commitSha) {
33+
throw new Error('Charts package tarballs are only published for pull request builds with a commit SHA');
34+
}
35+
36+
await yarnInstall();
37+
38+
startGroup('Preparing @elastic/charts package files');
39+
await exec('node ./packages/charts/scripts/move_txt_files.js');
40+
41+
startGroup('Building @elastic/charts package');
42+
await exec('yarn build', {
43+
cwd: 'packages/charts',
44+
});
45+
46+
startGroup('Packing @elastic/charts package');
47+
const packOutput = await exec('npm pack --json', {
48+
cwd: 'packages/charts',
49+
stdio: 'pipe',
50+
});
51+
const [packResult] = JSON.parse(packOutput) as NpmPackResult[];
52+
53+
if (!packResult?.filename || !packResult.version) {
54+
throw new Error('Failed to parse npm pack output for @elastic/charts');
55+
}
56+
57+
const commitShortSha = commitSha.slice(0, 7);
58+
const liveTarballFilename = `elastic-charts-v${packResult.version}-pr-${prNumber}.tgz`;
59+
const commitTarballFilename = `elastic-charts-v${packResult.version}-pr-${prNumber}-${commitShortSha}.tgz`;
60+
const artifactDir = '.buildkite/artifacts/packages';
61+
const packagedTarballPath = path.join('packages/charts', packResult.filename);
62+
const liveArtifactTarballPath = path.join(artifactDir, liveTarballFilename);
63+
const commitArtifactTarballPath = path.join(artifactDir, commitTarballFilename);
64+
const manifestPath = path.join(artifactDir, CHARTS_PACKAGE_MANIFEST_FILENAME);
65+
const chartsPackageMetadata = {
66+
liveTarballFilename,
67+
commitTarballFilename,
68+
version: packResult.version,
69+
commitSha,
70+
commitShortSha,
71+
};
72+
73+
fs.mkdirSync(artifactDir, { recursive: true });
74+
fs.rmSync(liveArtifactTarballPath, { force: true });
75+
fs.rmSync(commitArtifactTarballPath, { force: true });
76+
fs.rmSync(manifestPath, { force: true });
77+
fs.renameSync(packagedTarballPath, liveArtifactTarballPath);
78+
fs.copyFileSync(liveArtifactTarballPath, commitArtifactTarballPath);
79+
80+
fs.writeFileSync(manifestPath, JSON.stringify(createChartsPackageManifest(chartsPackageMetadata, prNumber), null, 2));
81+
82+
await setChartsPackageMetadata(chartsPackageMetadata);
83+
84+
await uploadArtifacts(liveArtifactTarballPath);
85+
await uploadArtifacts(commitArtifactTarballPath);
86+
await uploadArtifacts(manifestPath);
87+
})();

.buildkite/scripts/steps/firebase_deploy.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99
import fs from 'fs';
1010
import path from 'path';
1111

12-
import { firebaseDeploy, downloadArtifacts, startGroup, decompress, bkEnv } from '../../utils';
12+
import {
13+
firebaseDeploy,
14+
downloadArtifacts,
15+
getChartsPackageMetadata,
16+
prepareChartsPackagesForDeployment,
17+
startGroup,
18+
decompress,
19+
bkEnv,
20+
} from '../../utils';
1321
import { createDeploymentStatus } from '../../utils/deployment';
1422

1523
void (async () => {
@@ -54,19 +62,33 @@ void (async () => {
5462
dest: path.join(outDir, 'a11y-report'),
5563
});
5664

65+
const chartsPackage = await getChartsPackageMetadata(true);
66+
const { liveTarballDest, commitTarballDest, manifestDest, indexDest } = await prepareChartsPackagesForDeployment(
67+
outDir,
68+
chartsPackage,
69+
);
70+
5771
startGroup('Check deployment files');
5872

5973
const hasDocsIndex = fs.existsSync(path.join(outDir, 'index.html'));
6074
const hasStorybookIndex = fs.existsSync(path.join(outDir, 'storybook/index.html'));
6175
const hasE2EIndex = fs.existsSync(path.join(outDir, 'e2e/index.html'));
6276
const hasVrtReportIndex = fs.existsSync(path.join(outDir, 'vrt-report/index.html'));
6377
const hasA11yReportIndex = fs.existsSync(path.join(outDir, 'a11y-report/index.html'));
78+
const hasLiveChartsPackage = fs.existsSync(liveTarballDest);
79+
const hasCommitChartsPackage = fs.existsSync(commitTarballDest);
80+
const hasChartsPackageManifest = fs.existsSync(manifestDest);
81+
const hasChartsPackageIndex = fs.existsSync(indexDest);
6482
const missingFiles = [
6583
['docs', hasDocsIndex],
6684
['storybook', hasStorybookIndex],
6785
['e2e server', hasE2EIndex],
6886
['vrt report', hasVrtReportIndex],
6987
['a11y report', hasA11yReportIndex],
88+
['live charts package tarball', hasLiveChartsPackage],
89+
['commit charts package tarball', hasCommitChartsPackage],
90+
['charts package manifest', hasChartsPackageManifest],
91+
['charts package index', hasChartsPackageIndex],
7092
]
7193
.filter(([, exists]) => !exists)
7294
.map<string>(([f]) => f as string);

.buildkite/scripts/steps/firebase_pre_deploy.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99
import fs from 'fs';
1010
import path from 'path';
1111

12-
import { firebaseDeploy, downloadArtifacts, startGroup, decompress, bkEnv } from '../../utils';
12+
import {
13+
firebaseDeploy,
14+
downloadArtifacts,
15+
getChartsPackageMetadata,
16+
prepareChartsPackagesForDeployment,
17+
startGroup,
18+
decompress,
19+
bkEnv,
20+
} from '../../utils';
1321
import { createDeploymentStatus, createOrUpdateDeploymentComment } from '../../utils/deployment';
1422

1523
void (async () => {
@@ -45,15 +53,30 @@ void (async () => {
4553
dest: path.join(outDir, 'e2e'),
4654
});
4755

56+
// Serve the packaged tarball from the preview site so downstream PRs can install it.
57+
const chartsPackage = await getChartsPackageMetadata(true);
58+
const { liveTarballDest, commitTarballDest, manifestDest, indexDest } = await prepareChartsPackagesForDeployment(
59+
outDir,
60+
chartsPackage,
61+
);
62+
4863
startGroup('Check deployment files');
4964

5065
const hasDocsIndex = fs.existsSync('./e2e_server/public/index.html');
5166
const hasStorybookIndex = fs.existsSync('./e2e_server/public/storybook/index.html');
5267
const hasE2EIndex = fs.existsSync('./e2e_server/public/e2e/index.html');
68+
const hasLiveChartsPackage = fs.existsSync(liveTarballDest);
69+
const hasCommitChartsPackage = fs.existsSync(commitTarballDest);
70+
const hasChartsPackageManifest = fs.existsSync(manifestDest);
71+
const hasChartsPackageIndex = fs.existsSync(indexDest);
5372
const missingFiles = [
5473
['docs', hasDocsIndex],
5574
['storybook', hasStorybookIndex],
5675
['e2e server', hasE2EIndex],
76+
['live charts package tarball', hasLiveChartsPackage],
77+
['commit charts package tarball', hasCommitChartsPackage],
78+
['charts package manifest', hasChartsPackageManifest],
79+
['charts package index', hasChartsPackageIndex],
5780
]
5881
.filter(([, exists]) => !exists)
5982
.map<string>(([f]) => f as string);

.buildkite/steps/charts_package.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0 and the Server Side Public License, v 1; you may not use this file except
5+
* in compliance with, at your election, the Elastic License 2.0 or the Server
6+
* Side Public License, v 1.
7+
*/
8+
9+
import type { CustomCommandStep } from '../utils';
10+
import { bkEnv, createStep, commandStepDefaults } from '../utils';
11+
12+
export const chartsPackageStep = createStep<CustomCommandStep>(() => {
13+
const isPullRequest = bkEnv.isPullRequest;
14+
15+
return {
16+
...commandStepDefaults,
17+
label: ':package: Build - @elastic/charts package',
18+
key: 'build_charts_package_preview',
19+
ignoreForced: true,
20+
skip: isPullRequest ? false : 'Only pull request builds publish charts package tarballs',
21+
commands: ['npx ts-node .buildkite/scripts/steps/charts_package.ts'],
22+
env: {
23+
ECH_CHECK_ID: isPullRequest ? 'build_charts_package_preview' : undefined,
24+
},
25+
};
26+
});

.buildkite/steps/firebase_deploy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const firebaseDeployStep = createStep<CustomCommandStep>(() => {
1919
'build_docs',
2020
'build_storybook',
2121
'build_e2e',
22+
'build_charts_package_preview',
2223
'playwright_vrt_merge_and_status',
2324
'playwright_a11y_merge_and_status',
2425
],

.buildkite/steps/firebase_pre_deploy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const firebasePreDeployStep = createStep<CustomCommandStep>(() => {
1515
label: ':firebase: Pre Deploy - firebase',
1616
key: 'pre_deploy_fb',
1717
allow_dependency_failure: false,
18-
depends_on: ['build_docs', 'build_storybook', 'build_e2e'],
18+
depends_on: ['build_docs', 'build_storybook', 'build_e2e', 'build_charts_package_preview'],
1919
commands: ['npx ts-node .buildkite/scripts/steps/firebase_pre_deploy.ts'],
2020
env: {
2121
ECH_CHECK_ID: 'pre_deploy_fb',

.buildkite/steps/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export * from './playwright_a11y';
1616
export * from './docs';
1717
export * from './storybook';
1818
export * from './e2e_server';
19+
export * from './charts_package';
1920
export * from './firebase_pre_deploy';
2021
export * from './firebase_deploy';
2122
export * from './ghp_deploy';

.buildkite/utils/build.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const getBuildConfig = (): BuildConfig => {
2828
{ name: 'Build - Docs', id: 'build_docs' },
2929
{ name: 'Build - e2e server', id: 'build_e2e' },
3030
{ name: 'Build - Storybook', id: 'build_storybook' },
31+
...(bkEnv.isPullRequest ? [{ name: 'Build - @elastic/charts package', id: 'build_charts_package_preview' }] : []),
3132
{ name: 'Pre Deploy - firebase', id: 'pre_deploy_fb' },
3233
{ name: 'Eslint', id: 'eslint' },
3334
{ name: 'Prettier', id: 'prettier' },

.buildkite/utils/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export const MetaDataKeys = {
1616
deploymentPreviousSha: 'deploymentPreviousSha',
1717
deploymentStatus: 'deploymentStatus',
1818
deploymentUrl: 'deploymentUrl',
19+
chartsPackageLiveTarballFilename: 'chartsPackageLiveTarballFilename',
20+
chartsPackageCommitTarballFilename: 'chartsPackageCommitTarballFilename',
21+
chartsPackageVersion: 'chartsPackageVersion',
22+
chartsPackageCommitSha: 'chartsPackageCommitSha',
23+
chartsPackageCommitShortSha: 'chartsPackageCommitShortSha',
1924
};
2025

2126
/**

0 commit comments

Comments
 (0)