-
Notifications
You must be signed in to change notification settings - Fork 29
Make Knip and Jest workflows reusable #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,29 +1,12 @@ | ||
| name: Run Jest testing suite | ||
|
|
||
| on: | ||
| workflow_dispatch: | ||
| pull_request: | ||
|
|
||
| env: | ||
| NODE_ENV: "test" | ||
|
|
||
| jobs: | ||
| testing: | ||
| permissions: write-all | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - uses: oven-sh/setup-bun@v2 | ||
| with: | ||
| bun-version: latest | ||
|
|
||
| - uses: actions/checkout@v5 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Jest With Coverage | ||
| run: | | ||
| bun install --frozen-lockfile | ||
| bun run test | ||
|
|
||
| - name: Add Jest Report to Summary | ||
| if: always() | ||
| run: echo "$(cat test-dashboard.md)" >> $GITHUB_STEP_SUMMARY | ||
| uses: ./.github/workflows/reusable/jest.yml | ||
| with: | ||
| package_manager: bun | ||
| enable_summary: true |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| # Reusable Workflows | ||
|
|
||
| This directory contains reusable GitHub workflows that can be used across multiple repositories in the UbiquityOS ecosystem. | ||
|
|
||
| ## Available Workflows | ||
|
|
||
| ### Knip Workflow | ||
|
|
||
| **File:** `reusable/knip.yml` | ||
|
|
||
| Reusable Knip workflow with configurable package manager support. | ||
|
|
||
| #### Inputs | ||
|
|
||
| | Input | Type | Default | Description | | ||
| |-------|------|---------|-------------| | ||
| | `package_manager` | string | `bun` | Package manager to use (bun, yarn, npm, pnpm) | | ||
| | `yarn_version` | string | `latest` | Yarn version (only used if package_manager is yarn) | | ||
| | `node_version` | string | `latest` | Node.js version | | ||
| | `working_directory` | string | `.` | Working directory for the workflow | | ||
| | `knip_script` | string | `knip` | Knip script name to run | | ||
| | `enable_reporter` | boolean | `true` | Enable Knip reporter for PR comments | | ||
|
|
||
| #### Usage Example | ||
|
|
||
| ```yaml | ||
| name: Knip Check | ||
|
|
||
| on: | ||
| pull_request: | ||
| workflow_dispatch: | ||
|
|
||
| jobs: | ||
| knip: | ||
| uses: ubiquity-os/plugin-template/.github/workflows/reusable/knip.yml@main | ||
| with: | ||
| package_manager: bun | ||
| enable_reporter: true | ||
| ``` | ||
|
|
||
| #### Usage with Yarn | ||
|
|
||
| ```yaml | ||
| name: Knip Check | ||
|
|
||
| on: | ||
| pull_request: | ||
|
|
||
| jobs: | ||
| knip: | ||
| uses: ubiquity-os/plugin-template/.github/workflows/reusable/knip.yml@main | ||
| with: | ||
| package_manager: yarn | ||
| yarn_version: '3.6.0' | ||
| enable_reporter: true | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ### Jest Testing Workflow | ||
|
|
||
| **File:** `reusable/jest.yml` | ||
|
|
||
| Reusable Jest testing workflow with configurable package manager and coverage support. | ||
|
|
||
| #### Inputs | ||
|
|
||
| | Input | Type | Default | Description | | ||
| |-------|------|---------|-------------| | ||
| | `package_manager` | string | `bun` | Package manager to use (bun, yarn, npm, pnpm) | | ||
| | `yarn_version` | string | `latest` | Yarn version (only used if package_manager is yarn) | | ||
| | `node_version` | string | `latest` | Node.js version | | ||
| | `working_directory` | string | `.` | Working directory for the workflow | | ||
| | `test_script` | string | `test` | Test script name to run | | ||
| | `coverage` | boolean | `false` | Enable code coverage | | ||
| | `coverage_script` | string | `test:coverage` | Coverage script name (if different from test script) | | ||
| | `enable_summary` | boolean | `true` | Enable Jest summary in GitHub Actions summary | | ||
|
|
||
| #### Usage Example | ||
|
|
||
| ```yaml | ||
| name: Test Suite | ||
|
|
||
| on: | ||
| pull_request: | ||
| workflow_dispatch: | ||
|
|
||
| jobs: | ||
| test: | ||
| uses: ubiquity-os/plugin-template/.github/workflows/reusable/jest.yml@main | ||
| with: | ||
| package_manager: bun | ||
| enable_summary: true | ||
| ``` | ||
|
|
||
| #### Usage with Coverage | ||
|
|
||
| ```yaml | ||
| name: Test Suite with Coverage | ||
|
|
||
| on: | ||
| pull_request: | ||
|
|
||
| jobs: | ||
| test: | ||
| uses: ubiquity-os/plugin-template/.github/workflows/reusable/jest.yml@main | ||
| with: | ||
| package_manager: yarn | ||
| yarn_version: '3.6.0' | ||
| coverage: true | ||
| coverage_script: 'test:coverage' | ||
| enable_summary: true | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Benefits | ||
|
|
||
| 1. **Centralized Maintenance**: Update workflows in one place, propagate to all repositories | ||
| 2. **Consistency**: Ensure all repositories use the same testing and linting standards | ||
| 3. **Flexibility**: Configure package manager, versions, and scripts per repository | ||
| 4. **Reduced Duplication**: No need to maintain identical workflow files across repos | ||
|
|
||
| ## Migration Guide | ||
|
|
||
| To migrate an existing repository to use these reusable workflows: | ||
|
|
||
| 1. Remove existing `knip.yml`, `knip-reporter.yml`, and `jest-testing.yml` files | ||
| 2. Create new workflow files that call the reusable workflows (see examples above) | ||
| 3. Test the workflows on a pull request | ||
| 4. Commit and push changes | ||
|
|
||
| ## Permissions | ||
|
|
||
| Both workflows require the following permissions: | ||
|
|
||
| ```yaml | ||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
| actions: read | ||
| ``` | ||
|
|
||
| The Knip workflow additionally needs `actions: read` permission to download artifacts from the workflow run. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| name: Reusable Jest Testing Workflow | ||
|
|
||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| package_manager: | ||
| description: 'Package manager to use (bun, yarn, npm, pnpm)' | ||
| required: false | ||
| default: 'bun' | ||
| type: string | ||
| yarn_version: | ||
| description: 'Yarn version (only used if package_manager is yarn)' | ||
| required: false | ||
| default: 'latest' | ||
| type: string | ||
| node_version: | ||
| description: 'Node.js version' | ||
| required: false | ||
| default: 'latest' | ||
| type: string | ||
| working_directory: | ||
| description: 'Working directory for the workflow' | ||
| required: false | ||
| default: '.' | ||
| type: string | ||
| test_script: | ||
| description: 'Test script name to run' | ||
| required: false | ||
| default: 'test' | ||
| type: string | ||
| coverage: | ||
| description: 'Enable code coverage' | ||
| required: false | ||
| default: false | ||
| type: boolean | ||
| coverage_script: | ||
| description: 'Coverage script name (if different from test script)' | ||
| required: false | ||
| default: 'test:coverage' | ||
| type: string | ||
| enable_summary: | ||
| description: 'Enable Jest summary in GitHub Actions summary' | ||
| required: false | ||
| default: true | ||
| type: boolean | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
|
|
||
| env: | ||
| NODE_ENV: 'test' | ||
|
|
||
| jobs: | ||
| testing: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Checkout | ||
| uses: actions/checkout@v5 | ||
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Setup Node.js | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: ${{ inputs.node_version }} | ||
|
|
||
| - name: Setup Bun | ||
| if: inputs.package_manager == 'bun' | ||
| uses: oven-sh/setup-bun@v2 | ||
| with: | ||
| bun-version: latest | ||
|
|
||
| - name: Setup Yarn | ||
| if: inputs.package_manager == 'yarn' | ||
| uses: actions/setup-node@v4 | ||
| with: | ||
| node-version: ${{ inputs.node_version }} | ||
| - run: corepack enable && corepack prepare yarn@${{ inputs.yarn_version }} --activate | ||
| if: inputs.package_manager == 'yarn' | ||
|
|
||
| - name: Install dependencies | ||
| working-directory: ${{ inputs.working_directory }} | ||
| run: | | ||
| if [ "${{ inputs.package_manager }}" = "bun" ]; then | ||
| bun install --frozen-lockfile | ||
| elif [ "${{ inputs.package_manager }}" = "yarn" ]; then | ||
| yarn install --frozen-lockfile | ||
| elif [ "${{ inputs.package_manager }}" = "npm" ]; then | ||
| npm ci | ||
| elif [ "${{ inputs.package_manager }}" = "pnpm" ]; then | ||
| pnpm install --frozen-lockfile | ||
| fi | ||
|
Comment on lines
+82
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yarn Berry breaks with Yarn 2+ uses |
||
|
|
||
| - name: Run Jest Tests | ||
| id: test-run | ||
| working-directory: ${{ inputs.working_directory }} | ||
| run: | | ||
| if [ "${{ inputs.package_manager }}" = "bun" ]; then | ||
| bun run ${{ inputs.test_script }} | ||
| elif [ "${{ inputs.package_manager }}" = "yarn" ]; then | ||
| yarn ${{ inputs.test_script }} | ||
| elif [ "${{ inputs.package_manager }}" = "npm" ]; then | ||
| npm run ${{ inputs.test_script }} | ||
| elif [ "${{ inputs.package_manager }}" = "pnpm" ]; then | ||
| pnpm run ${{ inputs.test_script }} | ||
| fi | ||
|
|
||
| - name: Run Coverage (if enabled) | ||
| if: inputs.coverage | ||
| working-directory: ${{ inputs.working_directory }} | ||
| run: | | ||
| if [ "${{ inputs.package_manager }}" = "bun" ]; then | ||
| bun run ${{ inputs.coverage_script }} | ||
| elif [ "${{ inputs.package_manager }}" = "yarn" ]; then | ||
| yarn ${{ inputs.coverage_script }} | ||
| elif [ "${{ inputs.package_manager }}" = "npm" ]; then | ||
| npm run ${{ inputs.coverage_script }} | ||
| elif [ "${{ inputs.package_manager }}" = "pnpm" ]; then | ||
| pnpm run ${{ inputs.coverage_script }} | ||
| fi | ||
|
Comment on lines
+95
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Coverage runs in addition to tests, not instead. When ♻️ Suggested fix- - name: Run Jest Tests
- id: test-run
+ - name: Run Jest Tests
+ id: test-run
+ if: ${{ !inputs.coverage }}
working-directory: ${{ inputs.working_directory }}
run: |
...
- - name: Run Coverage (if enabled)
+ - name: Run Jest Tests with Coverage
if: inputs.coverage |
||
|
|
||
| - name: Add Jest Report to Summary | ||
| if: always() && inputs.enable_summary && github.event_name == 'pull_request' | ||
| run: | | ||
| if [ -f test-dashboard.md ]; then | ||
| echo "$(cat test-dashboard.md)" >> $GITHUB_STEP_SUMMARY | ||
| fi | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Permissions section contradicts itself and the Jest workflow.
The code block lists
actions: readas required for both, but the follow-up sentence says it's Knip-only — and indeedreusable/jest.ymlonly declarescontents: read+pull-requests: write. Split the blocks per workflow to avoid confusion.📝 Suggested fix
-The Knip workflow additionally needs
actions: readpermission to download artifacts from the workflow run.