This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.
preface
In the last article, we used Docker to write Dockerfile files, built our own projects into images, and then published them to Docker Hub, and used our own cloud server to pull the project images uploaded by ourselves on Docker Hub, and run the container by the image. This enabled us to successfully run our project with Docker and pass the test of external access.
If you haven’t built your own images with Docker, I suggest you read the first article: Portals
In this article, you’ll learn how to use Jenkins to help you listen for changes in your Git repository. Once a new push is made to the master branch, The Jenkins service actively pulls the project code from Gitee and builds a new image (with the help of the Dockerfile you wrote in the previous section). Then delete the old image and container with the same name and deploy the new container.
For developers, once you push the new code into the master branch of the remote repository, you can immediately access the URL and see the latest project results, all done by Jenkins (as long as the Jenkins workflow is configured beforehand).
Originally I learn programming is like watching video, and is the kind of video, long and full always thought can see someone every step of the operation is very solid, but gradually, or see more documents, I also recommend you to try to read the document here, English seems slow is in Chinese, compared to the video, see document can quickly find what you need, and the rhythm of the video need to follow others, It’s slower. (But I recommend watching videos for starters, along with reading books, documents and blog posts.)
Jenkins related
The installation
Here gives jerkins Chinese document: www.jenkins.io/zh/doc/tuto… , you can choose to install the Jenkins service directly on the server, or you can choose to run the Jenkins service on the server through the container. Here I choose the latter, click the link above, you can run the Jinkins service with Docker.
The core is the following shell that runs the Docker container. I have already explained the function of these parameters in the previous article. If you are not clear, you can go back and look at them together.
docker run \
-d \
--rm \
-u root \
-p 8080:8080 \
-v jenkins-data:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$HOME":/home \
jenkinsci/blueocean
Copy the code
The login
After running the Jenkins container, accessing the server’S IP :8080 redirects you to the Jenkins login page, which will first ask you to enter a key that will be displayed on the console when the server runs the Jenkins container (if no -d parameter is added).
Or enter the command below to view Jenkins’ key
cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the code
After filling in the key, you will be redirected to the Jerkins page, which will prompt you to install the recommended plug-in when you log in for the first time. Click. Then you will be guided to create a user to log in to Jerkins. Enter the user name, password, email and other information to complete the creation. After accessing the server IP :8080, you will be prompted to enter the user name and password to log in to Jenkins.
A new task
Click On the left side to create a new task, enter your task name, such as Wood-app-Backend, and select build a free-style project
Then go to the Jenkins project configuration area, select the source management TAB, Git option, enter your Git repository address, add your Git repository username and password to the Credentials, and select Listen on the Master branch (default).
The effect we want is to automatically build the image once the Git repository changes and deploy the new image container, so select the polling SCM under the build trigger and use the corn expression to control Jenkins to listen to the Git repository once per minute
Here’s the core operation. We already know what Jenkins is going to do. How does Jerkins know? The shell is what Jenkins will do when he listens for changes in the master branch of the Git repository, including deleting the created container (forcibly deleted because the port is occupied by the old container), building a new image, and running the new container
if docker ps -a|grep -i wood-app-backend; Then docker rm -f wood-app-backend fi # Port collision sleep 1 docker build -t baize1998/wood-app-backend:latest --name wood-app-backend baize1998/wood-app-backend:latest # Run an image to generate a containerCopy the code
Deleting an Old Image
The shell command above has the command to delete the old container, but not the command to delete the old image (every time a new image of the same name is built, the old image becomes None, but still occupies space and needs to be reclaimed).
However, writing the command to delete the mirror directly in the shell may cause errors during reclamation. Therefore, create an additional scheduled task to reclaim the old mirror. Here, specify the frequency of cleaning the mirror to be executed every day at 1:00 am (optional).
Shell scripts are used to determine if there are mirrors of the < None > state and to recycle them
echo ---------------Clear-Images... ------------------ clearImagesList=$(docker images -f "dangling=true" -q) if [ ! -n "$clearImagesList" ]; then echo "no images need clean up." else docker rmi $(docker images -f "dangling=true" -q) echo "clear success." fiCopy the code
The test CI/CD
CI– continuous integration (once pushed, the new image is built), CD– continuous deployment (once pushed, the new container will run against the new image and provide the latest services), let’s modify our project interface and push it to the master branch of the remote repository
One minute later, visit server IP :5000 and see that Jenkins has finished building the project image and running the new project container, providing the latest services, and is ready for agile development!
conclusion
Using Corn to monitor the changes of git repository every minute to achieve such fine-grained CI/CD effect, of course, this is only the tip of the iceberg of Jenkins function, the specific Jenkins deployment stage can be subdivided into construction, testing, deployment, etc. We can also configure different response behaviors for different push commands, and each stage can also specify the execution of various scripts. This needs to learn From Jenkins’ pipeline mechanism and specify a more detailed and standardized CI/CD process through pipeline. However, so far, the development and maintenance functions of school projects are barely enough
Set up a spring and autumn recruitment preparation/internal push/chat group, welcome to join.
P3-juejin.byteimg.com/tos-cn-i-k3…
Pay attention to the public number [programmer Bai Ze], take you to a bit of chatty programmer/student party.
P3-juejin.byteimg.com/tos-cn-i-k3…