问题描述
这是节点应用目录根目录下的 Jenkins 文件:
管道{代理任何触发{pollSCM('* * * * *')}阶段{阶段(部署"){脚步 {sh "scp"}}}}
我将 Jenkins 配置为连接到远程 gitlab node proj repo 以检查节点项目以及 Jenkinsfile 并运行项目的 Jenkinsfile.这部分工作正常,但是现在要执行什么操作(请注意,Jenkins 服务器和运行节点 js 的服务器以及 gitlab repo 都是相互远程的):
在运行节点应用程序的远程服务器上运行这些命令cd ~/mynodeprojpm2 停止 mynodeproj将项目源文件从 Jenkins 服务器复制到远程服务器节点应用程序正在运行npm 安装出口 NODE_ENV=生产pm2 启动 mynodeproj
如何做到这一点?
我是否需要在运行 jenkins 的服务器上设置私钥/公钥对,以便 jenkins 服务器可以执行 scp 将文件复制到运行节点应用程序的远程服务器?
我建议我们可以使用
构建触发器
构建
#!/bin/bash -elcd $JENKINS_HOME/app-deploy/app-socketio火箭部署 --on="develop"
develop 表示连接到Develop Remote Server(在.rocketeerconfig.php
文件中)
'连接'=>['开发' =>['主机' =>'35.xx.xx.xx','用户名' =>'ec2-用户','密码' =>'','键' =>'/var/lib/jenkins/.ssh/foo.pem','关键词' =>'','代理' =>'','db_role' =>真的,],'分期' =>['主机' =>'34.xx.xx.xx','用户名' =>'ec2-用户','密码' =>'','键' =>'/var/lib/jenkins/.ssh/bar.pem','关键词' =>'','代理' =>'','db_role' =>真的,],'生产' =>['主机' =>'18.xx.xx.xx:63612','用户名' =>'ec2-用户','密码' =>'','键' =>'/var/lib/jenkins/.ssh/woot.pem','关键词' =>'','代理' =>'','db_role' =>真的,],'自定义环境' =>['主机' =>'13.xx.xx.xx:63612','用户名' =>'ec2-用户','密码' =>'','键' =>'/var/lib/jenkins/.ssh/test.pem','关键词' =>'','代理' =>'','db_role' =>真的,],],
并在 hooks.php
文件中运行 pm2
命令行配置
'after' =>['设置' =>[],部署"=>["pm2 delete mynodeproj",//删除旧的pm2任务"pm2 start src/mynodeproj.js",//启动新的mynodeproj],'清理' =>[],],
希望对你有帮助!!
This is Jenkins file in the root of node app directory:
pipeline {
agent any
triggers {
pollSCM('* * * * *')
}
stages {
stage("deploy") {
steps {
sh "scp"
}
}
}
}
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
How to achieve this?
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?
I'm suggest we can use Rocketeer for this case.
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
- 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
(atremote.php
file) - It's can run your
pm2 command line
after deployed newest source code (athooks.php
file) - It's already help run
npm install
NodeJS packages.
So here is my Jenkins Job setting:
Source Code Management
Build Triggers
Build
#!/bin/bash -el
cd $JENKINS_HOME/app-deploy/app-socketio
rocketeer deploy --on="develop"
develop mean connect to Develop Remote Server (at .rocketeerconfig.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
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 将节点应用程序部署到远程主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!