twitter


I have to say that I am only in the mood to write my own code or article in my spare time on weekends.

preface


The last article talked about how to use Docker to deploy applications, but in fact, Docker is not necessary in the process of automatic deployment, and you can even build a good environment on the server, and then run the commands used by your deployment project through the automatic deployment tool. Therefore, the presence or absence of Docker does not affect automatic deployment, but is more convenient.

In this scenario I used Jenkins to correlate Github and implement a series of steps for automated deployment.

Jenkins is a continuous integration (CI) tool written in the Java language for building projects continuously and automatically.

It is possible to write a program to replace Jenkins, but to get an early taste of the fun of automated deployment, I will use Jenkins directly.

The body of the


First, we need to sort out the process

  1. The code was uploaded to Github, where Jenkins detected a change in the code and downloaded it.
  2. Once the code is downloaded, Jenkins automatically downloads the dependency files needed for the project and packages the code.
  3. With the Dockerfile we defined earlier, execute the Docker build command to put the packaged file into the container.
  4. If the deployment is successful, the test for accessing the listening port is successful.

Now start setting up the environment required for automation.

I. Jenkins installation

Install the JDK first, because Jenkins runs on JDK. Skip this step if you already have a JDK.

$ sudo apt update
$ sudo apt install openjdk-8-jdk
Copy the code

Then add the Jenkins repository. Here I’m using Ubuntu, so I’m adding the Debian repository. First add the key.

$ wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
Copy the code

Then add the repository

$ sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
Copy the code

Once the key and repository are added, Jenkins can be downloaded

$ sudo apt update
$ sudo apt install jenkins
Copy the code

After downloading Jenkins successfully, use this command to judge whether Jenkins has downloaded Jenkins successfully and is running normally.

$ systemctl status jenkins
Copy the code

At this time, we can access Jenkins’ page by accessing port 8080 of the server. If it cannot be opened, we can check whether port 8080 is open.

Github configuration

After Jenkins is configured, we need Jenkins to detect code changes on Github. Webhooks are the only way to do this.

First, we go to github -> Setting -> developers -> Personal Access Token page to generate a user with read and write permission.

This token needs to be written down because it will not be visible and will be used later.

Then create the project we need to deploy on Github and go to the Settings page to set up webHooks.

Server IP + port + Github -webhook.

Jenkins connected with Github

Now back to Jenkins, first we associate Jenkins with Github, and configure github information according to the path: System Management -> System Settings -> Github -> Add Github Server

The content in the override Hook URL is the address we just filled in on Github.

After connecting to Github, we created a project on Jenkins and chose to build a free-style project. Then fill in the GitHub project address in General.

Next, select the source Control TAB and fill in the details.

With all the information configured, we begin the process of configuring the build. Select the build trigger TAB and check the following:

Jenkins installs the NodeJS environment

Finally, we configure the Jenkins auto-build command:

Fourth, try to build

Once everything is configured, click Save and apply. Then we modify the code and push it onto the Master branch, check the Jenkins page, check the build queue or the Build history panel, and you can see the build process.

conclusion

So far, the solution has completed the whole process, but there are still many shortcomings of this solution, such as whether the process of NPM install can be executed in docker during the build process. Deployment automation solution, of course, now there are more mature than that of the server if ali cloud or the cloud of tencent, for example, can choose their container services, there are more concise, friendly, configuration, and can avoid the plan needs to be deleted in the original container can I create a new container, Unfortunately, after I had finished the configuration, I found out that the company’s automatic deployment used Tencent cloud configuration to avoid such tedious configuration.

At the same time, in the process of learning Docker and Jenkins, I also referred to many great articles, such as a set of real front-end development environment building + sustainable integration + automated deployment practice. It was these former articles that prevented us from repeating the process of stepping on the pit.

Finally, if there are any shortcomings in the article, please point them out below 🙂