cicd-test/.drone.yml

213 lines
6.5 KiB
YAML
Raw Normal View History

2025-11-28 19:00:26 +08:00
kind: pipeline
type: docker
name: multi-lang-ci
2025-12-07 21:56:08 -05:00
# 推荐工作区配置 - 修改为当前项目的正确路径
workspace:
base: /drone
path: .
# 定义缓存卷,用于存储 Maven 依赖
volumes:
- name: cache
temp: {}
- name: npm-cache
path: /localcache/npmcache
# 添加 Maven 缓存卷(虽然通过 runner 挂载,但显式声明更清晰)
- name: maven-cache
path: /localcache/maven/repository
# 定义 Docker socket 卷
- name: docker_sock
path: /var/run/docker.sock
# 添加共享卷用于访问JAR文件
- name: jar-cache
path: /localcache
2025-12-07 21:56:08 -05:00
# 触发器配置:针对 PR 和 tag 事件
# 注意tag事件不受branch限制所以移除branch条件
trigger:
2025-12-05 05:41:57 -05:00
event:
- pull_request
- tag
2025-12-07 21:56:08 -05:00
# 默认克隆步骤 (使用您本地已有的 drone/git:latest)
2025-11-28 19:00:26 +08:00
clone:
2025-12-05 12:21:23 +08:00
depth: 0
2025-12-05 11:23:48 +08:00
image: drone/git:latest
2025-12-07 21:56:08 -05:00
pull: false # 禁用镜像拉取
2025-11-28 19:00:26 +08:00
steps:
# PR时构建和测试
- name: java-build-test
image: registry.cn-beijing.aliyuncs.com/yinzy/maven:3.9-eclipse-temurin-8
pull: false
volumes:
- name: maven-cache
path: /localcache/maven/repository
environment:
MAVEN_OPTS: -Dmaven.repo.local=/localcache/maven/repository
commands:
- mkdir -p ~/.m2
# 动态生成 settings.xml仅用于 CI
- |
cat > ~/.m2/settings.xml << 'EOF'
<?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">
<mirrors>
<mirror>
<id>aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Aliyun Maven</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
</settings>
EOF
- mvn -s ~/.m2/settings.xml clean test package
# 验证 JAR 文件是否生成
- ls -lah target/*.jar || (echo "JAR file not found!" && exit 1)
when:
path:
include:
- pom.xml
event:
- pull_request
# Tag时构建跳过测试因为PR时已测试过
2025-11-28 19:00:26 +08:00
- name: java-build
2025-12-07 21:56:08 -05:00
image: registry.cn-beijing.aliyuncs.com/yinzy/maven:3.9-eclipse-temurin-8
2025-12-05 10:58:02 +08:00
pull: false
2025-12-07 21:56:08 -05:00
volumes:
- name: maven-cache
path: /localcache/maven/repository
- name: jar-cache
path: /localcache
environment:
2025-12-07 21:56:08 -05:00
MAVEN_OPTS: -Dmaven.repo.local=/localcache/maven/repository
2025-11-28 19:00:26 +08:00
commands:
2025-12-07 22:15:10 -05:00
- mkdir -p ~/.m2
2025-12-07 21:56:08 -05:00
# 动态生成 settings.xml仅用于 CI
- |
2025-12-07 21:56:08 -05:00
cat > ~/.m2/settings.xml << 'EOF'
<?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">
<mirrors>
<mirror>
<id>aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Aliyun Maven</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>
</mirrors>
</settings>
EOF
2025-12-07 21:56:08 -05:00
- mvn -s ~/.m2/settings.xml -DskipTests=true clean package
# 验证 JAR 文件是否生成
- ls -lah target/*.jar || (echo "JAR file not found!" && exit 1)
# 备份 JAR 文件到 /localcache/${DRONE_REPO_NAME}
- |
mkdir -p /localcache/${DRONE_REPO_NAME}
JAR_FILE=$(ls target/*.jar 2>/dev/null | head -1)
if [ -n "$JAR_FILE" ]; then
cp "$JAR_FILE" /localcache/${DRONE_REPO_NAME}/ -f
echo "✅ JAR file backed up to /localcache/${DRONE_REPO_NAME}/$(basename $JAR_FILE)"
ls -lh /localcache/${DRONE_REPO_NAME}/
else
echo "⚠️ No JAR file found to backup"
fi
2025-11-28 19:00:26 +08:00
when:
path:
include:
2025-12-07 21:56:08 -05:00
- pom.xml
event:
- tag
2025-12-07 21:56:08 -05:00
# Tag时构建Docker镜像
2025-12-07 22:18:43 -05:00
- name: docker_build
image: docker:latest
2025-12-07 22:18:43 -05:00
pull: false
volumes:
- name: docker_sock
path: /var/run/docker.sock
commands:
# 验证 Docker daemon 是否可用
- docker info || (echo "❌ Docker daemon not available" && exit 1)
2025-12-07 22:18:43 -05:00
# 验证 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: ${DRONE_REPO_NAME}:latest"
echo "📁 JAR file info:"
ls -lh target/drone-test-1.0.jar
docker build -t ${DRONE_REPO_NAME}:latest -f Dockerfile .
docker tag ${DRONE_REPO_NAME}:latest ${DRONE_REPO_NAME}:${DRONE_COMMIT_SHA:0:8}
echo "✅ Built Docker image: ${DRONE_REPO_NAME}:latest"
docker images ${DRONE_REPO_NAME}
depends_on:
- java-build
when:
path:
include:
- pom.xml
event:
- tag
# Tag时发布JAR文件到Gitea Release
- name: gitea_release
image: registry.cn-beijing.aliyuncs.com/yinzy/drone-plugins:gitea-release-latest
pull: false
volumes:
- name: jar-cache
path: /localcache
settings:
api_key:
from_secret: gitea_token
base_url:
from_secret: gitea_base_url
files:
- /localcache/${DRONE_REPO_NAME}/*.jar
title: Release ${DRONE_TAG}
note: |
Release ${DRONE_TAG}
Commit: ${DRONE_COMMIT_SHA:0:8}
Build: ${DRONE_BUILD_NUMBER}
file_exists: overwrite
depends_on:
- docker_build
when:
path:
include:
- pom.xml
event:
- tag
2025-11-28 19:00:26 +08:00
# 自动检测是否存在 Python (requirements.txt)
- name: python-test
2025-12-05 10:31:50 +08:00
image: python:3.12.0-slim
2025-12-05 10:58:02 +08:00
pull: false
2025-11-28 19:00:26 +08:00
commands:
2025-12-07 21:56:08 -05:00
# 临时设置 pip 源(仅本次会话)
- pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple/
- pip config set install.trusted-host pypi.tuna.tsinghua.edu.cn
2025-12-07 21:56:08 -05:00
- if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- if [ -f test_example.py ]; then python -m pytest test_example.py -v; fi
2025-11-28 19:00:26 +08:00
when:
2025-12-07 21:56:08 -05:00
branch:
exclude: [ "*" ]
2025-11-28 19:00:26 +08:00
# 最终报告(无论成功失败都运行)
- name: summary
image: alpine
2025-12-05 10:58:02 +08:00
pull: false
2025-11-28 19:00:26 +08:00
commands:
- echo "✅ CI completed at $(date)"
when:
status: [ success, failure ]