文章目录
- 一、先准备:你的 SpringBoot 项目
-
- 1. 创建一个最简单的 SpringBoot 项目
- 2. 在项目根目录创建 3 个文件(必须)
-
- ① `Dockerfile`
- ② `Jenkinsfile`
- ③ pom.xml 增加配置
- 二、Gitea 操作(必须做)
-
- 1. 打开 Gitea
- 2. 创建仓库
- 3. 拿到仓库地址
- 4. IDEA 把项目代码提交到 Gitea
- 三、Jenkins 关键配置(最容易错,我一步步教你)
-
- 1. 打开 Jenkins
- 2. 安装必须插件
- 3. 配置 JDK + Maven
-
- 系统管理 → 全局工具配置
- 第一步:新建流水线任务
- 第二步:配置流水线关联 Gitea 代码
- 第三步:保存并运行
- `由于上面的问题出现版本老旧换成用Dockerfile自制一个自带docker的Jenkins来解决上面的问题`
一、先准备:你的 SpringBoot 项目
1. 创建一个最简单的 SpringBoot 项目
确保有:
- Spring Web
- JDK 17
- Maven
2. 在项目根目录创建 3 个文件(必须)
devops/ ├── src/ │ └── main/ │ ├── java/ │ └── resources/ │ └── application.yaml ├── pom.xml ← 和这个同级 ├── Dockerfile ← 放在这里! └── Jenkinsfile ← 放在这里!
① Dockerfile
FROM openjdk:17-jdk-slim
WORKDIR /app
COPY target/*.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]
② Jenkinsfile
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
git url: 'http://gitea:3000/你的用户名/demo.git', credentialsId: 'gitea-account'
}
}
stage('编译打包') {
steps {
sh 'mvn clean package -DskipTests'
}
}
stage('Sonar 代码扫描') {
steps {
sh 'mvn sonar:sonar \\
–Dsonar.host.url=http://sonar:9000 \\
–Dsonar.login=admin \\
–Dsonar.password=admin'
}
}
stage('构建 Docker 镜像') {
steps {
sh 'docker build -t spring-demo:$BUILD_ID .'
}
}
stage('推送到 Nexus 镜像仓库') {
steps {
sh 'docker tag spring-demo:$BUILD_ID localhost:8081/spring-demo:$BUILD_ID'
sh 'docker push localhost:8081/spring-demo:$BUILD_ID'
}
}
}
}
③ pom.xml 增加配置
<properties>
<java.version>17</java.version>
<sonar.host.url>http://localhost:9000</sonar.host.url>
<sonar.projectKey>demo</sonar.projectKey>
<sonar.login>admin</sonar.login>
<sonar.password>admin</sonar.password>
</properties>
二、Gitea 操作(必须做)
1. 打开 Gitea
http://localhost:3000
2. 创建仓库
- 新建仓库 → 名字 demo
- 勾选「初始化仓库」
- 创建
3. 拿到仓库地址
http://localhost:3000/你的用户名/demo.git

4. IDEA 把项目代码提交到 Gitea
- 需要提交安装好git 先下载安装 Git(Windows 版) 直接点这个下载: https://git-scm.com/download/win 一路下一步安装,不用改任何东西

- 在IDEA的terminal里面依次执行下面命令

git init
git add .
git commit -m "init"
git remote add origin http://localhost:3000/admin/demo.git
git push -u origin master
git init = 初始化:告诉Git“这个文件夹我要管理” git add . = 全选:把所有代码放进“待提交包裹” git commit -m “init” = 打包:给包裹贴标签,确定提交 git remote add origin 地址 = 绑定:告诉Git快递要发到哪个Gitea仓库 git push -u origin master = 发货:把代码真正传到Gitea 初始化 → 全选文件 → 打包备注 → 绑定 Gitea 地址 → 上传代码
报错Author identity unknown *** Please tell me who you are.Rungit config –global user.email "you@example.com" git config –global user.name "Your Name" to set your account's default identity. Omit –global to set the identity only in this repository. fatal: unable to auto-detect email address (got 'wanxiangan@LAPTOP-SM4KU0JU.(none)')
git config –global user.name “qq” git config –global user.email “qq@example.com”
然后你就可以继续执行:
git commit -m "init"
此时文件提交到了 你项目里的 .git 隐藏文件夹 
✅ 代码现在在 Gitea 里了 
你每次改代码,都必须手动执行 git 命令! Git 永远不会自动帮你提交、自动帮你上传!**
✅ 简单说: 这些命令都是在IDEA的terminal里面执行的
git init
git add .
git commit -m "init"
git remote add origin 地址
git push -u origin master
git add .
git commit -m "我改了XX代码"
git push
remote: Failed to authenticate user
fatal: Authentication failed for 'http://localhost:3000/admin/demo.git/'
解决方法是 git remote set-url origin http://admin:123456@localhost:3000/admin/demo.git
Git 不是云盘! 你不敲命令,代码永远不会到 Gitea!
- git add . = 选中所有改动
- git commit = 保存到本地
- git push = 上传到 Gitea
git add .
git commit -m "更新了XXX功能"
git push
三、Jenkins 关键配置(最容易错,我一步步教你)
1. 打开 Jenkins
http://localhost:8080
2. 安装必须插件
- Maven Integration
- SonarQube Scanner
- Git
- Docker
- Pipeline
3. 配置 JDK + Maven
系统管理 → 全局工具配置
- JDK 17
- Maven 3.8.x
第一步:新建流水线任务

先讲:SCM 是什么 缩写:SCM = Source Control Management 中文:源代码管理系统
第二步:配置流水线关联 Gitea 代码
进入配置页后,往下滑到 Pipeline 板块:
在「Definition」里,选择 Pipeline script from SCM (Pipeline script from SCM =让 Jenkins 去 Git 上找 Jenkinsfile 运行,这就是现代 Jenkins 的标准用法!)
「SCM」选择 Git
在「Repository URL」里,填入你的 Gitea 仓库地址:
http://localhost:3000/你的用户名/demo.git 不能写成localhost因为这里的本质是另外的docker容器,不是本地 http://gitea:3000/admin/demo.git 要写成这个地址才对
4. 点击「Credentials」右侧的「添加」→「Jenkins」,选择「Username with password」:
- 输入你的 Gitea 用户名和密码 admin/123456
- 「ID」里填:gitea-account(后面 Jenkinsfile 会用到)
- 点击「添加」

「Branch to build」默认填 */master(如果你的分支叫 main,就改成 */main)
「Script Path」填:Jenkinsfile(就是你项目根目录的那个文件) 
第三步:保存并运行
报错
报错:
> git rev-parse –resolve-git-dir /var/jenkins_home/workspace/spring-demo-pipeline/.git # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url http://gitea:3000/admin/demo.git # timeout=10
Fetching upstream changes from http://gitea:3000/admin/demo.git
> git –version # timeout=10
> git –version # 'git version 2.47.3'
using GIT_ASKPASS to set credentials
> git fetch –tags –force –progress — http://gitea:3000/admin/demo.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision 3b209202670162cd8d4b6babda9b6f8583607510 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 3b209202670162cd8d4b6babda9b6f8583607510 # timeout=10
> git branch -a -v –no-abbrev # timeout=10
> git branch -D master # timeout=10
> git checkout -b master 3b209202670162cd8d4b6babda9b6f8583607510 # timeout=10
Commit message: "wofix了jenkinsfile"
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (编译打包)
[Pipeline] sh
+ mvn clean package -DskipTests
/var/jenkins_home/workspace/spring-demo-pipeline@tmp/durable-5cc2b260/script.sh.copy: 1: mvn: not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Sonar 代码扫描)
解决方法是
用下面的Jenkinsfile
pipeline {
agent any
tools {
maven 'M3' // 对应你刚才配置的 Maven 别名
}
stages {
stage('拉取代码') {
steps {
git url: 'http://gitea:3000/admin/demo.git', credentialsId: 'gitea-account'
}
}
stage('编译打包') {
steps {
sh 'mvn clean package -DskipTests'
}
}
stage('Sonar 代码扫描') {
steps {
sh 'mvn sonar:sonar \\
-Dsonar.host.url=http://sonar:9000 \\
-Dsonar.login=admin \\
-Dsonar.password=admin'
}
}
stage('构建 Docker 镜像') {
steps {
sh 'docker build -t spring-demo:$BUILD_ID .'
}
}
stage('推送到 Nexus 镜像仓库') {
steps {
sh 'docker tag spring-demo:$BUILD_ID nexus:8081/spring-demo:$BUILD_ID'
sh 'docker push nexus:8081/spring-demo:$BUILD_ID'
}
}
}
}
主要是添加了
tools {
maven 'M3' // 对应你刚才配置的 Maven 别名
}
然后再Jenkins的全局配置工具
里面添加maven
报错 [ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121:sonar (default-cli) on project devops: Not authorized. Please check the properties sonar.login and sonar.password. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
解决方法是Jenkinsfile里面的密码配置错误,替换成你自己的密码在提交
报错+ docker build -t spring-demo:7 . /var/jenkins_home/workspace/spring-demo-pipeline@tmp/durable-96bd72a5/script.sh.copy: 1: docker: not found [Pipeline] } [Pipeline] // withEnv [Pipeline] } [Pipeline] // stage [Pipeline] stage [Pipeline] { (推送到 Nexus 镜像仓库) Stage "推送到 Nexus 镜像仓库" skipped due to earlier failure(s)
问题找到了!1. 你缺的不是宿主机Docker,是Jenkins 容器内的 docker 命令 2. 执行 2 条命令就能临时修复 3. 换镜像可以永久解决,不会丢失环境
换成下面镜像
jenkins:
image: jenkinsci/blueocean
ports:
– "8080:8080"
– "50000:50000"
volumes:
– jenkins-data:/var/jenkins_home
– /var/run/docker.sock:/var/run/docker.sock
restart: always
user: root
由于换了镜像,以前的数据卷是脏的需要删除掉重来
别折腾密码、别折腾目录了,直接清空数据卷,重置干净 Jenkins! 终极方案:一键重置 Jenkins(100% 成功) 执行这 3 条命令,直接重置到全新状态: docker-compose down docker volume rm devops_jenkins-data docker-compose up -d 然后等 20 秒,直接拿密码(这次一定能拿到!) docker exec devops-jenkins-1 cat /var/jenkins_home/secrets/initialAdminPassword
测试 Docker 是否可用(秒出结果) docker exec devops-jenkins-1 docker –version
配置密码 admin/123456
方案一:直接跳过插件安装(最快) 点击页面右下角的 继续 按钮,直接进入 Jenkins 主界面。 这样可以跳过所有失败的插件安装,先进入系统,再手动配置国内源。 方案二:配置国内镜像源(永久解决) 先按方案一进入 Jenkins 主界面。 进入:Manage Jenkins → Plugin Manager → Advanced。 在 Update Site 中,把地址改成国内镜像,比如: https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json 点击 Submit 保存,然后重新安装插件即可。 
由于上面的问题出现版本老旧换成用Dockerfile自制一个自带docker的Jenkins来解决上面的问题
具体轻参照另外一篇文章
最终的两个文件
- Dockfile
# 1. 基础镜像:用官方最新稳定版 Jenkins + JDK17
FROM jenkins/jenkins:lts-jdk17
# 2. 切换到 root 管理员权限(必须,不然装不了软件)
USER root
# 3. 下载并安装 Docker 客户端(只装命令行工具,不装引擎)
RUN curl -fsSL https://download.docker.com/linux/static/stable/x86_64/docker-26.1.4.tgz \\
| tar xzvf – –strip-components=1 -C /usr/local/bin
# 4. 创建 docker 用户组,并把 jenkins 账户加入组(解决权限问题)
RUN groupadd -f docker && usermod -aG docker jenkins
# 5. 切回普通用户 jenkins 运行(安全规范)
USER jenkins
- docker-compose.yml
version: '3.8'
# 企业级DevOps 全栈本地环境(WSL 专用稳定版)
services:
# 1. 代码仓库(替代GitLab)
gitea:
image: gitea/gitea:latest
ports:
– "3000:3000"
– "222:22"
volumes:
– gitea-data:/data
restart: always
# 2. CI核心 Jenkins image: jenkins/jenkins:lts-jdk17 jenkinsci/blueocean
jenkins:
build: . # 重点:用本地Dockerfile构建
image: my-jenkins-docker:latest # 你自己的镜像名字
ports:
– "8080:8080"
– "50000:50000"
volumes:
– jenkins-data:/var/jenkins_home
– /var/run/docker.sock:/var/run/docker.sock
restart: always
user: root
# 3. 代码质量 SonarQube
sonar:
image: sonarqube:lts-community
ports:
– "9000:9000"
environment:
– SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true
volumes:
– sonar-data:/opt/sonarqube/data
– sonar-ext:/opt/sonarqube/extensions
restart: always
# 4. 制品仓库 Nexus(jar+镜像)
nexus:
image: sonatype/nexus3:latest
ports:
– "8081:8081"
volumes:
– nexus-data:/nexus-data
restart: always
# 5. 配置中心 Nacos
nacos:
image: nacos/nacos-server:v2.2.3
ports:
– "8848:8848"
– "9848:9848"
environment:
– MODE=standalone
– JVM_XMS=256m
– JVM_XMX=256m
volumes:
– nacos-data:/home/nacos/data
restart: always
# 6. 监控 Prometheus
prometheus:
image: prom/prometheus:latest
ports:
– "9090:9090"
volumes:
– prom-data:/prometheus
restart: always
# 7. 可视化监控 Grafana
grafana:
image: grafana/grafana:latest
ports:
– "3001:3000"
volumes:
– grafana-data:/var/lib/grafana
restart: always
# ====================== WSL 专用数据卷(永不报错、永久保存)======================
volumes:
gitea-data:
jenkins-data:
sonar-data:
sonar-ext:
nexus-data:
nacos-data:
prom-data:
grafana-data:





