preface

How did we get code deployed before ci&CD-related software came along? Manually go to the server to create a directory, local packaging compression, SCP upload to the specified server directory, manual decompression of the package such as a series of complicated steps like a tiger operation. Modifying a punctuation mark has to go through such a tedious operation again, and once there is an error in a link, it can not locate the problem for half a day. The test environment is ok, and if it is deployed online, it can be imagined that it will bring much trouble and consequences. Times are changing, we’re evolving, we’re trying to make our code building process simple, efficient and reliable, and Jenkins is a great invention.

What is Jenkins?

  1. Jenkins is an open source CI&CD software for automating a variety of tasks, including building, testing, and deploying software.
  2. Jenkins supports a variety of operations, either through system packages, Docker, or through a standalone Java program.

Flow chart:

The functionality to be implemented next

  • Git Hook functionality
  • Webhook automated deployment
  • Deploy mail sending
  • The deployment information is sent to the spike group through the spike robot, and the relevant personnel are notified

preparation

  • A cloud server (your own computer can also access the Internet on the line)
  • Linux Basic Commands
  • Git Basic commands
  • nginx
  • jenkins
  • Making project

Jenkins installation and startup

Install the Java

Jenkins relies on Java, which you will need to install if you don’t have it installed on your system.

yum install java
Copy the code

Viewing the Java Version

java -version
Copy the code

If the following information is displayed, the installation is successful.

Version "1.8.0_161" OpenJDK Runtime Environment (build 1.8.0_161-b14) OpenJDK 64-bit Server VM (build 25.161 - b14, mixed mode)Copy the code

Install Jenkins

Get Jenkins dependency library

sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
Copy the code

Check out the Jenkins installation directory

rpm -ql jenkins

/etc/init.d/jenkins
/etc/logrotate.d/jenkins
/etc/sysconfig/jenkins
/usr/lib/jenkins
/usr/lib/jenkins/jenkins.war
/usr/sbin/rcjenkins
/var/cache/jenkins
/var/lib/jenkins
/var/log/jenkins

Copy the code

View Jenkins Port

cat /etc/sysconfig/jenkins | more
Copy the code

The default Jenkins port is 8080. If the cloud host is used to install Jenkins, 8080 access port needs to be added to the console security group. I use the default port to access Jenkins.

Start the Jenkins

sysctemctl start jenkins
Copy the code

Visit Jenkins

Open the browser and enter IP :8080. The following interface appears:

View password

cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the code

Choose to install the custom plug-in

Plug-in installation (the initial plug-in installation is a bit slow, about 20 minutes)

Create account

After saving, enter the next page and click Ready

Create a task

  1. Click New Task

  1. Give the task a name

Git Hook functionality

Desired result: After the local push to Github is successful, Jenkins automatically starts building the deployment.

Thought: How did Jenkins know we had submitted code to the Github repository?

  1. Open the task, add the github remote repository address, and configure the login name and password.

2. Install the Generic Webhook Trigger Plugin

System Management -> Plug-in Management -> Filter (Optional plug-in)

3. Add a trigger

The plug-in can be customized to suit its own needs, such as if I submit code for a Test1 branch, need to perform unit tests, and then build and merge on another Test2 branch. When I commit the code for the master branch, I do nothing but deploy and build. My side of the default selection of the time to submit the master branch code, automatic deployment.

4. Configure Webhook for github repository

process

  • Jenkins webhook address on GitHub

  • Create an Access token on GitHub. Jenkins will use this access token to authenticate some operations that require permission.
  • Jenkins install GitHub Plugin;
  • Jenkins configure GitHub access permission;

  • Url is Jenkins’ startup IP address
  • Content Type Select Application/JSON
  • Secret added the hook password for Jenkins

5. Test hooks

Native push test code

Jenkins automatically triggers the build

Build successful, view workspace, code deployment update successful.

Jenkins completes static packing

