在Ubuntu上使用Node.js進行持續集成可按以下步驟操作:
使用NodeSource倉庫安裝(推薦)
sudo apt update
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - # 替換為所需版本
sudo apt install -y nodejs
node -v # 驗證安裝
使用NVM管理多版本(可選)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts # 安裝最新LTS版本
nvm use 20 # 切換版本
初始化項目
mkdir my-node-ci && cd my-node-ci
npm init -y
安裝開發依賴
npm install --save-dev jest eslint # 示例:測試和代碼檢查工具
.github/workflows/ci.yml
:name: Node.js CI
on: [push, pull_request]
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '20'
cache: 'npm'
- run: npm install
- run: npm test # 運行單元測試
- run: npm run lint # 代碼檢查
.gitlab-ci.yml
:stages:
- test
- build
test_job:
stage: test
script:
- npm install
- npm test
Jenkinsfile
定義構建步驟,例如:pipeline {
agent any
stages {
stage('Install') {
steps {
sh 'npm install'
}
}
stage('Test') {
steps {
sh 'npm test'
}
}
}
}
npm run lint
和npm test
確保代碼質量。以上步驟可根據項目需求選擇工具鏈,GitHub Actions適合輕量級項目,GitLab CI/CD適合復雜流水線,Jenkins適合企業級定制化場景。