Jenkins learning

Jenkins is a tool that automates the process of building project releases, implementing code -> Github or GitLab libraries -> Jenkins deployment -> access. A tool that runs a war package or jar package or other tools on Linux without having to be repackaged. But just know roughly a specific process, there is still no overall tried, this is the work above must use Jenkins, must take the time to learn the tool, is helpful to troubleshoot problems in the follow-up work, and published online also know the whole process, work more good development deployment!

1. Download and install

Jenkins download can be directly downloaded from the official website, the way is relatively simple, the official website provides specific download and installation steps.

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

# installation
yum install jenkins -y

# start
systemctl daemon-reoload
systemctl start jenkins
If there is no problem, the status should be running
systemctl status jenkins
Copy the code

Note here, if is not installed Java environment, you can follow the instructions of the website of www.jenkins.io/doc/book/in… Let’s install the JDK together.

Trouble spots

The startup error may occur because the Java environment is not configured correctly

The solution:

Change the Java path in Jenkins configuration file
vim /etc/init.d/jenkins 
Copy the code

2. Jenkins

Later, you can directly access IP :8080 according to the document, and you can see the specific Jenkins page. It will require you to enter the password operation, which gives the specific path, so you don’t need to worry about it. You can directly obtain the password operation through the CAT path. Enter the page of plug-in installation, and click the recommended plug-in to enter the plug-in.

Add a new item, configure the item, and configure the link between github and github. If it fails to connect to Github, it is because git has not been added as a trusted user. Therefore, you need to manually execute git Clone and add trusted credentials.

2.1 Configuration Process

You need to configure a specific address. The address must be an address that can be accessed on Github. Github cannot access the VM, unless it is an Aliyun or Tencent cloud server. I am using Tencent cloud server. At this time, I need to set the web address of Webhook on Github. The main purpose is that Jenkins will rebuild the entire server once the warehouse is pushed.

2.1.1 How can Jenkins communicate with Github

This is an important consideration, because the automatic build process requires that the local code be built with the Github library. Jenkins then receives the push signal from Github via Webhook and pulls the code from Github to perform a local build task.

For the local code to communicate with Github we need to generate public and private keys and then configure them. The same is true for Jenkins. For The Github code to be pulled from Github, we need to establish the public and private keys to communicate.

First we need to generate the public and private keys:

ssh-keygen -t rsa -C "[email protected]"Generate the public and private keys cat ~/.ssh/id_rsa.pubGithub Settings => SSH and GPG keys
cat ~/.ssh/id_rsa        Write to Jenkins' configuration
Copy the code

2.1.2 configuration Jenkins

Note that you need to create a new task before configuring it. You can choose a free style and name as you wish

After configuring the General section, which is the URL of our Github project, this is where we put the project house we want to build automatically.

Click add to the red font above to see this.

Building a trigger

The content in the Add box above

Secret Token is generated

build

This is what happens when the code is pulled in, and you need to write your own script to get your project running.

2.1.3 A script to start Django

#! /bin/bash
VENV_DIR=/usr/local/src/jenkins
JENKINS_PROJECT_DIR=/var/lib/jenkins/workspace/jenkinsdemo
Script commands in the build environment
echo "Congratulations! Build Success!"
# Determine whether the process is in the process. If it is in the process, do not worry about it. If it is not in the process, perform the activation operation
PROCESS_EXIST=`netstat -anp | grep 8899 | grep -v "grep" | awk '{print $7///}'`
If the length is 0, you need to activate the environment, otherwise skip it directly
if [ -z $PROCESS_EXIST ]
then
	To enter the venv environment, you need to activate the environment
	source $VENV_DIR/bin/activate
	Enter the workspace space of the specific code
	cd $JENKINS_PROJECT_DIR
	Start the current projectPython manage.py runServer 0.0.0.0:8899&echo -ne "\n"
	sleep 3
	Log off the environment
	# deactivate
	# A message is displayed indicating startup success
	echo "the project run success"
fi
echo "The project reload success"
Copy the code

2.1.4 Configuration of Webhook

If you don’t configure the Webhook, Jenkins won’t be able to automate the build, so you’ll have to click on it and it won’t be interesting. So here we also need to configure a Webhook.

2.1.5 Jenkins added Webhook

After clicking Advanced, the following screen will appear

Set the specific hook address

Making configuration webhook

3. Run

If you run it, modify the code locally, and you’ll see that Jenkins is running, and if you write the script, you’ll see that your program is running, and all you have to do is make a small change to your code, push it to Github, and you can deploy it automatically with Jenkins. Update the changed code to your code base for automatic build deployment, and then re-request to find that the content has been updated.

4. Summary

What you think you won’t see again will usually be seen again at some point in the future, and you’ll have to deal with it. — — — — — I & Jenkins

Jenkins has had contact with it before. In his first internship, his colleagues used Jenkins to implement the release of code, but at that time, it was still in 2019. He did not have the opportunity to participate in the real online release, so he could not use it. After the second internship, when I was in Didi, the infrastructure of Didi covered it with a layer. At that time, I didn’t even realize it was Jenkins. Haha, although I used it for many times, it was still good and comfortable to use. Later to Tencent internship, did not participate in the official version, so did not contact (internal should also do packaging).

Now I find that I need to use Jenkins when I am working. If I cannot escape it, I will learn how to use Jenkins! Spent half a day to get familiar with it, with a simple example to run, to achieve the specific operation, the code is very simple, is a small Django program, jump to an HTML page.

In general, Jenkins is very powerful. I feel that I need to learn shell script well, which is very useful. There are many scripts in the project, but I can’t understand many of them, which makes me cry.

Keep it up!

Keep thinking, keep coding! Written in Baoan, Shenzhen, May 29, 2021 at 18:06:45! Come on!