Article directory
foreword
With the maturity of software development, DevOps has been deeply rooted in the hearts of the people. As an important part of DevOps: automated deployment has always been everyone's top priority infrastructure. In fact, there are many tools for automated deployment. In my previous article, I wrote about using the Gitlab Runner of the Gitlab family to achieve automated deployment. However, limited by the fact that Gitlab Runner has no plug-ins and ecology, it began to try to transfer to Jenkins. This article only records the quick start of Jenkins automated deployment. This article does not cover some advanced operations such as process orchestration, Jenkinsfile, and WebHook.
1. What is Jenkins?
Jenkins is an open source automation server. It helps automate the parts of software development related to building, testing, and deployment, facilitating continuous integration and continuous delivery. It is a server-based system that runs in a servlet container such as Apache Tomcat. It supports version control tools including AccuRev, CVS, Subversion, Git, Mercurial, Perforce, ClearCase, and RTC, and can execute Apache Ant, Apache Maven, and sbt-based projects as well as arbitrary shell scripts and Windows batch commands
2. Use steps
2.1. Software installation
Software like this suggests choosing Docker without thinking:
docker run \
-d \
-u root \
-p 8080:8080 \
-v /usr/local/docker/jenkins-data:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v "$HOME":/home \
--privileged=true \
--restart=always \
--name jendemo \
jenkinsci/blueocean
After the Run is successful, you can directly access the 8080 port to see this interface.
Enter the container as required to view the password of this file:
docker exec -it jendemo bash
cat /var/jenkins_home/secrets/initialAdminPassword
Choose to install the recommended plug-in.
Jenkins will have a Java 11 environment by default. If you need Gradle and Maven, you need to configure it yourself.
The username and password of the administrator can be whatever you want.
Click Restart and wait for a while. Jenkins needs to be restarted every time the plug-in is installed. It is very convenient for us to use Docker to directly restart the command.
After restarting, enter System Management -> Plug-in Management, and upgrade all the plug-ins that need to be upgraded. Install the Gitlab and NodeJS plugins additionally, and then restart.
2.2. Software configuration
In the global tool configuration in the system management, you can select the environments of Maven, Gradle, and NodeJS, and check the automatic installation. But there is a small pit here, that is, after clicking Save, it will not be downloaded and installed directly, but only when the trigger is used. For example, Maven needs to select the Maven option in the new job to automatically download; for Node, it needs to check this option to automatically download.
Since Jenkins itself actually executes related commands in the container, if you feel that its environment configuration is not easy to use, you can configure and install it in the container yourself.
3. Spring project
3.1. Project construction
Jenkins can be used directly after being configured according to the second step. For verification, we create a new project on Gitlab, check SpringWeb and write a Controller:
@RestController
public class GreetingController {
@GetMapping("/greeting")
public String greeting() {
return "Hello Jenkins!";
}
}
Create a docker
folder in the root directory and put a Dockerfile
file in it:
# 指定是基于哪个基础镜像
FROM java:8
# 作者信息
MAINTAINER Peach
# 挂载点声明
VOLUME /tmp
# 将本地的一个文件或目录,拷贝到容器的文件或目录里
ADD /build/libs/demo-0.0.1-SNAPSHOT.jar springboot.jar
#shell脚本
RUN bash -c 'touch /springboot.jar'
# 将容器的8000端口暴露,给外部访问。
EXPOSE 8080
# 当容器运行起来时执行使用运行jar的指令
ENTRYPOINT ["java", "-jar", "springboot.jar"]
注:我这里的Spring是用Gradle依赖的,如果用maven请自行替换Jar包位置和构建命令
3.2. Jenkins configuration
On the Jenkins home page, create a new task, select Free Style:
Set up the project, select Git here for source code management, then put our Gitlab project address on it, and add a Gitlab username and password in Credentials.
The http method is used here. If you use ssh, you need to configure the public and private keys of the container and transfer them to the Gitlab account. This article aims to be fast, so I won't cover this method.
Then we check this Build when a change is pushed to Gitlab
option, and copy the following URL.
Copy the above URL to Gitlab, and click Webhook:
After adding, we can see the Hook here, and we can click Test to test it.
Returning 200 means success, so that every time the user submits the code, Jenkins will be actively notified to perform automatic deployment.
But if the following 403 error occurs in the Test:
you need to set up a security policy in Jenkins and check the following button:
Finally, we write what needs Jenkins to execute after receiving this event. In the build, we added an execution shell:
to execute Linux scripts, you can execute whatever you want. Here I build the Jar package of the project through Gradle, and then build the Dockerfile, put the Jar package into the Docker image, and start it.
gradle bootJar
docker rm -f app_docker
sleep 1
docker rmi -f app_docker:1.0
sleep 1
docker build -t app_docker:1.0 -f ./docker/Dockerfile .
sleep 1
docker run -d -p 5001:8080 --name app_docker app_docker:1.0
In the console, we can see the specific execution information. In fact, it is the same as typing commands on Linux. If something goes wrong, just solve it. I think you can do it.
After I succeed here, I can directly access the server's port 5001 to see the content of the Controller we wrote. Directly modify the content to Test123, wait a few seconds, and refresh to see the result.
3. Vue project
The background is actually similar to the front-end, nothing more than using Gradle for the background and NodeJS for the front-end. There is a simple way, which is to execute the shell directly like the Spring project, and then put dist
the folder mv to the Nginx directory of the server host. I use a more complicated method here, build it into Docker, and then publish the address of the front end through Docker.
Let's first create a docker folder and Dockerfile in the root directory of the front-end project:
# build stage
FROM node:lts-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# production stage
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Don’t forget to create a WebHook with the front-end project in Jenkins in the front-end Gitlab~ and the background Spring project, only the execution of the Shell is different:
docker rm -f glmx-fornt
sleep 1
docker rmi -f glmx-fornt:1.0
sleep 1
docker build -t glmx-fornt:1.0 -f ./docker/Dockerfile .
sleep 1
docker run -d -p 5003:8080 --name glmx-fornt glmx-fornt:1.0
After uploading the code, wait for the successful construction to access port 5003 of the server to see the front-end content.
Summarize
The above is the simple way to use Jenkins, I hope it can give you some help. If you have any questions, welcome to communicate with me in the comment area.