ddddww
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
编码猿
2025-09-09 10:28:41 +08:00
parent e8e3ef4c28
commit aac7cbf23e

View File

@@ -1,6 +1,6 @@
kind: pipeline
type: docker
name: nodejs-host-network-pipeline
name: nodejs-pipeline-with-cleanup
steps:
# 步骤0清理残留容器和镜像优先执行
@@ -11,28 +11,29 @@ steps:
path: /var/run/docker.sock
commands:
- |
# 定义应用相关的名称标识
APP_NAME="node-app"
CONTAINER_NAME="node-app-prod"
CURRENT_TAG="${DRONE_COMMIT_SHA:0:8}"
echo "开始清理残留资源..."
# 1. 停止并删除正在运行的旧容器
if [ "$(docker ps -q -f name=node-app-prod)" ]; then
echo "停止并删除运行中的容器: node-app-prod"
docker stop node-app-prod || true
docker rm node-app-prod || true
if [ "$(docker ps -q -f name=$CONTAINER_NAME)" ]; then
echo "停止并删除运行中的容器: $CONTAINER_NAME"
docker stop $CONTAINER_NAME || true
docker rm $CONTAINER_NAME || true
else
echo "没有运行中的 node-app-prod 容器"
echo "没有运行中的 $CONTAINER_NAME 容器"
fi
# 2. 删除同前缀的旧镜像(排除当前构建标签)
echo "清理旧镜像..."
# 只处理有ID和标签的镜像跳过空值
docker images --filter "reference=node-app:*" --format "{{.ID}} {{.Tag}}" | \
docker images --filter "reference=$APP_NAME:*" --format "{{.ID}} {{.Tag}}" | \
while read -r image_id tag; do
# 检查image_id和tag是否有效
if [ -n "$image_id" ] && [ -n "$tag" ] && [ "$tag" != "${DRONE_COMMIT_SHA:0:8}" ] && [ "$tag" != "<none>" ]; then
echo "删除旧镜像: node-app:$tag (IMAGE_ID: $image_id)"
if [ "$tag" != "$CURRENT_TAG" ] && [ "$tag" != "<none>" ]; then
echo "删除旧镜像: $APP_NAME:$tag (IMAGE_ID: $image_id)"
docker rmi -f $image_id || true
else
echo "跳过无效镜像: ID=$image_id, Tag=$tag"
fi
done
@@ -40,36 +41,46 @@ steps:
echo "清理悬空镜像..."
dangling_images=$(docker images -f "dangling=true" -q)
if [ -n "$dangling_images" ]; then
# 确保只传递非空ID
echo "$dangling_images" | xargs -I {} docker rmi -f {} || true
docker rmi -f $dangling_images || true
else
echo "没有悬空镜像需要清理"
fi
# 第一步:准备环境并安装依赖
# 步骤1安装Node.js依赖
- name: install-dependencies
image: node:18-bullseye
image: node:18-bullseye-slim
commands:
- apt-get update && apt-get install -y iproute2
- ip -V
- npm install
volumes:
- name: node_modules
path: /app/node_modules
# 第三构建Docker镜像
- name: build-image
# 步骤2构建Docker镜像
- name: build-app-image
image: docker:20.10
volumes:
- name: docker-socket
path: /var/run/docker.sock
- name: node_modules
path: /app/node_modules
path: /drone/src/node_modules
commands:
- docker build -t node-app:${DRONE_COMMIT_SHA:0:8} -f Dockerfile .
# 第四步使用host网络运行应用
- name: run-with-host-network
# 步骤3测试容器运行
- name: test-container
image: docker:20.10
volumes:
- name: docker-socket
path: /var/run/docker.sock
commands:
- |
docker run --rm \
--network host \
node-app:${DRONE_COMMIT_SHA:0:8} \
sh -c "ip -V && npm start -- --version"
# 步骤4部署容器
- name: deploy
image: docker:20.10
volumes:
- name: docker-socket
@@ -99,4 +110,3 @@ trigger:
event:
- push
- pull_request