乐闻世界logo
搜索文章和话题

How to use pnpm in diferent Gitlab CI stages

1个答案

1

When using GitLab CI/CD, pnpm (Performant npm) can be integrated into different stages to optimize the build and deployment processes. Below are the steps and examples for utilizing pnpm across various stages of GitLab CI:

1. Setup Stage: Installing pnpm

In the GitLab CI configuration file .gitlab-ci.yml, you can set up an initialization stage to install pnpm. Because pnpm efficiently manages dependencies and caching, it enhances the speed of subsequent steps.

yaml
stages: - setup - build - test - deploy install_pnpm: stage: setup image: node:latest script: - npm install -g pnpm cache: key: ${CI_COMMIT_REF_SLUG} paths: - .pnpm-store

In this stage, we use the official Node image and globally install pnpm. Additionally, we configure caching to store the pnpm store, reducing download time for subsequent steps.

2. Build Stage: Installing Dependencies and Building

In the build stage, we use pnpm to install all required dependencies and execute the build script.

yaml
build_app: stage: build image: node:latest script: - pnpm install --frozen-lockfile - pnpm run build cache: key: ${CI_COMMIT_REF_SLUG} paths: - .pnpm-store - node_modules/ artifacts: paths: - build/ expire_in: 1 hour

Additionally, we cache the node_modules directory to accelerate subsequent steps and configure the build artifacts for preservation.

3. Test Stage: Running Tests with pnpm

In the test stage, we use pnpm to execute the test script.

yaml
test_app: stage: test image: node:latest script: - pnpm install - pnpm test cache: key: ${CI_COMMIT_REF_SLUG} paths: - .pnpm-store - node_modules/ artifacts: when: always reports: junit: test-results.xml

Here, in addition to installing dependencies and running tests, we generate test reports. Using the reports option within artifacts exports test results in JUnit format, facilitating visualization of test reports in GitLab CI.

4. Deployment Stage: Deploying with pnpm

Finally, in the deployment stage, pnpm can be used to execute the deployment script.

yaml
deploy_app: stage: deploy image: node:latest script: - pnpm install --production - pnpm run deploy cache: paths: - .pnpm-store - node_modules/

During deployment, pnpm install --production is used to install only production dependencies, which reduces the size of the deployment package and enhances deployment efficiency. Subsequently, pnpm run deploy executes the deployment process.

By appropriately using pnpm in various stages of GitLab CI, it can significantly improve the efficiency and performance of the CI/CD pipeline.

2024年6月29日 12:07 回复

你的答案