在Jenkins中如何创建Pipeline实现npm install后并行运行指定npm命令
Jenkins Pipeline 配置方案
以下是满足需求的声明式Pipeline配置,你可以根据实际运行环境调整参数:
pipeline { // 可替换为你指定的运行agent,比如agent { label 'nodejs-agent' } agent any // 若使用Jenkins全局工具管理Node.js版本,取消下方注释并替换为你的Node.js工具名 // tools { // nodejs 'NodeJS-16' // } stages { // 第一步:安装依赖 stage('Install Dependencies') { steps { // 若项目不在当前工作目录,可通过dir指定项目路径 // dir('/var/jenkins_workspace/your_project') { sh 'npm install' // } } } // 第二步:并行启动两个服务 stage('Run Services') { parallel { stage('Start Main Service') { steps { // 若需要服务后台运行不阻塞流水线结束,改用 sh 'nohup npm run start > start-service.log 2>&1 &' sh 'npm run start' } } stage('Start Proxy Service') { steps { // 同理后台运行可改为 sh 'nohup npm run proxy > proxy-service.log 2>&1 &' sh 'npm run proxy' } } } } } }
注意事项
- 运行流水线的Jenkins节点需要提前配置好Node.js环境,保证npm命令可正常调用
- 如果
npm run start和npm run proxy是常驻运行的服务(如前端开发服务、接口代理服务),直接执行命令会导致流水线一直处于运行状态不会退出,需要用nohup加输出重定向的方式把进程放到后台运行,日志会写入指定的log文件方便后续排查问题 - 若你的项目代码需要从Git等代码仓库拉取,需要在Install Dependencies阶段前新增拉取代码的步骤
内容的提问来源于stack exchange,提问作者thesupremo43
相关产品推荐
相关产品推荐

