|
| 1 | +import { default as dotenv } from 'dotenv'; |
| 2 | +import { S3Client, PutObjectCommand } from '@aws-sdk/client-s3'; |
| 3 | +import { CloudFrontClient, CreateInvalidationCommand } from "@aws-sdk/client-cloudfront"; |
| 4 | +import fetch from 'node-fetch'; |
| 5 | +import fs from 'fs'; |
| 6 | +import { join, dirname } from 'path'; |
| 7 | +import { fileURLToPath } from 'url'; |
| 8 | +import { request } from '@octokit/request'; |
| 9 | +import readline from 'readline'; |
| 10 | + |
| 11 | +const OWNER = 'signalfx'; |
| 12 | +const REPO = 'splunk-otel-js-browser'; |
| 13 | + |
| 14 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 15 | +const packageConfig = JSON.parse(fs.readFileSync(join(__dirname, '..', 'package.json')).toString()); |
| 16 | +dotenv.config(); |
| 17 | + |
| 18 | +if (!process.env.CDN_DISTRIBUTION_ID) { |
| 19 | + throw new Error('You are missing an environment variable CDN_DISTRIBUTION_ID.'); |
| 20 | +} |
| 21 | +const { CDN_DISTRIBUTION_ID } = process.env; |
| 22 | + |
| 23 | +if (!process.env.CDN_BUCKET_NAME) { |
| 24 | + throw new Error('You are missing an environment variable CDN_BUCKET_NAME.'); |
| 25 | +} |
| 26 | +const { CDN_BUCKET_NAME } = process.env; |
| 27 | + |
| 28 | +const requestWithAuth = request.defaults({ |
| 29 | + headers: { |
| 30 | + authorization: `token ${process.env.GITHUB_TOKEN}`, |
| 31 | + }, |
| 32 | +}); |
| 33 | + |
| 34 | +const { data: releases, status } = await requestWithAuth(`GET /repos/${OWNER}/${REPO}/releases`); |
| 35 | +if (status >= 400) { |
| 36 | + throw new Error('There was an error while trying to fetch the list of releases.'); |
| 37 | +} |
| 38 | + |
| 39 | +const latestRelease = releases[0]; |
| 40 | +if (!latestRelease) { |
| 41 | + throw new Error('Latest release not found.'); |
| 42 | +} |
| 43 | +console.log(`I have found the latest version to be: ${latestRelease.tag_name}.`); |
| 44 | + |
| 45 | +if (!latestRelease.draft) { |
| 46 | + console.warn('This release is already published and may have been uploaded to CDN already.'); |
| 47 | +} |
| 48 | + |
| 49 | +const rl = readline.createInterface({input: process.stdin, output: process.stdout}); |
| 50 | +const question = (text) => new Promise(resolve => rl.question(text, (answer) => resolve(answer))); |
| 51 | +const confirmation = await question('Please retype the version to confirm uploading to CDN: '); |
| 52 | +rl.close(); |
| 53 | + |
| 54 | +if (confirmation !== latestRelease.tag_name) { |
| 55 | + throw new Error('You need to confirm the version before proceeding.'); |
| 56 | +} |
| 57 | + |
| 58 | +console.log(`Uploading release "${latestRelease.name}" to CDN using your AWS credentials.`); |
| 59 | + |
| 60 | +const { data: assets } = await requestWithAuth(`GET /repos/${OWNER}/${REPO}/releases/${latestRelease.id}/assets`); |
| 61 | +console.log(`Release has the following assets: ${assets.map(asset => asset.name).join(", ")}.`); |
| 62 | + |
| 63 | +const s3Client = new S3Client({region: 'us-east-1'}); |
| 64 | +const cfClient = new CloudFrontClient({region: 'us-east-1'}); |
| 65 | +const cdnLinks = ['\n## CDN']; |
| 66 | +for (const asset of assets) { |
| 67 | + console.log(`Fetching ${asset.name} from ${asset.browser_download_url}.`); |
| 68 | + const response = await fetch(asset.browser_download_url); |
| 69 | + const assetBuffer = await response.buffer(); |
| 70 | + |
| 71 | + const versionParts = packageConfig.version.split('.'); |
| 72 | + let isFinalVersion = true; |
| 73 | + while (versionParts.length) { |
| 74 | + const version = `v${versionParts.join('.')}`; |
| 75 | + const key = `o11y-gdi-rum/${version}/${asset.name}`; |
| 76 | + await s3Client.send(new PutObjectCommand({ |
| 77 | + Body: assetBuffer, |
| 78 | + Bucket: CDN_BUCKET_NAME, |
| 79 | + Key: `cdn/${key}`, |
| 80 | + ACL: 'public-read', |
| 81 | + ContentType: asset.content_type, |
| 82 | + CacheControl: 'max-age=3600', |
| 83 | + })); |
| 84 | + const publicUrl = `https://cdn.signalfx.com/${key}`; |
| 85 | + console.log(`Uploaded ${asset.name} as ${publicUrl}`); |
| 86 | + |
| 87 | + if (asset.name == 'splunk-rum.js') { |
| 88 | + cdnLinks.push( |
| 89 | +`### Version ${version} |
| 90 | +${(isFinalVersion ? '' : '**WARNING: Content behind this URL might be updated when we release a new version.**')} |
| 91 | +\`\`\`html |
| 92 | +<script src="${publicUrl}" crossorigin="anonymous"></script> |
| 93 | +\`\`\` |
| 94 | +` |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + versionParts.pop(); // 1.2.3 -> 1.2 |
| 99 | + isFinalVersion = false; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +console.log('Creating an invalidation to refresh shared versions.'); |
| 104 | +const invalidationRef = `o11y-gdi-rum-${new Date().toISOString()}`; |
| 105 | +const { Invalidation } = await cfClient.send(new CreateInvalidationCommand({ |
| 106 | + DistributionId: CDN_DISTRIBUTION_ID, |
| 107 | + InvalidationBatch: { |
| 108 | + CallerReference: invalidationRef, |
| 109 | + Paths: { |
| 110 | + Items: ['/cdn/o11y-gdi-rum/*'], |
| 111 | + Quantity: 1, |
| 112 | + }, |
| 113 | + }, |
| 114 | +})); |
| 115 | +console.log(`Invalidation ${Invalidation.Id} sent. Typically it takes about 5 minutes to execute.`); |
| 116 | + |
| 117 | +console.log('Appending CDN instructions to release description.'); |
| 118 | +await requestWithAuth(`PATCH /repos/${OWNER}/${REPO}/releases/${latestRelease.id}`, { |
| 119 | + body: latestRelease.body + cdnLinks.join('\n'), |
| 120 | +}) |
0 commit comments