持續整合 (CI)
pnpm 可以輕易地在多種 CI 系統中使用。
注意事項
本篇指南提供的所有範例中,存放區都已快取。 不過此功能是非強制的,且開啟儲存區快取不一定會讓安裝更快。 因此您可以依需要決定是否啟用。
Travis
對於 Travis CI,在 .travis.yml
檔案中加入這幾行,就可以利用 pnpm 來安裝相依性
.travis.yml
cache:
npm: false
directories:
- "~/.pnpm-store"
before_install:
- corepack enable
- corepack prepare pnpm@latest-7 --activate
- pnpm config set store-dir ~/.pnpm-store
install:
- pnpm install
Semaphore
對於 Semaphore,在 .semaphore/semaphore.yml
檔案中加入這幾行,就可以利用 pnpm 來安裝相依性
.semaphore/semaphore.yml
version: v1.0
name: Semaphore CI pnpm example
agent:
machine:
type: e1-standard-2
os_image: ubuntu1804
blocks:
- name: Install dependencies
task:
jobs:
- name: pnpm install
commands:
- corepack enable
- corepack prepare pnpm@latest-7 --activate
- checkout
- cache restore node-$(checksum pnpm-lock.yaml)
- pnpm install
- cache store node-$(checksum pnpm-lock.yaml) $(pnpm store path)
AppVeyor
對於 AppVeyor,在 appveyor.yml
檔案中加入這幾行,就可以利用 pnpm 來安裝相依性
appveyor.yml
install:
- ps: Install-Product node $env:nodejs_version
- corepack enable
- corepack prepare pnpm@latest-7 --activate
- pnpm install
GitHub Actions
對於 GitHub Actions,在 .github/workflows/NAME.yml
中加入這幾行,就可以利用 pnpm 來安裝、快取相依性
.github/workflows/NAME.yml
name: pnpm Example Workflow
on:
push:
jobs:
build:
runs-on: ubuntu-20.04
strategy:
matrix:
node-version: [15]
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 7
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'
- name: Install dependencies
run: pnpm install
注意事項
當使用 actions/setup-node@v2
來快取相依性時,安裝的 pnpm 版本需高於 6.10 版。
GitLab CI
對於 GitLab,在 .gitlab-ci.yml
中加入這幾行,就可以利用 pnpm 來安裝、快取相依性
.gitlab-ci.yml
stages:
- build
build:
stage: build
image: node:14.16.0-buster
before_script:
- corepack enable
- corepack prepare pnpm@latest-7 --activate
- pnpm config set store-dir .pnpm-store
script:
- pnpm install # install dependencies
cache:
key:
files:
- pnpm-lock.yaml
paths:
- .pnpm-store
Bitbucket Pipelines
使用 pnpm 安裝、快取相依性的方法如下:
.bitbucket-pipelines.yml
definitions:
caches:
pnpm: $BITBUCKET_CLONE_DIR/.pnpm-store
pipelines:
pull-requests:
"**":
- step:
name: Build and test
image: node:14.16.0
script:
- corepack enable
- corepack prepare pnpm@latest-7 --activate
- pnpm install
- pnpm run build # Replace with your build/test…etc. commands
caches:
- pnpm
Azure Pipelines
對於 Azure Pipelines,在 azure-pipelines.yml
中加入這幾行,就可以利用 pnpm 來安裝、快取相依性
azure-pipelines.yml
variables:
pnpm_config_cache: $(Pipeline.Workspace)/.pnpm-store
steps:
- task: Cache@2
inputs:
key: 'pnpm | "$(Agent.OS)" | pnpm-lock.yaml'
path: $(pnpm_config_cache)
displayName: Cache pnpm
- script: |
corepack enable
corepack prepare pnpm@latest-7 --activate
pnpm config set store-dir $(pnpm_config_cache)
displayName: "Setup pnpm"
- script: |
pnpm install
pnpm run build
displayName: "pnpm install and build"
CircleCI
對於 CircleCI,在 .circleci/config.yml
中加入這幾行,就可以利用 pnpm 來安裝、快取相依性
.circleci/config.yml
version: 2.1
jobs:
build: # this can be any name you choose
docker:
- image: node:18
resource_class: large
parallelism: 10
steps:
- checkout
- restore_cache:
name: Restore pnpm Package Cache
keys:
- pnpm-packages-{{ checksum "pnpm-lock.yaml" }}
- run:
name: Install pnpm package manager
command: |
corepack enable
corepack prepare pnpm@latest-7 --activate
- run:
name: Install Dependencies
command: |
pnpm install
- save_cache:
name: Save pnpm Package Cache
key: pnpm-packages-{{ checksum "pnpm-lock.yaml" }}
paths:
- node_modules
Jenkins
使用 pnpm 安裝、快取相依性的方法如下:
pipeline {
agent {
docker {
image 'node:lts-bullseye-slim'
args '-p 3000:3000'
}
}
stages {
stage('Build') {
steps {
sh 'corepack enable'
sh 'corepack prepare pnpm@latest-7 --activate'
sh 'pnpm install'
}
}
}
}