在Debian下进行JavaScript项目的持续集成,可按以下步骤操作,以GitHub Actions为例:
安装Node.js环境
确保系统已安装Node.js和npm,可通过以下命令安装:
sudo apt update
sudo apt install nodejs npm
初始化项目
在项目根目录执行:
git init
npm init -y
创建GitHub仓库
将项目推送到GitHub,确保包含.gitignore文件(忽略node_modules/等目录)。
添加CI配置文件
在项目根目录创建.github/workflows/ci.yml,内容示例:
name: CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm install
- run: npm test # 执行单元测试(需提前配置Jest等框架)
- run: npm run lint # 代码质量检查(如ESLint)
配置npm脚本
在package.json中添加测试、构建等脚本:
"scripts": {
"test": "jest",
"lint": "eslint .",
"build": "webpack --mode production"
}
Freestyle project或Pipeline任务。npm install && npm test)。.gitlab-ci.yml文件,定义build、test等阶段。stages:
- test
test:
stage: test
script:
- npm install
- npm test
提交代码触发CI
推送代码到GitHub/GitLab,进入对应平台的CI页面查看构建日志,确保测试通过。
扩展功能
nyc工具)。