[fix]:[20251208][构建docker]
This commit is contained in:
parent
c7c01ddc21
commit
006c3c6643
92
.drone.yml
92
.drone.yml
@ -2,30 +2,50 @@ kind: pipeline
|
|||||||
type: docker
|
type: docker
|
||||||
name: multi-lang-ci
|
name: multi-lang-ci
|
||||||
|
|
||||||
|
# 推荐工作区配置 - 修改为当前项目的正确路径
|
||||||
|
workspace:
|
||||||
|
base: /drone
|
||||||
|
path: .
|
||||||
|
|
||||||
|
# 定义缓存卷,用于存储 Maven 依赖
|
||||||
|
volumes:
|
||||||
|
- name: cache
|
||||||
|
temp: {}
|
||||||
|
- name: npm-cache
|
||||||
|
path: /localcache/npmcache
|
||||||
|
# 添加 Maven 缓存卷(虽然通过 runner 挂载,但显式声明更清晰)
|
||||||
|
- name: maven-cache
|
||||||
|
path: /localcache/maven/repository
|
||||||
|
|
||||||
|
# 触发器配置:针对 master 分支的 PR
|
||||||
trigger:
|
trigger:
|
||||||
event:
|
event:
|
||||||
- pull_request
|
- pull_request
|
||||||
|
- push
|
||||||
|
- tag
|
||||||
branch:
|
branch:
|
||||||
- main # <<< 确保分支名称与您的主分支一致
|
- master
|
||||||
|
|
||||||
|
# 默认克隆步骤 (使用您本地已有的 drone/git:latest)
|
||||||
clone:
|
clone:
|
||||||
depth: 0
|
depth: 0
|
||||||
image: drone/git:latest
|
image: drone/git:latest
|
||||||
pull: false
|
pull: false # 禁用镜像拉取
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
# 自动检测是否存在 Java (pom.xml)
|
# 自动检测是否存在 Java (pom.xml)
|
||||||
- name: java-build
|
- name: java-build
|
||||||
image: maven:3.9-eclipse-temurin
|
image: registry.cn-beijing.aliyuncs.com/yinzy/maven:3.9-eclipse-temurin-8
|
||||||
pull: false
|
pull: false
|
||||||
|
volumes:
|
||||||
|
- name: maven-cache
|
||||||
|
path: /localcache/maven/repository
|
||||||
environment:
|
environment:
|
||||||
MAVEN_OPTS: -Dmaven.repo.local=.m2/repository
|
MAVEN_OPTS: -Dmaven.repo.local=/localcache/maven/repository
|
||||||
commands:
|
commands:
|
||||||
# 在当前工作目录而非 ~ 下创建 .m2 目录,避免权限问题
|
# 动态生成 settings.xml(仅用于 CI)
|
||||||
- mkdir -p .m2/repository
|
|
||||||
# 动态生成 settings.xml
|
|
||||||
- |
|
- |
|
||||||
cat > settings.xml << 'EOF'
|
cat > ~/.m2/settings.xml << 'EOF'
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
|
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
@ -40,27 +60,65 @@ steps:
|
|||||||
</mirrors>
|
</mirrors>
|
||||||
</settings>
|
</settings>
|
||||||
EOF
|
EOF
|
||||||
# 使用自定义 settings.xml
|
- mvn -s ~/.m2/settings.xml -DskipTests=true clean package
|
||||||
- mvn -s settings.xml clean verify
|
# 验证 JAR 文件是否生成
|
||||||
|
- ls -lah target/*.jar || (echo "JAR file not found!" && exit 1)
|
||||||
when:
|
when:
|
||||||
path:
|
path:
|
||||||
include:
|
include:
|
||||||
- pom.xml # 仅当存在 pom.xml 文件时运行
|
- pom.xml
|
||||||
|
event: [ push, pull_request, tag ]
|
||||||
|
|
||||||
|
- name: docker_build
|
||||||
|
image: docker:latest
|
||||||
|
pull: false
|
||||||
|
volumes:
|
||||||
|
- name: docker_sock
|
||||||
|
path: /var/run/docker.sock
|
||||||
|
commands:
|
||||||
|
# 验证 JAR 文件存在
|
||||||
|
- |
|
||||||
|
if [ ! -f target/drone-test-1.0.jar ]; then
|
||||||
|
echo "❌ JAR file not found! Make sure java-build step completed successfully."
|
||||||
|
ls -la target/ || echo "target directory does not exist"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
# 显示构建信息
|
||||||
|
- echo "📦 Building Docker image with tag: drone-test:${DRONE_REPO_NAME}"
|
||||||
|
- echo "📁 JAR file info:"
|
||||||
|
- ls -lh target/drone-test-1.0.jar
|
||||||
|
# 构建 Docker 镜像
|
||||||
|
- docker build -t drone-test:${DRONE_REPO_NAME} -f Dockerfile .
|
||||||
|
# 如果存在 DRONE_TAG,同时打 latest 标签
|
||||||
|
- |
|
||||||
|
if [ -n "${DRONE_TAG}" ]; then
|
||||||
|
docker tag drone-test:${DRONE_REPO_NAME} drone-test:latest
|
||||||
|
echo "✅ Tagged as drone-test:latest"
|
||||||
|
fi
|
||||||
|
- echo "✅ Built Docker image: drone-test:${DRONE_REPO_NAME}"
|
||||||
|
# 显示镜像信息
|
||||||
|
- docker images drone-test
|
||||||
|
depends_on:
|
||||||
|
- java-build
|
||||||
|
when:
|
||||||
|
path:
|
||||||
|
include:
|
||||||
|
- pom.xml
|
||||||
|
event: [ push, pull_request, tag ]
|
||||||
|
|
||||||
# 自动检测是否存在 Python (requirements.txt)
|
# 自动检测是否存在 Python (requirements.txt)
|
||||||
- name: python-test
|
- name: python-test
|
||||||
image: python:3.12.0-slim
|
image: python:3.12.0-slim
|
||||||
pull: false
|
pull: false
|
||||||
commands:
|
commands:
|
||||||
# 临时设置 pip 源
|
# 临时设置 pip 源(仅本次会话)
|
||||||
- pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
|
- pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
|
||||||
- pip config set install.trusted-host pypi.tuna.tsinghua.edu.cn
|
- pip config set install.trusted-host pypi.tuna.tsinghua.edu.cn
|
||||||
- pip install -r requirements.txt
|
- if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
||||||
- python -m pytest test_example.py -v
|
- if [ -f test_example.py ]; then python -m pytest test_example.py -v; fi
|
||||||
when:
|
when:
|
||||||
path:
|
branch:
|
||||||
include:
|
exclude: [ "*" ]
|
||||||
- requirements.txt # 仅当存在 requirements.txt 文件时运行
|
|
||||||
|
|
||||||
# 最终报告(无论成功失败都运行)
|
# 最终报告(无论成功失败都运行)
|
||||||
- name: summary
|
- name: summary
|
||||||
|
|||||||
60
pom.xml
60
pom.xml
@ -7,10 +7,14 @@
|
|||||||
<groupId>com.example</groupId>
|
<groupId>com.example</groupId>
|
||||||
<artifactId>drone-test</artifactId>
|
<artifactId>drone-test</artifactId>
|
||||||
<version>1.0</version>
|
<version>1.0</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<maven.compiler.source>17</maven.compiler.source>
|
<maven.compiler.source>8</maven.compiler.source>
|
||||||
<maven.compiler.target>17</maven.compiler.target>
|
<maven.compiler.target>8</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
@ -19,4 +23,56 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<!-- Maven 编译插件 -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>8</source>
|
||||||
|
<target>8</target>
|
||||||
|
<encoding>UTF-8</encoding>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<!-- Maven JAR 插件,用于生成可执行 JAR -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<version>3.2.0</version>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>com.example.App</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<!-- Maven Shade 插件,用于生成包含所有依赖的可执行 JAR(可选) -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.2.4</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<transformers>
|
||||||
|
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<mainClass>com.example.App</mainClass>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
</project>
|
</project>
|
||||||
Loading…
x
Reference in New Issue
Block a user