From b60caa9eb182ee6eaaac8f03ae07fcb0f05e8764 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20St=C4=99pniewski?= <190422+k3nsei@users.noreply.github.com> Date: Fri, 31 Mar 2023 16:20:56 +0200 Subject: [PATCH] ci: reduce duplication in workflows --- .github/actions/install-npm-deps/action.yml | 42 +++++++++++++ .github/workflows/main.yml | 69 ++------------------- .github/workflows/pr.yml | 69 +++------------------ .github/workflows/qodana-scan.yml | 23 +------ .github/workflows/release-please.yml | 24 +------ .gitignore | 12 ++++ 6 files changed, 74 insertions(+), 165 deletions(-) create mode 100644 .github/actions/install-npm-deps/action.yml diff --git a/.github/actions/install-npm-deps/action.yml b/.github/actions/install-npm-deps/action.yml new file mode 100644 index 00000000..2408cf7c --- /dev/null +++ b/.github/actions/install-npm-deps/action.yml @@ -0,0 +1,42 @@ +name: Setup Node.js and install NPM dependencies +description: Install Node.js and NPM, then install NPM dependencies + +inputs: + node-version: + description: 'Which version of Node.js to install' + required: false + default: 'lts/*' + registry-url: + description: 'Which NPM registry url to use' + required: false + default: 'https://registry.npmjs.org' + npm-cache-key: + description: 'Key for naming NPM cache' + required: false + default: npm-cache + +runs: + using: composite + steps: + - name: Setup Node.js and NPM + uses: actions/setup-node@v3 + with: + node-version: ${{ inputs.node-version }} + registry-url: ${{ inputs.registry-url }} + + - name: Get NPM cache directory + id: npm-cache-dir + shell: bash + run: echo "dir=$(npm config get cache --global)" >> ${GITHUB_OUTPUT} + + - name: Restore NPM cache + uses: actions/cache@v3 + with: + path: ${{ steps.npm-cache-dir.outputs.dir }} + key: ${{ runner.os }}-${{ inputs.npm-cache-key }}-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-${{ inputs.npm-cache-key }} + + - name: Install NPM dependencies + shell: bash + run: npm ci diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 50cdb9b7..a349b8a1 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -22,27 +22,8 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 - - name: Setup node.js - uses: actions/setup-node@v3 - with: - node-version: 'lts/*' - - - name: Get npm cache directory - id: npm-cache-dir - shell: bash - run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} - - - name: Restore dependencies cache - id: npm-cache - uses: actions/cache@v3 - with: - path: ${{ steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-main-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-node- - - - name: Install dependencies - run: npm ci + - name: Setup Node.js and install NPM dependencies + uses: ./.github/actions/install-npm-deps - name: Check code format run: npm run format @@ -95,27 +76,8 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 - - name: Setup node.js - uses: actions/setup-node@v3 - with: - node-version: 'lts/*' - - - name: Get npm cache directory - id: npm-cache-dir - shell: bash - run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} - - - name: Restore dependencies cache - id: npm-cache - uses: actions/cache@v3 - with: - path: ${{ steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-main-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-node- - - - name: Install dependencies - run: npm ci + - name: Setup Node.js and install NPM dependencies + uses: ./.github/actions/install-npm-deps - name: Download build artifact uses: actions/download-artifact@v3 @@ -152,27 +114,8 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 - - name: Setup node.js - uses: actions/setup-node@v3 - with: - node-version: 'lts/*' - - - name: Get npm cache directory - id: npm-cache-dir - shell: bash - run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} - - - name: Restore dependencies cache - id: npm-cache - uses: actions/cache@v3 - with: - path: ${{ steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-main-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-node- - - - name: Install dependencies - run: npm ci + - name: Setup Node.js and install NPM dependencies + uses: ./.github/actions/install-npm-deps - name: Download build artifact uses: actions/download-artifact@v3 diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 08224665..3c01c895 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -16,27 +16,10 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 - - name: Setup node.js - uses: actions/setup-node@v3 + - name: Setup Node.js and install NPM dependencies + uses: ./.github/actions/install-npm-deps with: - node-version: 'lts/*' - - - name: Get npm cache directory - id: npm-cache-dir - shell: bash - run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} - - - name: Restore dependencies cache - id: npm-cache - uses: actions/cache@v3 - with: - path: ${{ steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-pr-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-node- - - - name: Install dependencies - run: npm ci + npm-cache-key: 'npm-cache-pr' - name: Check code format run: npm run format @@ -83,27 +66,10 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 - - name: Setup node.js - uses: actions/setup-node@v3 - with: - node-version: 'lts/*' - - - name: Get npm cache directory - id: npm-cache-dir - shell: bash - run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} - - - name: Restore dependencies cache - id: npm-cache - uses: actions/cache@v3 + - name: Setup Node.js and install NPM dependencies + uses: ./.github/actions/install-npm-deps with: - path: ${{ steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-pr-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-node- - - - name: Install dependencies - run: npm ci + npm-cache-key: 'npm-cache-pr' - name: Download build artifact uses: actions/download-artifact@v3 @@ -140,27 +106,10 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 - - name: Setup node.js - uses: actions/setup-node@v3 - with: - node-version: 'lts/*' - - - name: Get npm cache directory - id: npm-cache-dir - shell: bash - run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} - - - name: Restore dependencies cache - id: npm-cache - uses: actions/cache@v3 + - name: Setup Node.js and install NPM dependencies + uses: ./.github/actions/install-npm-deps with: - path: ${{ steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-pr-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-node- - - - name: Install dependencies - run: npm ci + npm-cache-key: 'npm-cache-pr' - name: Download build artifact uses: actions/download-artifact@v3 diff --git a/.github/workflows/qodana-scan.yml b/.github/workflows/qodana-scan.yml index 6cdb72f6..b405b557 100644 --- a/.github/workflows/qodana-scan.yml +++ b/.github/workflows/qodana-scan.yml @@ -21,27 +21,10 @@ jobs: with: fetch-depth: 0 - - name: Setup node.js - uses: actions/setup-node@v3 + - name: Setup Node.js and install NPM dependencies + uses: ./.github/actions/install-npm-deps with: - node-version: 'lts/*' - - - name: Get npm cache directory - id: npm-cache-dir - shell: bash - run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} - - - name: Restore dependencies cache - id: npm-cache - uses: actions/cache@v3 - with: - path: ${{ steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-qodana-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-node- - - - name: Install dependencies - run: npm ci + npm-cache-key: 'npm-cache-qodana' - name: Qodana Scan uses: JetBrains/qodana-action@v2022.3.4 diff --git a/.github/workflows/release-please.yml b/.github/workflows/release-please.yml index f52ddad1..cdde83ed 100644 --- a/.github/workflows/release-please.yml +++ b/.github/workflows/release-please.yml @@ -28,33 +28,13 @@ jobs: if: ${{ steps.release.outputs.release_created }} uses: actions/checkout@v3 - - name: Setup node.js + - name: Setup Node.js and install NPM dependencies if: ${{ steps.release.outputs.release_created }} - uses: actions/setup-node@v3 + uses: ./.github/actions/install-npm-deps with: node-version: 'lts/*' registry-url: 'https://registry.npmjs.org' - - name: Get npm cache directory - if: ${{ steps.release.outputs.release_created }} - id: npm-cache-dir - shell: bash - run: echo "dir=$(npm config get cache)" >> ${GITHUB_OUTPUT} - - - name: Restore dependencies cache - if: ${{ steps.release.outputs.release_created }} - id: npm-cache - uses: actions/cache@v3 - with: - path: ${{ steps.npm-cache-dir.outputs.dir }} - key: ${{ runner.os }}-node-release-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-node- - - - name: Install dependencies - if: ${{ steps.release.outputs.release_created }} - run: npm ci - - name: Build library if: ${{ steps.release.outputs.release_created }} run: npm run build:lib diff --git a/.gitignore b/.gitignore index 0711527e..3e59b4b1 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,15 @@ testem.log # System files .DS_Store Thumbs.db + +# GitHub +/.github/* +/.github/actions/* +/.github/workflows/* +!/.github/dependabot.yml +!/.github/funding.yml +!/.github/actions/install-npm-deps/action.yml +!/.github/workflows/main.yml +!/.github/workflows/pr.yml +!/.github/workflows/qodana-scan.yml +!/.github/workflows/release-please.yml