node.js - 如何从 Jenkins 将 Node 应用程序部署到远程主机?

标签 node.js jenkins deployment gitlab

这是 Node 应用目录根目录下的 Jenkins 文件:

pipeline {
    agent any
    triggers {
        pollSCM('* * * * *')
    }
    stages {
        stage("deploy") {
            steps {
            sh "scp"
            }
        }
    } 
}

我将 Jenkins 配置为连接到远程 gitlab node proj repo 以检查 Node 项目以及 Jenkinsfile 并运行项目的 Jenkinsfile。这部分工作正常,但是现在要执行什么操作(请注意,Jenkins 服务器和运行 Node js 的服务器以及 gitlab repo 都是相互远程的):

run these commands on remote server on which node app is running

cd ~/mynodeproj 

pm2 stop mynodeproj 

copy project source files from Jenkins server to remote server where 
node app is running 

npm install 

export NODE_ENV=production 

pm2 start mynodeproj

如何做到这一点?

我是否需要在运行 jenkins 的服务器上设置私钥/公钥对,以便 jenkins 服务器可以执行 scp 将文件复制到运行 Node 应用程序的远程服务器?

最佳答案

我建议我们可以使用 Rocketeer对于这种情况。

这是 Rocketeer 在我的 Jenkins 服务器上用于 NodeJS 应用程序的树

$ tree .rocketeer/
.rocketeer/
├── config.php
├── hooks.php
├── logs
│   ├── develop--20170613.log
│   ├── develop--20170614.log
│   ├── develop--20170616.log
│   ├── staging--20180323.log
│   ├── staging--20180324.log
│   ├── staging--20180326.log
│   ├── production--20180223.log
│   ├── production--20180226.log
│   ├── production--20180227.log
│   ├── production--20180227.log
│   └── custom-environment--20180328.log
├── paths.php
├── remote.php
├── scm.php
├── stages.php
└── strategies.php
  • 您可以管理 NodeJS App 的远程环境:Develop、Staging、Production(在 config.php 文件中)
  • 它将在您的 Gitlab 上提取最新的源代码并保持发布版本,如 Capistrano(在 remote.php 文件中)
  • 它可以在部署最新的源代码(在 hooks.php 文件)后运行你的 pm2 命令行
  • 它已经可以帮助运行 npm install NodeJS 包了。

这是我的 Jenkins 工作设置:

源代码管理

enter image description here

构建触发器

enter image description here

构建

enter image description here

#!/bin/bash -el
cd $JENKINS_HOME/app-deploy/app-socketio
rocketeer deploy --on="develop"

develop 表示连接到Develop Remote Server(在.rocketeer\config.php 文件中)

'connections'      => [
    'develop' => [
        'host'      => '35.xx.xx.xx',
        'username'  => 'ec2-user',
        'password'  => '',
        'key'       => '/var/lib/jenkins/.ssh/foo.pem',
        'keyphrase' => '',
        'agent'     => '',
        'db_role'   => true,
    ],
    'staging' => [
        'host'      => '34.xx.xx.xx',
        'username'  => 'ec2-user',
        'password'  => '',
        'key'       => '/var/lib/jenkins/.ssh/bar.pem',
        'keyphrase' => '',
        'agent'     => '',
        'db_role'   => true,

    ],
    'production' => [
        'host'      => '18.xx.xx.xx:63612',
        'username'  => 'ec2-user',
        'password'  => '',
        'key'       => '/var/lib/jenkins/.ssh/woot.pem',
        'keyphrase' => '',
        'agent'     => '',
        'db_role'   => true,
    ],
    'custom-environment' => [
        'host'      => '13.xx.xx.xx:63612',
        'username'  => 'ec2-user',
        'password'  => '',
        'key'       => '/var/lib/jenkins/.ssh/test.pem',
        'keyphrase' => '',
        'agent'     => '',
        'db_role'   => true,
    ],
],

并在 hooks.php 文件中运行 pm2 命令行配置

'after'  => [
    'setup'   => [],
    'deploy'  => [
            "pm2 delete mynodeproj", //Delete old pm2 task
            "pm2 start src/mynodeproj.js", //Start new mynodeproj
    ],
    'cleanup' => [],
],

希望对你有帮助!!

关于node.js - 如何从 Jenkins 将 Node 应用程序部署到远程主机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49801472/

相关文章:

jenkins - 在 Jenkins 中聚合下游参数化作业的结果

jenkins - 执行脚本时出现错误 ssh 异常 auth 在 jenkins 中失败

amazon-web-services - Amazon AWS 和 MS Azure 上的 Elasticsearch 跨集群复制 (CCR)

azure - 使用 Bitbucket 管道自动将静态网站部署到 Azure 中

javascript - 用于跟踪用户分数历史记录的数据库表

node.js - POST 请求 ReactJS 后列表项未呈现

node.js - 执行 https 请求时出现 CERT_UNTRUSTED 错误

jenkins - 通过构建参数将环境变量传递到管道节点

c# - 在一个解决方案中部署多个 Web 应用程序的最简单方法是什么?

javascript - Node.js 重击 :/usr/local/bin/node: Permission denied