From 4e9e4f5041cf4eb1c7c28a6fc5a0fb6e715f1770 Mon Sep 17 00:00:00 2001 From: ptaylor Date: Mon, 20 May 2024 20:09:48 -0700 Subject: [PATCH] refactor into reusable workflows --- .github/workflows/bundle.yml | 52 +++++++++ .github/workflows/lint.yml | 43 ++++++++ .github/workflows/main.pr.yml | 199 +++------------------------------- .github/workflows/test.yml | 117 ++++++++++++++++++++ 4 files changed, 228 insertions(+), 183 deletions(-) create mode 100644 .github/workflows/bundle.yml create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/bundle.yml b/.github/workflows/bundle.yml new file mode 100644 index 00000000..3e1a1e43 --- /dev/null +++ b/.github/workflows/bundle.yml @@ -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 }} diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000..80c083aa --- /dev/null +++ b/.github/workflows/lint.yml @@ -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 diff --git a/.github/workflows/main.pr.yml b/.github/workflows/main.pr.yml index 2bb8e62b..51a63007 100644 --- a/.github/workflows/main.pr.yml +++ b/.github/workflows/main.pr.yml @@ -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: @@ -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: @@ -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 }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..9f607260 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,117 @@ +name: Test + +on: + workflow_call: + inputs: + args: {type: string, required: false} + node: {type: string, required: true} + target: {type: string, required: true} + module: {type: string, required: true} + +jobs: + build-and-test-pull-request: + name: ${{ inputs.node }} ${{ inputs.target }} ${{ inputs.module }} + runs-on: ubuntu-22.04 + steps: + - name: Setup node v${{ inputs.node }} + uses: actions/setup-node@v4 + with: + node-version: ${{ inputs.node }} + + - name: Checkout + uses: actions/checkout@v4 + with: + persist-credentials: false + + - name: Construct cache keys + run: | + echo node_modules_key='["${{ runner.os }}", "node_modules", "${{ inputs.node }}", "${{ hashFiles('package.json', 'yarn.lock') }}"]' >> $GITHUB_ENV; + echo targets_key='["${{ runner.os }}", "targets", "${{ inputs.node }}", "${{ 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: 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' && inputs.target != 'src' }} + env: + t: "${{ inputs.target }}" + m: "${{ inputs.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: "${{ inputs.target }}" + m: "${{ inputs.module }}" + run: | + yarn test ${t:+-t ${t}} ${m:+-m ${m}} ${{ inputs.args }} + + - name: Test importing + if: ${{ steps.source_files_changed.outputs.any_modified == 'true' && inputs.target != 'src' }} + env: + t: "${{ inputs.target }}" + m: "${{ inputs.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}";