Skip to content

Commit

Permalink
refactor into reusable workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
trxcllnt committed May 21, 2024
1 parent 75c1abb commit 4e9e4f5
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 183 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/bundle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Bundle

on:
workflow_call:
inputs:
bundler: {type: string, required: true}
target: {type: string, required: true}
module: {type: string, required: true}

jobs:
test-tree-shaking-pull-request:
needs:
- build-and-test-pull-request
name: ${{ inputs.bundler }} ${{ inputs.target }} ${{ inputs.module }}
runs-on: ubuntu-22.04
steps:
- name: Setup node v20.x
uses: actions/setup-node@v4
with:
node-version: 20.x
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Construct cache keys
run: |
echo node_modules_key='["${{ runner.os }}", "node_modules", "20.x", "${{ hashFiles('package.json', 'yarn.lock') }}"]' >> $GITHUB_ENV;
echo targets_key='["${{ runner.os }}", "targets", "20.x", "${{ inputs.target }}", "${{ inputs.module }}", "${{ hashFiles('package.json', 'yarn.lock', 'tsconfig.json', 'src/**/*', 'tsconfigs/**/*') }}"]' >> $GITHUB_ENV;
- name: Cache targets
uses: actions/cache@v4
with:
key: ${{ join(fromJSON(env.targets_key), '-') }}
path: targets

- name: Cache node_modules
uses: actions/cache@v4
with:
key: ${{ join(fromJSON(env.node_modules_key), '-') }}
path: node_modules

- name: Test ${{ inputs.bundler }} tree-shaking
env:
t: "${{ inputs.target }}"
m: "${{ inputs.module }}"
run: |
integration/make-files-to-bundle.sh
if test "${t}" != ix; then
yarn build ${t:+-t ${t}} ${m:+-m ${m}}
fi
yarn gulp bundle${t:+:${t}}${m:+:${m}}:${{ inputs.bundler }}
43 changes: 43 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Lint

on:
workflow_call:

jobs:
lint-pull-request:
name: Lint
runs-on: ubuntu-22.04
steps:
- name: Setup node v20.x
uses: actions/setup-node@v4
with:
node-version: 20.x

- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Construct cache keys
run: |
echo node_modules_key='["${{ runner.os }}", "node_modules", "20.x", "${{ hashFiles('package.json', 'yarn.lock') }}"]' >> $GITHUB_ENV;
- name: Cache node_modules
uses: actions/cache@v4
with:
key: ${{ join(fromJSON(env.node_modules_key), '-') }}
path: node_modules

- name: Check if source or test files changed
id: files_changed
uses: tj-actions/changed-files@v44
with:
files: |
src/**/*
spec/**/*
- name: Lint files
if: ${{ steps.files_changed.outputs.any_modified == 'true' }}
run: |
yarn --ignore-engines --non-interactive
yarn lint:ci
199 changes: 16 additions & 183 deletions .github/workflows/main.pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,12 @@ on:
jobs:
lint-pull-request:
name: Lint
runs-on: ubuntu-22.04
steps:
- name: Setup node v20.x
uses: actions/setup-node@v4
with:
node-version: 20.x

- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Construct cache keys
run: |
echo node_modules_key='["${{ runner.os }}", "node_modules", "20.x", "${{ hashFiles('package.json', 'yarn.lock') }}"]' >> $GITHUB_ENV;
- name: Cache node_modules
uses: actions/cache@v4
with:
key: ${{ join(fromJSON(env.node_modules_key), '-') }}
path: node_modules

- name: Check if source or test files changed
id: files_changed
uses: tj-actions/changed-files@v44
with:
files: |
src/**/*
spec/**/*
- name: Lint files
if: ${{ steps.files_changed.outputs.any_modified == 'true' }}
run: |
yarn --ignore-engines --non-interactive
yarn lint:ci
uses: .github/workflows/lint.yml

build-and-test-pull-request:
needs:
- lint-pull-request
name: Test ${{ matrix.node }} ${{ matrix.target }} ${{ matrix.module }}
runs-on: ubuntu-22.04
name: Test
needs: lint-pull-request
uses: .github/workflows/test.yml
strategy:
fail-fast: false
matrix:
Expand All @@ -64,115 +29,16 @@ jobs:
- {node: 18.x, target: ix}
- {node: 20.x, target: ix}
- {node: 20.x, target: src, args: --coverage}
steps:
- name: Setup node v${{ matrix.node }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}

- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Construct cache keys
run: |
echo node_modules_key='["${{ runner.os }}", "node_modules", "${{ matrix.node }}", "${{ hashFiles('package.json', 'yarn.lock') }}"]' >> $GITHUB_ENV;
echo targets_key='["${{ runner.os }}", "targets", "${{ matrix.node }}", "${{ matrix.target }}", "${{ matrix.module }}", "${{ hashFiles('package.json', 'yarn.lock', 'tsconfig.json', 'src/**/*', 'tsconfigs/**/*') }}"]' >> $GITHUB_ENV;
- name: Cache targets
uses: actions/cache@v4
with:
key: ${{ join(fromJSON(env.targets_key), '-') }}
path: targets