Usually we do this by using NPM Script and the Webpack plugin. Once the git push hook is triggered, how does Jenkins implement a set of processes that we package locally?

There are generally three steps:

  • npm install
  • npm run test
  • npm run build
  1. First of all, just like running NPM script locally, we need to configure the node environment in Jenkins first. We can import Node by configuring environment variables or install plug-ins. Here we use plug-ins. Install the NVM Wrapper plugin.

Run the build in an NVM managed Environment. Run the build in an NVM managed Environment

3. Add a build shell

Shell execution order

  • Set NPM image. Here I choose the image of Taobao. You can also choose the official image of NPM
  • NPM install Installs dependencies
  • Execute the NPM run build package

4. Execute local push, Jenkins automatically execute script to build static.

Automatic remote server deployment is implemented using SSH

  1. Publish Over SSH: Publish Over SSH: Publish Over SSH: Publish Over SSH: Publish Over SSH
  2. Create a folder on the server where the code will be deployed to receive the code from Jenkins. I created a Jenkins folder on the server.
  3. If Jenkins wants to deploy code to the server, you have to log in to the server. There are two ways to log in to the server. One is SSH authentication, and the other is password authentication. Publish over SSH in system Settings.

Key parameters:

Passphrase: SSH Servers: Path to the key file (private key) key: Copy the private key to this box SSH Server Name (whatever you choose) Hostname specifies the Name or IP address of the SSH Server to be connected to. Remote directory (path to the testJenkins folder created in step 2 above)Copy the code

4. According to the above steps, add two packing commands to compress the dist directory generated on the Jenkins server, so as to upload it to the newly created Jenkins directory on the server.

cd dist
tar -zcvf dist.tar.gz *
Copy the code

5. Click Post-Build Operation, add post-build operation steps, and select Send Build Artificial over SSH

Name: select an SSH server that you have configured. Source files: Write the path of the file that you want to transfer. Remove prefix: Delete the prefix that you want to Remove. Exec Command: SSH Servers Exec Command: SSH Servers Exec Command: SSH Servers Exec commandCopy the code

6. We modify the test code locally and push it. After Jenkins completes automatic packaging, we go to the newly created Jenkins directory on the server and find that the directory on the server has been updated to the latest.

So far, we have completed the whole process of automatic construction and deployment through Webhook, Github and SSh with Jenkins.

Mail is sent after build

What if we, as development or testing students, want to know the results of each build in a timely manner when a build fails or stops abnormally during the above mentioned build process?

Here, I used the Extended E-mail Notification plug-in with stronger customization. I also need to install this plug-in before using it, and then restart Jenkins.

1. System Administration > System Configuration >Extended Email Notification. Before configuring this, you must configure Jenkins Location system administrator email address; otherwise, sending email will not take effect. In addition, there is a small hole, fill in the mailbox must first confirm whether open through SMTP service, if not also can not send mail!!

2. Open the created onewebapck4-constructTask, add post-build actions to configuration

3. We launched a Git push locally again, Jenkins automatically completed the build deployment and sent an email to our mailbox.

I’m using a custom mail format here, and the official mail format is too simple to locate build problems.

Send build messages to the group via the pin bot

1. Create a group and add a robot

2. Get the custom robot Webhook

3. Install dingTalk plug-in

4. Open the build task, genral configuration

Enter the notification person, the notification content can also be customized.

5. Build the test successfully

The end of the

1. Through the small case mentioned above, we simply realized a set of workflow for automatic construction. As long as local git push is initiated, the cloud server can realize automatic construction and deployment.

2. Our actual working scene is much more complicated than CL in the above case. Of course, no matter how complicated CL process is, as long as we master and understand Jenkins’ basic concept, we believe that as long as we do more work and think more, Everything will become simple.

3. Jenkins also has a lot of great features like Pipeline, Slave, and a few plugins you wouldn’t expect. Later, more complex Cl scenarios will be attempted by writing Pipeline scripts.