在Debian系统下实现JavaScript项目的持续集成(CI),可通过 Jenkins 或 GitLab CI/CD 两种主流工具完成,以下是详细步骤:
确保Debian系统已安装Java(Jenkins依赖) 和 Node.js/npm(JavaScript项目构建工具):
# 安装OpenJDK 11(Jenkins推荐版本)
sudo apt update && sudo apt install -y openjdk-11-jdk
java -version # 验证Java安装
# 安装Node.js和npm(使用NodeSource仓库获取最新LTS版本)
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install -y nodejs
node -v && npm -v # 验证Node.js/npm安装
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
echo "deb http://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list
sudo apt update && sudo apt install -y jenkins
sudo systemctl enable --now jenkins
http://<服务器IP>:8080,输入 /var/lib/jenkins/secrets/initialAdminPassword 中的密码解锁。nodejs并保存。js-project-ci),点击“OK”。https://github.com/yourusername/js-project.git),若有私有仓库,需在“Credentials”中添加Git账号密码或SSH密钥。H/5 * * * * 每5分钟检查一次)或“Build when a change is pushed to GitHub”(GitHub webhook触发)。nodejs环境;在“Execute shell”中输入构建/测试命令:npm install # 安装依赖
npm test # 运行测试(需项目中配置了Jest/Mocha等测试框架)
若项目需要更规范的流水线,可在项目根目录创建Jenkinsfile(Declarative语法):
pipeline {
agent any
tools {
nodejs 'nodejs' // 关联全局配置的Node.js
}
stages {
stage('Checkout') {
steps {
git 'https://github.com/yourusername/js-project.git'
}
}
stage('Install Dependencies') {
steps {
sh 'npm install'
}
}
stage('Run Tests') {
steps {
sh 'npm test'
}
}
}
post {
always {
echo 'CI process completed.'
}
success {
echo 'Build and test succeeded!'
}
failure {
echo 'Build or test failed, please check logs.'
}
}
}
创建“Pipeline”类型任务,选择“Pipeline script from SCM”,指定仓库URL和Jenkinsfile路径(如Jenkinsfile),保存后触发流水线。
将JavaScript项目推送到GitLab仓库(如https://gitlab.com/yourusername/js-project.git),确保项目包含package.json和测试脚本(如"test": "jest")。
GitLab Runner是执行CI/CD任务的代理,需在Debian系统上安装:
# 添加GitLab Runner仓库并安装
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt install -y gitlab-runner
# 注册Runner(替换为GitLab项目中的Registration Token)
sudo gitlab-runner register \
--non-interactive \
--executor "shell" \ # 或docker(需安装Docker)
--url "https://gitlab.com/" \
--registration-token "YOUR_REGISTRATION_TOKEN" \
--description "Debian Runner for JS Project" \
--tag-list "js,linux" \
--run-untagged "false"
在项目根目录创建.gitlab-ci.yml,定义CI/CD流程(示例):
stages:
- install
- test
- deploy
variables:
NODE_ENV: "test"
install_dependencies:
stage: install
script:
- npm install
artifacts:
paths:
- node_modules/ # 缓存依赖,避免后续阶段重复安装
run_tests:
stage: test
script:
- npm test
only:
- main # 仅在main分支推送时触发
deploy_production:
stage: deploy
script:
- echo "Deploying to production server..."
- scp -r build/* user@your-production-server:/var/www/js-app # 替换为实际部署命令
only:
- main
when: manual # 手动触发部署(生产环境建议)
将.gitlab-ci.yml提交到GitLab仓库并推送:
git add .gitlab-ci.yml
git commit -m "Add GitLab CI/CD configuration"
git push origin main
GitLab会自动检测到.gitlab-ci.yml,并触发流水线。通过GitLab项目的“CI/CD” → “Pipelines”页面查看流程状态。
artifacts(GitLab)或cache(Jenkins NodeJS插件),避免每次构建重复安装node_modules。package-lock.json或yarn.lock锁定依赖版本,确保不同环境构建结果一致。--coverage选项或Istanbul,生成测试覆盖率报告并上传至Jenkins/GitLab。