温馨提示×

Debian中js项目如何持续集成

小樊
31
2025-12-26 21:36:21
栏目: 编程语言

在 Debian 上为 JavaScript 项目搭建持续集成

一 环境与基础准备

  • 安装运行时与包管理器:在 Debian 上执行 sudo apt update && sudo apt install -y git nodejs npm;如需更快依赖管理,可 sudo npm install -g yarn
  • 初始化仓库与规范:git init;添加 .gitignore(排除 node_modules/dist/、.env 等);在 package.json 中约定脚本(如 “test”“lint”“build”)。
  • 版本一致性:使用 nvm 管理 Node,创建 .nvmrc 统一版本;团队协作时锁定依赖(package-lock.jsonyarn.lock)。
  • 质量与安全:接入 ESLint/Prettier 做静态检查与格式化;用 npm auditSnyk 扫描依赖漏洞。

二 选择 CI 工具与最小示例

  • GitHub Actions(托管在 GitHub 时优先):在仓库根目录创建 .github/workflows/ci.yml,示例:
    name: CI
    on: push: branches: [main]; pull_request: branches: [main]
    jobs:
    build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - uses: actions/setup-node@v4
    with: node-version: ‘18’
    - run: npm ci
    - run: npm run lint
    - run: npm test – --ci
    - run: npm run build
  • GitLab CI/CD(托管在 GitLab 时优先):在项目根目录创建 .gitlab-ci.yml,示例:
    stages: [build, test, deploy]
    cache:
    paths: [node_modules/, .npm/]
    build:
    stage: build
    script: npm ci && npm run build
    artifacts: paths: [dist/]
    test:
    stage: test
    script: npm test – --ci
    deploy:
    stage: deploy
    script: |
    which ssh-agent || (apt-get update -y && apt-get install -y openssh-client)
    eval $(ssh-agent -s)
    echo “$SSH_PRIVATE_KEY” | tr -d ‘\r’ | ssh-add -
    mkdir -p ~/.ssh && chmod 700 ~/.ssh
    ssh-keyscan your-production-server >> ~/.ssh/known_hosts
    scp -r dist/* user@your-production-server:/var/www/html
    only: [main]
  • Jenkins(自托管与复杂流水线):安装 openjdk-11-jdk 与 Jenkins;安装插件(Git、NodeJS、Pipeline);创建 Pipeline Job,使用 Jenkinsfile
    pipeline {
    agent any
    tools { nodejs ‘node-18’ }
    stages {
    stage(‘Checkout’) { steps { git ‘https://github.com/your/repo.git’ } }
    stage(‘Install’) { steps { sh ‘npm ci’ } }
    stage(‘Lint’) { steps { sh ‘npm run lint’ } }
    stage(‘Test’) { steps { sh ‘npm test – --ci’ } }
    stage(‘Build’) { steps { sh ‘npm run build’ } }
    stage(‘Deploy’) { when { branch ‘main’ } steps { sh ‘rsync -av dist/ user@host:/var/www/app && ssh user@host “pm2 restart app”’ } }
    }
    }
    上述示例覆盖了触发条件、缓存、产物、SSH 部署与通知等关键环节,可直接按需裁剪。

三 提升稳定性与安全

  • 缓存加速:缓存 node_modules.npm 以缩短安装耗时(GitHub Actions 使用 actions/cache,GitLab CI 使用 cache: paths)。
  • 环境一致性:用 actions/setup-node 指定 Node 版本,或在自托管 Runner 使用 Docker 镜像(如 node:18) 统一环境。
  • 质量门禁:在 CI 中强制执行 linttestbuild,必要时设置“仅当所有检查通过才允许合并”。
  • 依赖安全:在 CI 中加入 npm auditSnyk 步骤,发现问题即阻断合并。
  • 密钥与变量:将 SSH 私钥API Key服务器地址等放入 GitHub Secrets / GitLab CI Variables / Jenkins Credentials,严禁硬编码。

四 部署与运行

  • 进程管理:生产环境建议使用 PM2 守护进程(npm install -g pm2;pm2 start app.js 或 pm2 start ecosystem.config.js --env production)。
  • 反向代理:使用 Nginx 暴露 80/443 并转发到 Node 服务(proxy_pass http://localhost:3000),配置完成后重启 Nginx。
  • 零停机发布:在 CI 中通过 PM2 reloadRSYNC + 重启 完成发布;如需回滚,保留上一版本产物或镜像标签。
  • 容器化可选:编写 Dockerfile(FROM node:18-alpine;WORKDIR /app;COPY package*.json ./;RUN npm ci --only=production;COPY . .;CMD [“npm”,“start”]),在 CI 中构建并推送镜像,目标主机拉取并启动。

0