如何从Jenkins将节点应用程序部署到远程主机? [英] How to deploy node app to remote host from Jenkins?

查看:418
本文介绍了如何从Jenkins将节点应用程序部署到远程主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是节点应用程序目录根目录中的Jenkins文件:

This is Jenkins file in the root of node app directory:

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

我将Jenkins配置为与Jenkinsfile一起连接到远程gitlab节点proj存储库以检出节点项目,并运行该项目的Jenkinsfile.这部分工作正常,但是现在要执行的操作(请注意,Jenkins服务器与运行js节点的服务器以及gitlab repo都彼此遥远):

I configured Jenkins to connect to remote gitlab node proj repo to checkout node project along with Jenkinsfile and run the project's Jenkinsfile. This part works fine but what to do now to perform (note that Jenkins server and the server on which node js is running as well as gitlab repo are all remote to each other):

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将文件复制到运行节点应用程序的远程服务器上?

Do I need to setup private/public keypair on server running jenkins so that jenkins server can do scp to copy file to remote server running node app?

推荐答案

我建议我们可以使用这种情况下的火箭手.

这是我的Jenkins服务器上的Rocketeer树,用于NodeJS应用

Here is Rocketeer tree on my Jenkins Server for NodeJS App

$ 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的远程环境:开发,登台,生产(在config.php文件中)
  • 它将在您的Gitlab上获取最新的源代码,并保持发布Capistrano之类的版本(在remote.php文件中)
  • 在部署最新的源代码(在hooks.php文件中)后,可以运行您的pm2 command line
  • 运行npm install NodeJS软件包已经对您有所帮助.
    • You can manage remote environment of NodeJS App: Develop, Staging, Production (at config.php file)
    • It's will pull newest source code on your Gitlab and keep releases version like Capistrano (at remote.php file)
    • It's can run your pm2 command line after deployed newest source code (at hooks.php file)
    • It's already help run npm install NodeJS packages.
    • 这是我的詹金斯工作设置:

      So here is my Jenkins Job setting:

      源代码管理

      构建触发器

      构建

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

      开发是指连接到开发远程服务器"(位于.rocketeer\config.php文件)

      develop mean connect to Develop Remote Server (at .rocketeer\config.php file)

      '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命令行配置

      And run pm2 command line configure at hooks.php file

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

      希望这可以为您提供帮助!

      Hope this can help you!!

      这篇关于如何从Jenkins将节点应用程序部署到远程主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