Configure Jenkins
Configure JDK
With Jenkins installed in the container mode, the JDK has been installed by default in the container, as shown below:
bash-5.1$ java -version
openjdk version "11.0.14.1" 2022-02-08
OpenJDK Runtime Environment Temurin-11.0.14.1+1 (build 11.0.14.1+1)
OpenJDK 64-Bit Server VM Temurin-11.0.14.1+1 (build 11.0.14.1+1, mixed mode)
bash-5.1$ echo $JAVA_HOME
/opt/java/openjdk
bash-5.1$
Configure the JDK installation path to the Jenkins global configuration as follows:
Configure Maven
This demo installs version 3.6.3 of Maven.
Install the Maven plugin
If the project we develop is a Maven project, we need to install the Maven plug-in and restart Jenkins after installation.
Enter the address in the browser http://192.168.164.134:8099/restart
to restart Jenkins, and then you can see the Maven project option in the new task.
Configure code cloud code warehouse to realize automatic deployment
Create a new Maven project on Jenkins.
Configure the project address on Code Cloud, as follows. When adding credentials, you need to enter the code cloud account and password.
The build command is configured according to its own situation. The project we pulled from Code Cloud is a simple Maven project, so it is enough to configure simple commands.
Post Steps selection 执行 shell
. This step is performed after packaging, which is to execute the shell to start the project.
A simple startup service script is as follows:
#!/bin/bash
# 服务名称
SERVER_NAME=jenkins-demo
# 工程所在路径(根据自己情况进行调整)
APP_HOME=$(pwd)
# maven打包后的jar包名
JAR_NAME=jenkins-demo-0.0.1-SNAPSHOT.jar
# jar包的目录
JAR_PATH=${APP_HOME}/target
# 杀死之前的进程
PID_FILE="${APP_HOME}"/"${SERVER_NAME}".pid
if [ -f "${PID_FILE}" ];then
PID=`cat "$PID_FILE"` && kill -9 $PID && echo "kill process "${PID}" finished!"
fi
cd $JAR_PATH
# 修改文件权限
chmod 755 $JAR_NAME
# 启动服务
BUILD_ID=dontKillMe nohup java -jar $JAR_NAME &
# 将新进程ID写到文件中
JAVA_PID=$!
echo "${JAVA_PID}" > "${PID_FILE}"
After saving the project configuration, click Build Now to pull the code, build the package, and start the service.
We can view the console output log of the build as follows:
It is the first time to build a Maven project, because we configured Jenkins to automatically download Maven 3.6.3 at the beginning, so we need to download the Maven installation package for the first time, resulting in a slower build speed. As can be seen from the log above, the downloaded Maven The installation package is extracted to the following directory:
/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven3.6.3/conf
In order to speed up the speed of downloading dependencies for Maven projects, we can open setting.xml
the configuration file and change the warehouse to Alibaba Cloud.
vi /var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven3.6.3/conf/settings.xml
Write the following mirrors
to the middle of the label pair in the file.
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>maven-default-http-blocker</id>
<mirrorOf>external:http:*</mirrorOf>
<name>Pseudo repository to mirror external repositories initially using HTTP.</name>
<url>http://0.0.0.0/</url>
<blocked>true</blocked>
</mirror>
Our Maven project will be deployed to /var/jenkins_home/workspace/
a directory inside the container. And the build script will be automatically executed, and the final deployment success log is as follows:
Finally, visit your engineering interface in the container to verify whether the service is deployed successfully.
bash-5.1$ curl 127.0.0.1:15340/test
Hello Jenkins!
bash-5.1$
If you need to access the deployed service interface externally, you need to map the port of your service to the host through the container, that is, you can perform port mapping when starting the Jenkins container. Assume that the deployed service port is 15340, as follows:
# -d 后台方式启动
# -p映射端口,宿主机端口:容器内端口
# -v 挂载卷,将容器Jenkins工作目录/var/jenkins_home挂载到宿主机目录/usr/local/jenkins
# -name 给容器起个别名
docker run -d -p 8099:8080 -p 50099:50000 -p 15340:15340 -v /usr/local/jenkins:/var/jenkins_home --name myjenkins jenkinsci/blueocean