Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,38 @@ By default, files are compared after gzip compression, but it's possible to use
compression: "none"
```

### Specifying the base ref

Use the `base-ref` option to compare against a specific ref. Otherwise, the action compares against the PR's base branch.

```diff
name: Compressed Size
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: preactjs/compressed-size-action@v2
with:
+ base-ref: "v1.2.0"
```

Eg, a project could set `base-ref` conditionally to use the default PR base branch for feature PRs, but compare against `production` for release PRs (e.g. those opened by [release-please](https://github.com/googleapis/release-please)). This is useful for release PRs that only represent a version bump and wouldn't produce a meaningful size comparison. Comparing against the previous release tag instead shows cumulative size change across all changes going into the release.

```yaml
name: Compressed Size
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: preactjs/compressed-size-action@v2
with:
base-ref: ${{ startsWith(github.head_ref, 'release-please--') && 'production' || '' }}
```

### Checking multiple bundles

The action reuses the same comment each time it runs on a PR. In order to run the action multiple times against separate bundles for a single PR, you must provide a `comment-key` option, which the action will use to determine which comment to add or update for the run. The example below demonstrates this for separate "modern" and "legacy" bundles:
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ inputs:
description: 'The GITHUB_TOKEN secret'
required: false
default: ${{ github.token }}
base-ref:
description: 'A specific git ref (branch, tag, or SHA) to compare against instead of the PR base branch'
required: false
clean-script:
description: 'An npm-script that cleans/resets state between branch builds'
install-script:
Expand Down
27 changes: 20 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ async function run(octokit, context, token) {
);
}

const inputBaseRef = getInput('base-ref');
if (inputBaseRef) {
console.log(`Overriding base ref with input base-ref: ${inputBaseRef}`);
baseRef = inputBaseRef;
// fallback base sha doesn't make sense if given an explicit base ref:
baseSha = null;
}

if (getInput('cwd')) process.chdir(getInput('cwd'));

const plugin = new SizePlugin({
Expand Down Expand Up @@ -76,15 +84,19 @@ async function run(octokit, context, token) {
console.log('successfully fetched base.ref');
} catch (e) {
console.log('fetching base.ref failed', e.message);
try {
await exec(`git fetch -n origin ${baseSha}`);
console.log('successfully fetched base.sha');
} catch (e) {
console.log('fetching base.sha failed', e.message);
if (baseSha === null) {
throw new Error('base.ref fetch failed and no base.sha as fallback');
} else {
try {
await exec(`git fetch -n`);
await exec(`git fetch -n origin ${baseSha}`);
console.log('successfully fetched base.sha');
} catch (e) {
console.log('fetch failed', e.message);
console.log('fetching base.sha failed', e.message);
try {
await exec(`git fetch -n`);
} catch (e) {
console.log('fetch failed', e.message);
}
}
}
}
Expand All @@ -101,6 +113,7 @@ async function run(octokit, context, token) {
if (!baseRef) throw Error('missing context.payload.base.ref');
await exec(`git reset --hard ${baseRef}`);
} catch (e) {
if (!baseSha) throw e;
await exec(`git reset --hard ${baseSha}`);
}
endGroup();
Expand Down