需求: 使用jenkins搭建流水线实现持续集成持续部署
一、编写docker-compose.yml安装jenkins, jenkins的版本是带blueocean插件的版本
version: '3'
services:
mysql:
image: mysql:8.0.29
container_name: mysql
restart: always
ports:
- 3306:3306
privileged: true
environment:
TZ: Asia/Shanghai
MYSQL_ROOT_PASSWORD: 123456
command:
--default-authentication-plugin=mysql_native_password
--character-set-server=utf8mb4
--collation-server=utf8mb4_general_ci
--explicit_defaults_for_timestamp=true
--lower_case_table_names=1
volumes:
- /mydata/mysql/data/db:/var/lib/mysql #数据文件挂载
- /mydata/mysql/data/conf:/etc/mysql/conf.d #配置文件挂载
- /mydata/mysql/log:/var/log/mysql #日志文件挂载
redis:
image: redis:7
container_name: redis
restart: always
command: redis-server --requirepass 1234566 --appendonly yes
volumes:
- /mydata/redis/data:/data
- /etc/localtime:/etc/localtime:ro
ports:
- 6379:6379
nginx:
image: nginx:1.22
container_name: nginx
restart: always
volumes:
- /mydata/nginx/nginx.conf:/etc/nginx/nginx.conf #配置文件挂载
- /mydata/nginx/html:/usr/share/nginx/html #静态资源根目录挂载
- /mydata/nginx/log:/var/log/nginx #日志文件挂载
- /etc/localtime:/etc/localtime:ro
ports:
- 80:80
rabbitmq:
image: rabbitmq:3.9-management
container_name: rabbitmq
restart: always
ports:
- 5672:5672
- 15672:15672
environment:
TZ: Asia/Shanghai
volumes:
- /mydata/rabbitmq/data:/var/lib/rabbitmq #数据文件挂载
- /etc/localtime:/etc/localtime:ro
mongo:
image: mongo:4
container_name: mongo
restart: always
volumes:
- /mydata/mongo/db:/data/db
ports:
- 27017:27017
environment:
TZ: Asia/Shanghai
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 123456
command: --auth
jenkins:
image: jenkinsci/blueocean
container_name: jenkins
restart: always
user: root
ports:
- 8080:8080
- 50000:50000
volumes:
- /mydata/jenkins_home:/var/jenkins_home
- /etc/localtime:/etc/localtime:ro
- /var/run/docker.sock:/var/run/docker.sock
在/mydata/jenkins_home
目录配置maven的阿里源

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
</servers>
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
<localRepository>/root/.m2</localRepository>
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
</settings>
二、登录xxx:8080配置jenkins
1、初始密码使用 docker logs jenkins
查看获取
2、安装插件 Docker Pipeline
, 后面构建流水线需要

3、准备一个项目,提交到Gitee

创建两个文件 Jenkinsfile和Dockerfile
pipeline{
agent any
environment {
WS = "${WORKSPACE}"
IMAGE_NAME = "java-devops-demo"
}
//定义流水线的加工流程
stages {
//流水线的所有阶段
stage('1.环境检查'){
steps {
sh 'pwd && ls -alh'
sh 'printenv'
sh 'docker version'
sh 'java -version'
sh 'git --version'
}
}
stage('2.编译'){
agent {
docker {
image 'maven:3-alpine'
args '-v maven-repository:/root/.m2'
}
}
steps {
sh 'pwd && ls -alh'
sh 'mvn -v'
sh 'cd ${WS} && mvn clean package -s "/var/jenkins_home/appconfig/maven/settings.xml" -Dmaven.test.skip=true'
}
}
stage('3.打包'){
steps {
sh 'pwd && ls -alh'
sh 'docker build -t ${IMAGE_NAME} .'
}
}
stage('4.部署'){
// 删除容器和虚悬镜像
steps {
sh 'pwd && ls -alh'
sh 'docker rm -f ${IMAGE_NAME} && docker rmi $(docker images -q -f dangling=true)'
sh 'docker run -d -p 8888:8080 --name ${IMAGE_NAME} -v /mydata/logs/${IMAGE_NAME}:/logs/${IMAGE_NAME} ${IMAGE_NAME}'
}
}
}
}
FROM openjdk:8-jre-alpine
# 将当前目录下的jar包复制到docker容器的/目录下
COPY target/*.jar /app.jar
# 运行过程中创建一个xx.jar文件
RUN touch /app.jar;
ENV TZ=Asia/Shanghai JAVA_OPTS="-Xms128m -Xmx256m -Djava.security.egd=file:/dev/./urandom"
ENV PARAMS=""
# 声明服务运行在8080端口
EXPOSE 8080
# 指定docker容器启动时运行jar包
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /app.jar $PARAMS" ]
4、jenkins首页首页左侧的新建任务,输入任务名称,选择流水线

5、配置流水线

6、返回首页点击’打开 Blue Ocean’选择运行

到此整体功能已经完成
三、测试

四、触发远程构建


创建一个jenkins账号用来触发远程构建

构建maven项目

来源:blog.csdn.net/qq_39940205/
article/details/126159330
后端专属技术群 构建高质量的技术交流社群,欢迎从事编程开发、技术招聘HR进群,也欢迎大家分享自己公司的内推信息,相互帮助,一起进步!
文明发言,以
交流技术
、职位内推
、行业探讨
为主广告人士勿入,切勿轻信私聊,防止被骗
加我好友,拉你进群
原文始发于微信公众号(Java知音):Docker+Jenkins(blueocean)+Gitee构建CICD流水线实战
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/154250.html