- name: Cache node_modules
uses: actions/cache@v4
with:
key: ${{ join(fromJSON(env.node_modules_key), '-') }}
path: node_modules

- name: Check if test files changed
id: test_files_changed
uses: tj-actions/changed-files@v44
with:
files: |
spec/**/*
tsconfig.json
tsconfigs/**/*
jest.config.js
jestconfigs/**/*
- name: Check if source files changed
id: source_files_changed
uses: tj-actions/changed-files@v44
with:
files: |
.npmrc
yarn.lock
package.json
tsconfig.json
src/**/*
gulp/**/*
tsconfigs/**/*
integration/**/*
- name: Install dependencies
if: ${{ steps.source_files_changed.outputs.any_modified == 'true' || steps.test_files_changed.outputs.any_modified == 'true' }}
run: |
yarn --ignore-engines --non-interactive
- name: Build package
if: ${{ steps.source_files_changed.outputs.any_modified == 'true' && matrix.target != 'src' }}
env:
t: "${{ matrix.target }}"
m: "${{ matrix.module }}"
run: |
yarn build ${t:+-t ${t}} ${m:+-m ${m}}
- name: Test package
if: ${{ steps.source_files_changed.outputs.any_modified == 'true' || steps.test_files_changed.outputs.any_modified == 'true' }}
env:
t: "${{ matrix.target }}"
m: "${{ matrix.module }}"
run: |
yarn test ${t:+-t ${t}} ${m:+-m ${m}} ${{ matrix.args }}
- name: Test importing
if: ${{ steps.source_files_changed.outputs.any_modified == 'true' && matrix.target != 'src' }}
env:
t: "${{ matrix.target }}"
m: "${{ matrix.module }}"
run: |
targetdir="./targets${t:+/${t}}${m:+/${m}}";
pkg_name="$(jq -r '.name' "${targetdir}/package.json")";
pkg_type="$(jq -r '.type' "${targetdir}/package.json")";
# Install the package into a temp dir
_tmp="$(mktemp -d)";
mkdir -p "$(dirname "${_tmp}/node_modules/${pkg_name}")";
cp -ar "${targetdir}" "${_tmp}/node_modules/${pkg_name}";
cd "${_tmp}/node_modules/${pkg_name}";
npm i;
cd "${_tmp}/";
set -x;
if test "${pkg_type}" = "module"; then
# Test importing as ESModule
node --input-type=module -e "import '${pkg_name}'";
else
# Test importing as CommonJS
node --input-type=commonjs -e "require('${pkg_name}')";
# Test importing CommonJS module but allow it to fail
node --input-type=module -e "import '${pkg_name}'" || true;
fi
set +x;
cd /;
rm -rf "${_tmp}";
with:
args: ${{ matrix.args }}
node: ${{ matrix.node }}
target: ${{ matrix.target }}
module: ${{ matrix.module }}

test-tree-shaking-pull-request:
needs:
- build-and-test-pull-request
name: Bundle ${{ matrix.bundler }} ${{ matrix.target }} ${{ matrix.module }}
runs-on: ubuntu-22.04
needs: build-and-test-pull-request
name: Bundle
uses: .github/workflows/bundle.yml
strategy:
fail-fast: false
matrix:
Expand All @@ -183,40 +49,7 @@ jobs:
- {bundler: esbuild, target: ix}
- {bundler: rollup, target: ix}
- {bundler: webpack, target: ix}
steps:
- name: Setup node v20.x
uses: actions/setup-node@v4
with:
node-version: 20.x
- name: Checkout
uses: actions/checkout@v4
with:
persist-credentials: false

- name: Construct cache keys
run: |
echo node_modules_key='["${{ runner.os }}", "node_modules", "20.x", "${{ hashFiles('package.json', 'yarn.lock') }}"]' >> $GITHUB_ENV;
echo targets_key='["${{ runner.os }}", "targets", "20.x", "${{ matrix.target }}", "${{ matrix.module }}", "${{ hashFiles('package.json', 'yarn.lock', 'tsconfig.json', 'src/**/*', 'tsconfigs/**/*') }}"]' >> $GITHUB_ENV;
- name: Cache targets
uses: actions/cache@v4
with:
key: ${{ join(fromJSON(env.targets_key), '-') }}
path: targets

- name: Cache node_modules
uses: actions/cache@v4
with:
key: ${{ join(fromJSON(env.node_modules_key), '-') }}
path: node_modules

- name: Test ${{ matrix.bundler }} tree-shaking
env:
t: "${{ matrix.target }}"
m: "${{ matrix.module }}"
run: |
integration/make-files-to-bundle.sh
if test "${t}" != ix; then
yarn build ${t:+-t ${t}} ${m:+-m ${m}}
fi
yarn gulp bundle${t:+:${t}}${m:+:${m}}:${{ matrix.bundler }}
with:
bundler: ${{ matrix.bundler }}
target: ${{ matrix.target }}
module: ${{ matrix.module }}
Loading

0 comments on commit 4e9e4f5

Please sign in to comment.