|
| 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 | +})(); |
0 commit comments