Automatically deploy Nodejs apps with Jenkins

Source: Internet
Author: User
Tags git client

It has been the experience of friends who presumably deployed Nodejs applications:

1. Login to server 2 via SSH, enter Nodejs directory 3, execute: Git pull command to fetch the latest code 4, execute: NPM Install new Dependency 5, execute: PM2 Restart all restart all Nodejs processes if we only have one machine, And the update is not very frequent, it seems that such an operation is acceptable, but if we have 4 Nodejs application server, each code change will be a disaster! Fortunately we have Jenkins to help us do these things, Jenkins is a tool that automates remote deployment, executes remote scripting commands, and is called a continuous integration tool, and we can quickly build a Jenkins service through Docker. First, install Docker, pull images, start container under CENTOS7, only need to execute
Yum Install Dockerservice Docker startps-ef|grep Docker

Under CENTOS6, separate execution is required

Yum install Sudosudo yum install-y epel-releasesudo yum install-y docker-ioservice Docker Startps-ef|grep Docker
Official Docker image address to Jenkins: https://registry.hub.docker.com/_/jenkins/then follow the instructions:
Docker Pull Jenkins

After pulling the image, we create the directory and start the Docker container

Mkdir/var/jenkins_homedocker run-d--name myjenkins-p 49001:8080-v/var/jenkins_home:/var/jenkins_home Jenkins

Second, install Nginx installation and start Nginx, put the following code in the configuration file, add a server node, reverse proxy to Docker
Upstream app {    server 127.0.0.1:49001;} server {    listen;    server_name jenkins.your-domain.com;    Location/{        proxy_pass Http://app;    }}

This opens the browser so we can see the first page of Jenkins.

Third, configuration jinkens we first need to enter the Jinkens add user, here is not tired, according to the hint step by step can be completed. Then we have to install the plug-in, go to: System Management, plug-in management, Advanced (tab), the bottom of the immediately get button wait a moment, you can update the available plug-in, and then we choose to install the following plug-ins:
GIT client plugingit plugingit server Plugingithub API plugingithub pluginssh Credentials pluginssh pluginpublish over SSH

After the installation is complete, restart Jenkins. If you cannot restart automatically, you can manually restart it by entering '/restart ' in the Address bar.

Iv. Add remote computer We Jenkins is installed in Docker, so even if deployed to this machine, for Jenkins is also remote external machine, need to use SSH to connect, we need to add SSH host, to let Jenkins join. Go to System management and system setup such as adding remote server, this is the server that increases the SSH remotes plugin is the remotely server five, which increases the files of the long-distance copy. I've built a Nodejs Hellowo on GitHub. Rld Project: Https://github.com/DoubleSpout/jenkinsNodejs We need to create a corresponding project in Jenkins, click: New--Named Nodejspublish to enter the project, Click on the configuration, we can configure this project, let it automatically deploy 1, set the GIT repository path: for example, because it is a public warehouse, so do not need to verify the user name, password: 2, after the build, the code is packaged save we first, add a completed shell script execution and then some like script, Package the code, then move to the workspace directory 3, then we need to connect to the deployment server, the original service temporarily paused, delete the last deployment of the compressed package, choose to add remote shell script execution, such as: write the following script, the PM2 service stopped, Here you can suspend the script according to their own requirements 4, finally we have to send the packaged code to the server, and then perform the installation of the dependent package operation, and start the Nodejs service, choose to send the file via SSH and execute the command to the remote server, the source files The default current path is Workspaces,remote directory's current path is the previous add server settings, here I set to/var/nodejs the following command is very simple, unzip the code, perform a dependent package installation, and then start the Nodejs service through PM2.
5, start to build the configuration of the above command, we click on Save, and then click Build Now, the blue Ball appears, the building is successful, the red ball indicates the build failed, the yellow ball indicates that the execution of the build command failed, for example, is being built and published: Regardless of build success or failure, We can see the results of the build by looking at the console output: If we have more than one device, we only need to configure it once, and then each time we have code changes, manually click the build immediately to automatically deploy to the target server, eliminating the repetitive work of our withered grass. Of course, other languages, such as python,php, can be deployed in this way.

Automatically deploy Nodejs apps with Jenkins

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.