action-setup-pnpm/action.yml
2024-04-21 13:52:13 +08:00

98 lines
3.3 KiB
YAML

name: 'Pnpm Setup and Install'
description: 'Setup pnpm and run pnpm install with cache enabled'
inputs:
run_install:
description: "If specified, run pnpm install."
required: false
default: 'true'
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'
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
if: ${{ inputs.enable-corepack == 'true' }}
shell: bash
working-directory: ${{ inputs.cwd }}
run: |
corepack enable
echo "corepack enabled"
- 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 }}
- 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:
# Prevent installing new dependencies without using pnpm
patterns: |
**/package-lock.json
**/pnpm-lock.yaml
**/yarn.lock
- uses: actions/cache@v3
name: Setup pnpm cache
id: pnpm-cache
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 }}-
- 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 }}-
# 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
- name: Install dependencies
if: ${{ steps.pnpm-cache.outputs.cache-hit != 'true' && inputs.run_install == 'true' }}
shell: bash
working-directory: ${{ inputs.cwd }}
run: ${{ inputs.install_cmd }}
env:
# Other environment variables
HUSKY: '0' # By default do not run HUSKY install