From c11dfbbe00fe2ea6e2f7a90a2a4f62ee08a0cb56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milorad=20FIlipovi=C4=87?= Date: Wed, 13 Sep 2023 15:24:02 +0200 Subject: [PATCH] ci: Add reusable unit test workflow (no-changelog) (#7159) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Github issue / Community forum post (link here to close automatically): --------- Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ --- .github/workflows/ci-master.yml | 36 ++----------- .github/workflows/ci-pull-requests.yml | 35 ++----------- .github/workflows/units-tests-dispatch.yml | 39 ++++++++++++++ .github/workflows/units-tests-reusable.yml | 60 ++++++++++++++++++++++ 4 files changed, 108 insertions(+), 62 deletions(-) create mode 100644 .github/workflows/units-tests-dispatch.yml create mode 100644 .github/workflows/units-tests-reusable.yml diff --git a/.github/workflows/ci-master.yml b/.github/workflows/ci-master.yml index 9977f9cd06..1c76725066 100644 --- a/.github/workflows/ci-master.yml +++ b/.github/workflows/ci-master.yml @@ -40,41 +40,15 @@ jobs: unit-test: name: Unit tests - runs-on: ubuntu-latest + uses: ./.github/workflows/units-tests-reusable.yml needs: install-and-build strategy: matrix: node-version: [18.x, 20.5] - steps: - - uses: actions/checkout@v3.5.3 - with: - repository: n8n-io/n8n - ref: ${{ inputs.branch }} - - - uses: pnpm/action-setup@v2.4.0 - - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v3.7.0 - with: - node-version: ${{ matrix.node-version }} - cache: pnpm - - - name: Install dependencies - run: pnpm install --frozen-lockfile - - - name: Restore cached build artifacts - uses: actions/cache/restore@v3.3.1 - with: - path: ./packages/**/dist - key: ${{ github.sha }}-base:${{ matrix.node-version }}-test-lint - - - name: Test - run: pnpm test - - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3 - with: - files: packages/@n8n/client-oauth2/coverage/cobertura-coverage.xml,packages/cli/coverage/cobertura-coverage.xml,packages/core/coverage/cobertura-coverage.xml,packages/design-system/coverage/cobertura-coverage.xml,packages/editor-ui/coverage/cobertura-coverage.xml,packages/nodes-base/coverage/cobertura-coverage.xml,packages/workflow/coverage/cobertura-coverage.xml + with: + ref: ${{ inputs.branch }} + nodeVersion: ${{ matrix.node-version }} + cacheKey: ${{ github.sha }}-base:${{ matrix.node-version }}-test-lint lint: name: Lint changes diff --git a/.github/workflows/ci-pull-requests.yml b/.github/workflows/ci-pull-requests.yml index 6bc6e7f97e..c952e91b6c 100644 --- a/.github/workflows/ci-pull-requests.yml +++ b/.github/workflows/ci-pull-requests.yml @@ -34,38 +34,11 @@ jobs: unit-test: name: Unit tests - runs-on: ubuntu-latest + uses: ./.github/workflows/units-tests-reusable.yml needs: install - steps: - - uses: actions/checkout@v3.5.3 - with: - repository: n8n-io/n8n - ref: ${{ inputs.branch }} - - - uses: pnpm/action-setup@v2.4.0 - - - name: Use Node.js 18 - uses: actions/setup-node@v3.7.0 - with: - node-version: 18.x - cache: pnpm - - - name: Install dependencies - run: pnpm install --frozen-lockfile - - - name: Restore cached build artifacts - uses: actions/cache/restore@v3.3.1 - with: - path: ./packages/**/dist - key: ${{ github.sha }}-base:18-test-lint - - - name: Test - run: pnpm test - - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v3 - with: - files: packages/@n8n/client-oauth2/coverage/cobertura-coverage.xml,packages/cli/coverage/cobertura-coverage.xml,packages/core/coverage/cobertura-coverage.xml,packages/design-system/coverage/cobertura-coverage.xml,packages/editor-ui/coverage/cobertura-coverage.xml,packages/nodes-base/coverage/cobertura-coverage.xml,packages/workflow/coverage/cobertura-coverage.xml + with: + ref: ${{ github.event.pull_request.head.ref }} + cacheKey: ${{ github.sha }}-base:18-test-lint lint: name: Lint changes diff --git a/.github/workflows/units-tests-dispatch.yml b/.github/workflows/units-tests-dispatch.yml new file mode 100644 index 0000000000..5ad63f000e --- /dev/null +++ b/.github/workflows/units-tests-dispatch.yml @@ -0,0 +1,39 @@ +name: Runs unit tests for a given ref + +on: + workflow_dispatch: + inputs: + ref: + description: 'GitHub ref to test.' + required: false + default: 'master' + type: string + prNumber: + description: 'PR number to run tests for.' + required: false + type: number + +jobs: + prepare: + name: Prepare + runs-on: ubuntu-latest + outputs: + branch: ${{ steps.compute-branch.outputs.branch }} + steps: + - name: Compute branch + id: compute-branch + run: | + BRANCH_NAME="" + if [[ "${{ inputs.prNumber }}" != "" && "${{ inputs.prNumber }}" != "null" ]]; then + BRANCH_NAME="refs/pull/${{ inputs.prNumber }}/merge" + else + BRANCH_NAME="${{ inputs.ref }}" + fi + echo "branch=$BRANCH_NAME" >> $GITHUB_OUTPUT + + unit-test: + name: Unit tests + needs: prepare + uses: ./.github/workflows/units-tests-reusable.yml + with: + ref: ${{ needs.prepare.outputs.branch }} diff --git a/.github/workflows/units-tests-reusable.yml b/.github/workflows/units-tests-reusable.yml new file mode 100644 index 0000000000..508ec7d019 --- /dev/null +++ b/.github/workflows/units-tests-reusable.yml @@ -0,0 +1,60 @@ +name: Reusable units test workflow + +on: + workflow_call: + inputs: + ref: + description: 'GitHub ref to test.' + required: false + type: string + default: 'master' + nodeVersion: + description: 'Version of node to use.' + required: false + type: string + default: '18.x' + cacheKey: + description: 'Cache key for modules and build artifacts.' + required: false + default: '' + type: string + +jobs: + unit-test: + name: Unit tests + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3.5.3 + with: + repository: n8n-io/n8n + ref: ${{ inputs.ref }} + + - uses: pnpm/action-setup@v2.4.0 + + - name: Use Node.js ${{ inputs.nodeVersion }} + uses: actions/setup-node@v3.7.0 + with: + node-version: ${{ inputs.nodeVersion }} + cache: pnpm + + - name: Install dependencies + run: pnpm install --frozen-lockfile + + - name: Build + if: ${{ inputs.cacheKey == '' }} + run: pnpm build + + - name: Restore cached build artifacts + if: ${{ inputs.cacheKey != '' }} + uses: actions/cache/restore@v3.3.1 + with: + path: ./packages/**/dist + key: ${{ inputs.cacheKey }} + + - name: Test + run: pnpm test + + - name: Upload coverage to Codecov + uses: codecov/codecov-action@v3 + with: + files: packages/@n8n/client-oauth2/coverage/cobertura-coverage.xml,packages/cli/coverage/cobertura-coverage.xml,packages/core/coverage/cobertura-coverage.xml,packages/design-system/coverage/cobertura-coverage.xml,packages/editor-ui/coverage/cobertura-coverage.xml,packages/nodes-base/coverage/cobertura-coverage.xml,packages/workflow/coverage/cobertura-coverage.xml