action-setup-pnpm/action.yml

98 lines
3.3 KiB
YAML
Raw Normal View History

2024-04-21 13:20:33 +08:00
name: 'Pnpm Setup and Install'
2023-11-11 20:27:09 +08:00
description: 'Setup pnpm and run pnpm install with cache enabled'
inputs:
run_install:
2024-04-21 13:20:33 +08:00
description: "If specified, run pnpm install."
2023-11-11 20:27:09 +08:00
required: false
default: 'true'
2024-04-21 13:20:33 +08:00
install_cmd:
description: "Default install cmd is <pnpm install --frozen-lockfile --prefer-offline>"
required: false
default: 'pnpm install --frozen-lockfile --prefer-offline'
enable-corepack:
description: "Enable corepack"
required: false
default: 'true'
verison:
description: "Only enable-corepack false, If you're not setting the packageManager field in package.json, add the version here"
required: false
default: '8'
2023-11-11 20:27:09 +08:00
cwd:
description: "Changes node's process.cwd() if the project is not located on the root. Default to process.cwd()"
required: false
default: '.'
runs:
using: 'composite'
steps:
- name: ⚙️ Enable Corepack
2024-04-21 13:20:33 +08:00
if: ${{ inputs.enable-corepack == 'true' }}
2023-11-11 20:27:09 +08:00
shell: bash
working-directory: ${{ inputs.cwd }}
run: |
corepack enable
echo "corepack enabled"
2024-04-21 13:20:33 +08:00
- uses: pnpm/action-setup@v2.2.4
if: ${{ inputs.enable-corepack == 'false' }}
with:
run_install: false
# If you're not setting the packageManager field in package.json, add the version here
version: ${{ inputs.verison }}
2023-11-11 20:27:09 +08:00
- name: Expose pnpm config(s) through "$GITHUB_OUTPUT"
id: pnpm-config
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT
- name: Cache rotation keys
id: cache-rotation
shell: bash
run: |
echo "YEAR_MONTH=$(/bin/date -u "+%Y%m")" >> $GITHUB_OUTPUT
- uses: seepine/hash-files@v1
name: Get hash of lock files
id: get-hash
with:
2024-04-21 13:20:33 +08:00
# Prevent installing new dependencies without using pnpm
2023-11-11 20:27:09 +08:00
patterns: |
2024-04-21 13:20:33 +08:00
**/package-lock.json
2023-11-11 20:27:09 +08:00
**/pnpm-lock.yaml
2024-04-21 13:20:33 +08:00
**/yarn.lock
2023-11-11 20:27:09 +08:00
- uses: actions/cache@v3
name: Setup pnpm cache
2024-04-21 13:20:33 +08:00
id: pnpm-cache
2023-11-11 20:27:09 +08:00
with:
path: ${{ steps.pnpm-config.outputs.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-cache-${{ steps.cache-rotation.outputs.YEAR_MONTH }}-${{ steps.get-hash.outputs.hash }}
restore-keys: |
${{ runner.os }}-pnpm-store-cache-${{ steps.cache-rotation.outputs.YEAR_MONTH }}-
2024-04-21 13:52:13 +08:00
- uses: actions/cache@v3
name: Setup pnpm node_modules cache
with:
path: node_modules
key: ${{ runner.os }}-pnpm-node_modules-store-cache-${{ steps.cache-rotation.outputs.YEAR_MONTH }}-${{ steps.get-hash.outputs.hash }}
restore-keys: |
${{ runner.os }}-pnpm-node_modules-store-cache-${{ steps.cache-rotation.outputs.YEAR_MONTH }}-
2024-04-21 13:20:33 +08:00
# Prevent store to grow over time (not needed with yarn)
# Note: not perfect as it prune too much in monorepos so the idea
# is to use cache-rotation as above. In the future this might work better.
#- name: Prune pnpm store
# shell: bash
# run: pnpm prune store
2023-11-11 20:27:09 +08:00
- name: Install dependencies
2024-04-21 13:20:33 +08:00
if: ${{ steps.pnpm-cache.outputs.cache-hit != 'true' && inputs.run_install == 'true' }}
2023-11-11 20:27:09 +08:00
shell: bash
working-directory: ${{ inputs.cwd }}
2024-04-21 13:20:33 +08:00
run: ${{ inputs.install_cmd }}
2023-11-11 20:27:09 +08:00
env:
2024-04-21 13:20:33 +08:00
# Other environment variables
HUSKY: '0' # By default do not run HUSKY install