I talked about packaging and deployment of front and back end projects through Gitlab-CI before, but in the new company, Jenkins is used to complete continuous integration and continuous deployment process. Therefore, this article is about installing Jenkins service on the server through Docker and configuring Jenkins so that the front and back ends of Node can be packaged and deployed to another server (generally, packaging and deployment are in different servers to avoid mutual influence, etc.).

First of all, you need two servers. If the packaged services are deployed on one server, that’s fine, but not recommended.

One 2-core 4G is used to deploy Jenkins service and one 1-core 2G is used to deploy packaged service.

Install Docker

The Linux server uses curl to download shell scripts for quick installation

curl -fsSL get.docker.com -o get-docker.sh
Copy the code

Once the download is complete, you can view it using the ls command. If it already exists, run the sh command to execute the script

sh get-docker.sh
Copy the code

Notice If you are not the root user, run sudo su to obtain the super administrator permission.

After the installation is complete, start Docker Server

systemctl start docker
Copy the code

Using the docker version command, you can see that the Client and Server are successfully started.

Install Jenkins using Docker

1. Pull Jenkins mirror image

Jenkinsci/BlueOcean is recommended.

docker pull jenkinsci/blueocean
Copy the code

2. Start the Jenkins service

docker run -d --name jenkins -p 8080:8080 -p 50000:50000 jenkinsci/blueocean
Copy the code

After startup, you can view the service status through Docker PS.

Here also say, in the cloud server firewall to open port 8080 to access oh.

Then you can access our Jenkins service via the server IP: port 8080.

3. Configure and initialize Jenkins

Obtain the initialization password

Enter the container via Docker execit Jenkins bash

Then the cat/var/jenkins_home/secrets/initialAdminPassword view the initialization code

Installing recommended plug-ins

Then go to the install plug-in page and install the recommended plug-in

Create a user

The next step is to create an administrator user

Next, select jenkinsurl address, and then Jenkins will be restarted. After the reboot, you will officially enter the interface.

3. Configure Jenkins to package the front-end and back-end services of Node

1. Install the plug-in

Two plug-ins need to be installed in

Home > System > Plug-in Management > Optional Plug-ins > Search Select NodeJS > Restart the service after installation

  • NodeJs: The environment required when the code is packaged
  • Publish Over SSHWhen the package is complete, it is sent to the remote server via this plug-in SSH connection

Publish Over SSH has been removed for security reasons and cannot be found in plugin manager.

We downloaded it through the open source software mirror of Tsinghua University.

Search for downloads inside

  • publish-over-ssh.hpi
  • jsch.hpi
  • publish-over.hpi

Because publish-over-ssh relies on the following two plug-ins, you need to download them together. After downloading the plug-in, choose System > Plug-in Management > Advanced > Upload the plug-in

Upload it and reboot Jenkins.

2. Configure the NodeJS plug-in version

This step is done because sometimes the installation package fails with the latest version. It is recommended to use the latest stable version, which I used here as version 16.13.1. in

Home > System > Global Tools Tool Configuration > NodeJS > Added

Fill in the alias and select the NodeJS version.

3. Create a project task to implement construction and deployment

(1) Home > New Task > Build a free style software project

(2) Git source management

The configuration step is for Jenkins to go to a Git repository and pull the code.

  • Repository URL: gitWarehouse address
  • Credentials: connectiongitThe authentication mode of the warehouse is generally usedUsername with passwordorSecret TextThe former is the user name and password, the latter is the need to generatetokenKey, connected by key.
  • Branches to build: Specifies the pull branch
(3) Specify the build environment

Build environment select the node version we configured earlier.

(4) Build script

Build > Add Build Steps > Execute shell

Enter the build command as shown in the figure above, first use NPM to package the build, and then compress the package to facilitate the transmission of remote services.

After this step is configured and the package construction is complete, you can go to the /var/jenkins_home/workspace directory in the container to see what happens after the package.

(5) Send the packaged package to the remote server for deployment

SSH connection configuration of Publish over SSH under home configuration.

On the home page, choose System > System Configuration > Publish Over SSH

Enter these configuration items, mainly Hostname and Username, check Use Password Authentication, or Use a different key, and then enter password.

There is a Test Configuration to Test whether the connection is successful.

Returns the configuration of the project you just created.

Operate selection after build

And then configure it

  • Source files: the source file, which is the file we just compressed,dist.tar.gz
  • Remove prefixThe prefix to remove is the prefix before the compressed file is removeddistFolder, otherwise moved to the remote server after also havedistfolder
  • Exec command: executed after the file is moved to a remote servershellCommand, this is probably empty the previous project folder under all files, and then unzipdist.tar.gzGo to the project folder and delete it at lastdist.tar.gzCompressed package

At this point we completed all the steps, from completing the Jenkins service with Docker, configuring Jenkins to package and build our project, and finally SSH connection to the remote server to complete the code deployment. We can actually use Docker to deploy our service, which we can discuss later if necessary. Here, if there is any do not understand the small partners can comment on the comment area ha ~!

Recommend my front-end engineering related article 😄

# Build a complete set of automated CI and CD processes based on Docker (Gitlab, Gitlab Runner) to complete the process from code submission to automatic packaging and compilation to automatic deployment and operation

# Build Npm private library based on Docker + Verdaccio, realize the release and download of Npm package on the private library

# Install a static server based on Docker Nginx, and configure SSL certificate to enable HTTPS access