如何从 Jenkins 将节点应用程序部署到远程主机?

2024-01-03

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

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

我将 Jenkins 配置为连接到远程 gitlab Node Proj 存储库,以检出节点项目以及 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 将文件复制到运行节点应用程序的远程服务器?


我建议我们可以使用火箭手 https://i.stack.imgur.com/1pJsF.png对于这个案例。

Here is 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
  • 您可以管理 Node JS 应用程序的远程环境:开发、暂存、生产(位于config.php file)
  • 它将在您的 Gitlab 上提取最新的源代码并保留发布版本,例如Capistrano (at remote.php file)
  • 它可以运行你的pm2 command line部署最新源代码后(位于hooks.php file)
  • 已经可以帮助运行了npm installNodeJS 包。

这是我的 Jenkins 工作设置:

源代码管理

构建触发器

Build

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

develop意味着连接到开发远程服务器(位于.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,
    ],
],

And run pm2命令行配置在hooks.php file

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

希望这可以帮到你!!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 Jenkins 将节点应用程序部署到远程主机? 的相关文章

随机推荐