实现Jenkins自动发包配置

参考抖音:Java不良人 其中的视频演示代码

不推荐把jenkins端口一直开放,推荐使用时候放开(版本不太新,避免漏洞攻击)

[root@VM-4-12-centos soft]# docker-compose -v
Docker Compose version v2.19.1

docker-compose.yml 如下

version: '3.1'
services:
  jenkins:
    image: jenkins/jenkins:2.403
    volumes:
      - ./data/jenkins/:/var/jenkins_home
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker
      - /usr/lib/x86_64-linux-gnu/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
    ports:
    	# 8088为访问端口
      - "8088:8080"
      # 8081为你要启动的jar服务端口
	  - "8081:8081"
    expose:
      - "8080"
      - "50000"
    privileged: true
    user: root
    restart: always
    container_name: jenkins
    environment:
      JAVA_OPTS: '-Djava.util.logging.config.file=/var/jenkins_home/log.properties' 

找到 docker-compose.yml 下面执行

[root@VM-4-12-centos soft]# docker-compose up -d
查看启动
[root@VM-4-12-centos soft]# docker ps
CONTAINER ID   IMAGE                   COMMAND                  CREATED       STATUS       PORTS                                                                                             NAMES
c0cedad006a1   jenkins/jenkins:2.403   "/usr/bin/tini -- /u…"   9 hours ago   Up 9 hours   0.0.0.0:8081->8081/tcp, :::8081->8081/tcp, 50000/tcp, 0.0.0.0:8088->8080/tcp, :::8088->8080/tcp   jenkins

在浏览器打开Jenkins端口

查找密码

cat /var/lib/jenkins/secrets/initialAdminPassword

添加自己的用户

选择插件

选择“Install suggested plugins”安装默认的插件,下面Jenkins就会自己去下载相关的插件进行安装。
参考文章:使用docker-compose部署jenkins

配置Java环境

配置maven环境

wget https://mirrors.aliyun.com/apache/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz

#将容器中的文件拷贝出来
sudo docker cp c0cedad006a1:/opt/apache-maven-3.9.4/conf/settings.xml /soft/
#将容器中的文件拷贝回去
sudo docker cp /soft/settings.xml  容器ID:/opt/apache-maven-3.9.4/conf/

修改 maven settings.xml 仓库镜像地址为阿里云镜像(否则下载依赖会很慢)

使用修改maven-settings 阿里云镜像

<mirror>
	<id>alimaven</id>
	<name>aliyun maven</name>
	<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
 	<mirrorOf>central</mirrorOf>
</mirror>
原文链接:https://blog.csdn.net/u012723183/article/details/129347151

编写自己代码上gitee

遇到 jar运行报错no main manifest attribute
使用下面代码解决

 <plugin>
    <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <version>${spring-boot.version}</version>
         <configuration>
             <includeSystemScope>true</includeSystemScope>
             <fork>true</fork>
         </configuration>
         <executions>
             <execution>
                 <goals>
                     <goal>repackage</goal>
                 </goals>
             </execution>
         </executions>
     </plugin>

错误代码如下

   <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring-boot.version}</version>
        <configuration>
            <mainClass>com.example.demo.DemoApplication</mainClass>
            <skip>true</skip>
        </configuration>
        <executions>
            <execution>
                <id>repackage</id>
                <goals>
                    <goal>repackage</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

构建后的执行shell命令如下

#!/bin/bash

# 获取进程PID
PID=$(ps -ef | grep "demo-0.0.1-SNAPSHOT.jar" |grep -v grep|awk '{print $2}')

# 杀死进程
if [ -n "$PID" ]; then
  kill -9 $PID
  echo "结束进程Process $PID killed" 
else
  echo "没有找到指定进程"
fi
cd /var/jenkins_home/workspace/demoss/target
chmod 755 demo-0.0.1-SNAPSHOT.jar 
BUILD_ID=dotkillme nohup java -jar demo-0.0.1-SNAPSHOT.jar &
echo "启动进程"

Gitee + Jenkins 实现自动触发构建

参考文章

https://juejin.cn/post/7003226813904191496

实现Jenkins自动发包配置_第1张图片
实现Jenkins自动发包配置_第2张图片

优秀文章推荐

https://blog.csdn.net/u012723183/article/details/129347151

你可能感兴趣的:(Jenkins,jenkins,servlet,运维)