From a95ad430a3b5f2902393fbac67c63f3c81c8d8de Mon Sep 17 00:00:00 2001 From: Eric Cornelissen Date: Tue, 29 Jun 2021 12:07:50 +0200 Subject: [PATCH] Improve CI config: caching & refactoring (#5912) * Set up caching jest cache for tests in CI Use actions/cache to cache the jest cache between runs. The cache will be invalided if the package-lock.json file is updated OR after 7 days [1]. The cache directory for jest is now also configured to be in a directory called ".jestcache" at the root of this project. This directory will be ignored by git due to its addition to the .gitignore. -- 1. https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows#usage-limits-and-eviction-policy * Trigger jest cache in CI * Include date in cache key This way the cache is invalidated very day * Add name for caching Jest's cache step * Demonstrate test failure with cache enabled * Revert ff915d466075d5292c7b5772406ff62fe1a3e220 * Cache dependencies * Update Jest cache configuration * Refactor and use Jest cache in Publish workfow - Refactor out the duplicated sanity check in the publish workflow into a separate, depended upon, job. - Use the Jest cache in the Publish workflow. * Update linter job in CI to just run `npm run lint` ... whereas previously it ran each linter separately. This avoids having to update the workflow file if a new linter is added (or a linter is removed). * Use run_number instead of date for jest cache in CI * Simplify GitHub release publish job --- .github/workflows/publish.yml | 60 +++++++++++++++++++++++++---------- .github/workflows/verify.yml | 35 +++++++++++++++++--- .gitignore | 3 ++ jest.config.js | 3 ++ 4 files changed, 80 insertions(+), 21 deletions(-) create mode 100644 jest.config.js diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 358ed753e..b4e4d84f6 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -5,8 +5,8 @@ on: - master jobs: - npm: - name: NPM Package + sanity-check: + name: Pre-publish checks runs-on: ubuntu-latest steps: - name: Checkout @@ -15,32 +15,60 @@ jobs: uses: actions/setup-node@v2 with: node-version: 12.x + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ~/.npm + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-node- + - name: Cache Jest + uses: actions/cache@v2 + with: + path: .cache/jest + key: ${{ runner.os }}-jest-${{ hashFiles('package-lock.json') }}-${{ github.run_number }} + restore-keys: | + ${{ runner.os }}-jest-${{ hashFiles('package-lock.json') }}- + ${{ runner.os }}-jest- + - name: Install dependencies + run: npm ci + - name: Build NodeJS package + run: npm run build + - name: Run linters + run: npm run lint + - name: Run tests + run: npm run test + npm: + name: NPM Package + needs: sanity-check + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Use Node.js 12.x + uses: actions/setup-node@v2 + with: + node-version: 12.x + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ~/.npm + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-node- - name: Install dependencies run: npm ci - - name: Sanity check - run: | - npm run lint - npm run test - name: Deploy to NPM uses: JS-DevTools/npm-publish@v1 with: token: ${{ secrets.NPM_TOKEN }} github: name: GitHub release + needs: sanity-check runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v2 - - name: Use Node.js 12.x - uses: actions/setup-node@v2 - with: - node-version: 12.x - - name: Install dependencies - run: npm ci - - name: Sanity check - run: | - npm run lint - npm run test - name: Get commit message (for release title and body) id: commit uses: kceb/git-message-action@v1 diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index 208c5c353..dfc600cee 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -12,6 +12,13 @@ jobs: uses: actions/setup-node@v2 with: node-version: 12.x + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ~/.npm + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-node- - name: Install dependencies run: npm ci - name: Build NodeJS package @@ -26,14 +33,17 @@ jobs: uses: actions/setup-node@v2 with: node-version: 12.x + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ~/.npm + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-node- - name: Install dependencies run: npm ci - name: Run linter - run: | - npm run jsonlint - npm run svglint - npm run wslint - npm run our-lint + run: npm run lint test: name: Test package runs-on: ubuntu-latest @@ -44,6 +54,21 @@ jobs: uses: actions/setup-node@v2 with: node-version: 12.x + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ~/.npm + key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-node- + - name: Cache Jest + uses: actions/cache@v2 + with: + path: .cache/jest + key: ${{ runner.os }}-jest-${{ hashFiles('package-lock.json') }}-${{ github.run_number }} + restore-keys: | + ${{ runner.os }}-jest-${{ hashFiles('package-lock.json') }}- + ${{ runner.os }}-jest- - name: Install dependencies run: npm ci - name: Run tests diff --git a/.gitignore b/.gitignore index 2c06d5e44..90b510e0d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,9 @@ icons/* # Except SVG files !icons/*.svg +# Caches +.cache/ + ### NodeJS ### # Logs diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 000000000..0699d7e7f --- /dev/null +++ b/jest.config.js @@ -0,0 +1,3 @@ +module.exports = { + cacheDirectory: "./.cache/jest", +};