这是 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 工作设置:
源代码管理
构建触发器
构建
#!/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/