Files
composite-actions/setup-node-environment/action.yml
2024-10-13 17:21:42 +08:00

92 lines
3.6 KiB
YAML

# 🔗 Links:
# Source file: https://github.com/obytes/react-native-template-obytes/blob/master/.github/actions/setup-node-pnpm-install/action.yml
# Composite actions docs: https://docs.github.com/en/actions/creating-actions/creating-a-composite-action
# ✍️ Description:
# This is a composite action, which means it can be used in other actions.
# It is used in almost all workflows to set up the environment and install dependencies.
# Updating the package manager or Node version here will be reflected in all workflows.
# 👀 Example usage:
# - name : 📦 Setup Node + PNPM + install deps
# uses: ./.github/actions/setup-node-pnpm-install
name: "Setup Node Environment"
description: "Setup pnpm + Node.js + install dependencies"
runs:
using: "composite"
steps:
- name: Check .git folder
id: check-git-folder
shell: bash
run: |
if [ -d .git ]; then
echo "Found .git folder"
echo "git-folder-exists=true" >> $GITHUB_OUTPUT
else
echo "No .git folder found"
echo "git-folder-exists=false" >> $GITHUB_OUTPUT
fi
- uses: actions/checkout@main
if: steps.check-git-folder.outputs.git-folder-exists == 'false'
with:
# fetch-depth: 0 # zero stands for full checkout, which is required for semantic-release
filter: blob:none # we don't need all blobs, only the full tree
show-progress: false
- name: Prepare data
shell: bash
id: prepare
run: |
# 判断是否需要 pnpm/action-setup
echo "#############################"
setupPnpm=false
packageManager=$(node -p "require('./package.json').packageManager")
echo "packageManager: $packageManager found in package.json"
if [[ "$packageManager" == "pnpm"* ]] && ! which pnpm > /dev/null; then
echo "the package manager is pnpm but pnpm is not installed"
setupPnpm=true
else
echo "the package manager is not pnpm or pnpm is already installed"
fi
echo "setupPnpm: $setupPnpm"
echo "setup-pnpm=$setupPnpm" >> $GITHUB_OUTPUT
echo ""
# 判断是否需要运行 pnpm install
echo "#############################"
if [[ -f "pnpm-lock.yaml" ]]; then
echo "pnpm-lock.yaml exists. So pnpm install --frozen-lockfile will run"
echo "pnpm-lock-exists=true" >> $GITHUB_OUTPUT
echo "cache=pnpm" >> $GITHUB_OUTPUT
else
echo "pnpm-lock.yaml does not exist. So pnpm install will not run"
fi
echo ""
# 设置 node 版本
echo "#############################"
nodeVersion=$(sed -n 's/.*use-node-version=\([0-9.]*\).*/\1/p' .npmrc)
if [[ -z "$nodeVersion" ]]; then
nodeVersion="lts/*"
echo "Node version not found in .npmrc. Using default: $nodeVersion"
else
echo "Node version found in .npmrc: $nodeVersion"
fi
echo "node-version=$nodeVersion" >> $GITHUB_OUTPUT
echo ""
- uses: pnpm/action-setup@v4 # https://github.com/pnpm/action-setup?tab=readme-ov-file#inputs
if: steps.prepare.outputs.setup-pnpm == 'true'
- uses: actions/setup-node@v4 # https://github.com/actions/setup-node?tab=readme-ov-file#usage
with:
node-version: ${{ steps.prepare.outputs.node-version }}
cache: ${{ steps.prepare.outputs.cache }}
- run: cat .npmrc
shell: bash
- name: 📦 Install Project Dependencies
if: steps.prepare.outputs.pnpm-lock-exists == 'true'
run: pnpm install --frozen-lockfile
shell: bash