Configure jenkins: configure jdk, configure maven, test pipeline

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:

1650642428228-a35ee3ac-8eda-4d2f-8124-f7fabd6a1665.png

1650642525118-f76c27a1-666f-47b5-bb5c-992b5d0a6c8c.png

Configure Maven

This demo installs version 3.6.3 of Maven.

1650642660948-649ca1f6-8b6d-4d46-9423-d1da35ccb610.png

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.

1650642706144-3909b57a-5c21-4675-8de1-2795961eab8b.png

1650642747759-6deab9c4-5201-4724-af0b-ad055c5f546c.png

Enter the address in the browser http://192.168.164.134:8099/restartto restart Jenkins, and then you can see the Maven project option in the new task.

1650643032247-9bc9e23e-bdcf-4966-a279-8810560b2cbe.png

Configure code cloud code warehouse to realize automatic deployment

Create a new Maven project on Jenkins.

1650643392628-c4a1ab69-4e58-406c-ad13-deda92cdaedb.png

Configure the project address on Code Cloud, as follows. When adding credentials, you need to enter the code cloud account and password.

1650644085666-556b411d-cf8c-45bf-acda-ffa5f388d773.png

1650643825204-61cf81ec-397f-4229-8064-301ad5a9b89c.png

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.

1650644163025-8761c662-a784-482a-8fdc-bbdc8b5f2e91.png

Post Steps selection 执行 shell. This step is performed after packaging, which is to execute the shell to start the project.

1650644457396-caa15c04-fab0-4f47-b1a0-9d2d36d799ce.png

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.

1650644521395-51941158-0998-4918-a826-1b634ebb6d72.png

We can view the console output log of the build as follows:

1650645263446-578a1004-5f9b-4d9e-99f3-d75c5288483c.png

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.xmlthe 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 mirrorsto 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:

1650648344021-733540b5-7762-4f3c-b8e7-b96be7446e62.png

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

Guess you like

Origin blog.csdn.net/a772304419/article/details/132173021