发布于2026-07-13 阅读(0)
扫一扫,手机访问
在Debian上给Node.js项目搭一套持续集成(CI),方法其实不少。这里拿GitHub Actions举例,把整个流程一步步拆开讲清楚。

Debian系统上得先有这几样东西:
装起来也很直接:
sudo apt update
sudo apt install git nodejs npm
# 如果用 yarn
sudo apt install yarn
还没仓库的话,在GitHub上新建一个,然后把Node.js项目推上去就行。
在项目根目录新建 .github/workflows 文件夹,里面放一个YAML文件,比如 ci.yml:
name: CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: '14.x'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
- name: Build project
run: npm run build
- name: Deploy to production (optional)
if: github.ref == 'refs/heads/main'
run: |
# 这里填部署脚本
echo "Deploying to production..."
上面那个 node-version 字段,按项目实际需要的版本改就行。
在 package.json 里加上测试命令,比如:
{
"scripts": {
"test": "jest"
}
}
如果项目需要打包,同样在 package.json 里加上:
{
"scripts": {
"build": "webpack --mode production"
}
}
把 .github/workflows/ci.yml 提交到仓库,然后推到 main 分支:
git add .github/workflows/ci.yml
git commit -m "Add CI configuration"
git push origin main
配置文件一推送,GitHub Actions就会自动触发。去仓库的 Actions 标签页就能看到运行日志和最终结果,是红是绿一目了然。
需要在CI完成后自动部署?在 ci.yml 里加上部署步骤就行。比如用SSH连服务器:
- name: Deploy to production
if: github.ref == 'refs/heads/main'
run: |
ssh -o StrictHostKeyChecking=no ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }} 'bash -s' < deploy_script.sh
别忘了先在GitHub仓库的 Settings → Secrets 里把 DEPLOY_USER、DEPLOY_HOST 这些变量配好。
整套流程走下来,Debian上的Node.js项目就算有了一个自动化的持续集成流水线。之后每次提交代码,GitHub Actions都会自动跑测试、构建,甚至部署,省心不少。
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8