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?
You might want to consider doing this with ansible and just call your ansible playbook from: Build -> Execute Shell
https://dzone.com/articles/running-ansible-playbooks-from-jenkins
You can have ansible manage all your remote environments.
Your question is how to deploy your application to remote host from jenkins
easiest solution
1.using Jenkins File
If your using a pipeline project, then you will notice a Pipeline Syntax option, just select ssh Publisher:send artifacts over SSH (you'llneed the plugin Publish over SSH plugin)
Add an SSH server
Finally just click Generate Pipeline Script and BOOM, you get the script (it internally does an scp) just copy and paste it into your Pipeline file and your done :)
)
2.Publish over SSH
You can do the same thing( follow the above steps adding details only) in a normal Jenkins project in the Post Build Action If you don't have a Pipeline Project (helps if you don't have/know a Pipeline Project)
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?
Yes its required(Recommended) you can add the deatils as shown below in Jenkins under Manage Jenkins, Configure Jenkins Under Publish over SSH and you can test your connection .
Hope this was useful :)
I'm suggest we can use Rocketeer for this case.
Here is
Rocketeer
tree on my Jenkins Server for NodeJS Appconfig.php
file)Capistrano
(atremote.php
file)pm2 command line
after deployed newest source code (athooks.php
file)npm install
NodeJS packages.So here is my Jenkins Job setting:
Source Code Management
Build Triggers
Build
develop mean connect to Develop Remote Server (at
.rocketeer\config.php
file)And run
pm2
command line configure athooks.php
fileHope this can help you!!
Those command should be in a file part of your Git repo, meaning checked out in the workspace used by the pipeline.
From there, you can scp that script to the server:
And execute it through ssh, provided the private/public key of the remote user is deployed on the agent (or passed through the ssh-agent plugin):
The trick though, as mentioned here, is that the PATH of an ssh session is fairly limited, and might not include node, if node is not installed in a standard path like
/usr/bin
.So you might want to modify your script in order to use absolute paths for your executable (like
/absolute/path/to/node
)