Skip to content

Commit c305aad

Browse files
committed
chore: debug bundle release workflow
1 parent 81bbea9 commit c305aad

File tree

3 files changed

+69
-71
lines changed

3 files changed

+69
-71
lines changed

.github/workflows/bundle.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Release
2+
3+
on:
4+
release:
5+
types:
6+
- published
7+
8+
jobs:
9+
release:
10+
# Prevents changesets action from creating a PR on forks
11+
if: github.repository == 'patternfly/patternfly-elements'
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v2
15+
- uses: actions/setup-node@v2
16+
with:
17+
node-version: 16
18+
cache: npm
19+
20+
- name: Install packages
21+
run: npm i --prefer-offline
22+
23+
- name: Bundle
24+
id: bundle
25+
if: ${{ steps.changesets.outputs.published }}
26+
uses: actions/github-script@v6
27+
with:
28+
github-token: ${{secrets.GITHUB_TOKEN}}
29+
script: |
30+
const bundle = require('./scripts/bundle-release.cjs');
31+
await bundle({ context, github, glob, workspace: '${{ github.workspace }}' });

.github/workflows/release.yml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,3 @@ jobs:
3535
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3636
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
3737
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
38-
39-
- name: Bundle
40-
id: bundle
41-
if: ${{ steps.changesets.outputs.published }}
42-
uses: actions/github-script@v6
43-
with:
44-
github-token: ${{secrets.GITHUB_TOKEN}}
45-
script: |
46-
const bundle = require('./scripts/bundle-release.cjs');
47-
await bundle({
48-
github,
49-
glob,
50-
workspace: '${{ github.workspace }}',
51-
publishedPackages: JSON.parse('${{ steps.changesets.outputs.publishedPackages }}'),
52-
});

scripts/bundle-release.cjs

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,68 @@
1-
const sleep = ms => new Promise(res => setTimeout(res, ms));
2-
3-
/** exponential back-off function to await results while avoiding rate limits */
4-
async function backoff(fn, depth = 0) {
5-
try {
6-
return await fn();
7-
} catch (e) {
8-
if (depth > 7) {
9-
throw e;
10-
} else {
11-
await sleep(2 ** depth * 10);
12-
return backoff(fn, depth + 1);
13-
}
14-
}
15-
}
16-
17-
module.exports = async function({ github, glob, workspace, publishedPackages }) {
1+
module.exports = async function({ github, glob, workspace, context }) {
182
const tar = require('tar');
193
const { readFile, copyFile } = require('fs').promises;
204

215
const { singleFileBuild } = await import('../tools/pfe-tools/esbuild.js');
226
const { execaCommand } = await import('execa');
237

8+
const { payload } = context;
9+
const { release } = payload;
10+
2411
// https://github.com/patternfly/patternfly-elements
2512
const owner = 'patternfly';
2613
const repo = 'patternfly-elements';
2714

15+
const tag = release.tag_name;
16+
2817
// repo root
2918
const cwd = `${workspace}`;
3019
const outfile = `${cwd}/pfe.min.js`;
3120

3221
await singleFileBuild({ outfile });
3322

34-
async function getRelease({ owner, repo, tag }) {
35-
const release = await github.request(`GET /repos/{owner}/{repo}/releases/tags/{tag}`, { owner, repo, tag });
36-
if (!release.id) {
37-
throw new Error(`Could not find release for tag: ${tag}`);
38-
}
39-
}
23+
const params = {
24+
owner,
25+
release_id: release.id,
26+
repo
27+
};
28+
29+
await copyFile(`${cwd}/core/pfe-styles/pfe.min.css`, `${cwd}/pfe.min.css`);
4030

41-
for (const { name: packageName, version } of publishedPackages) {
42-
// get the tag for the release for this package
43-
const tag = `${packageName}@${version}`;
31+
const globber = await glob.create('pfe.min.*');
32+
const files = await globber.glob();
4433

45-
// wait until the release is created
46-
const release = await backoff(() => getRelease({ owner, repo, tag }));
34+
// eslint-disable-next-line
35+
console.log('creating tarball for', files);
4736

48-
const params = {
49-
owner,
50-
release_id: release.id,
51-
repo
52-
};
37+
await tar.c({ gzip: true, file: 'pfe.min.tgz' }, files);
5338

54-
await copyFile(`${cwd}/core/pfe-styles/pfe.min.css`, `${cwd}/pfe.min.css`);
39+
// upload the bundle to each release
40+
await github.rest.repos.uploadReleaseAsset({
41+
...params,
42+
name: 'pfe.min.tgz',
43+
data: await readFile(`${cwd}/pfe.min.tgz`),
44+
});
5545

56-
const globber = await glob.create('pfe.min.*');
57-
const files = await globber.glob();
46+
const { packageName } = tag.match(/^(?<packageName>@[-\w]+[/]{1}[-\w]+)@(.*)$/)?.groups ?? {};
5847

48+
if (!packageName) {
5949
// eslint-disable-next-line
60-
console.log('creating tarball for', files);
50+
console.log(release);
51+
throw new Error('No Package found');
52+
}
6153

62-
await tar.c({ gzip: true, file: 'pfe.min.tgz' }, files);
54+
// make a tarball for the package
55+
// this was already published to npm in the changesets action
56+
const { stdout } = await execaCommand(`npm run pack -w ${packageName}`);
57+
const match = stdout.match(/^[\w-.]+\.tgz$/g);
6358

64-
// upload the bundle to each release
59+
// upload the package tarball to the release
60+
if (match) {
61+
const [name] = match;
6562
await github.rest.repos.uploadReleaseAsset({
6663
...params,
67-
name: 'pfe.min.tgz',
68-
data: await readFile(`${cwd}/pfe.min.tgz`),
64+
name,
65+
data: await readFile(`${cwd}/${name}`),
6966
});
70-
71-
// make a tarball for the package
72-
// this was already published to npm in the changesets action
73-
const { stdout } = await execaCommand(`npm run pack -w ${packageName}`);
74-
const match = stdout.match(/^[\w-.]+\.tgz$/g);
75-
76-
// upload the package tarball to the release
77-
if (match) {
78-
const [name] = match;
79-
await github.rest.repos.uploadReleaseAsset({
80-
...params,
81-
name,
82-
data: await readFile(`${cwd}/${name}`),
83-
});
84-
}
8567
}
8668
};

0 commit comments

Comments
 (0)