This commit is contained in:
42
.drone.yml
42
.drone.yml
@@ -1,6 +1,6 @@
|
|||||||
kind: pipeline
|
kind: pipeline
|
||||||
type: docker
|
type: docker
|
||||||
name: nodejs-pipeline-with-cleanup
|
name: nodejs-host-network-pipeline
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
# 步骤0:清理残留容器和镜像(优先执行)
|
# 步骤0:清理残留容器和镜像(优先执行)
|
||||||
@@ -16,11 +16,13 @@ steps:
|
|||||||
CONTAINER_NAME="node-app-prod"
|
CONTAINER_NAME="node-app-prod"
|
||||||
CURRENT_TAG="${DRONE_COMMIT_SHA:0:8}"
|
CURRENT_TAG="${DRONE_COMMIT_SHA:0:8}"
|
||||||
|
|
||||||
echo "开始清理残留资源..."
|
echo "-----------------------"
|
||||||
|
echo "1 开始清理残留资源..."
|
||||||
|
|
||||||
|
|
||||||
# 1. 停止并删除正在运行的旧容器
|
# 停止并删除正在运行的旧容器
|
||||||
if [ "$(docker ps -q -f name=$CONTAINER_NAME)" ]; then
|
if [ "$(docker ps -q -f name=$CONTAINER_NAME)" ]; then
|
||||||
echo "停止并删除运行中的容器: $CONTAINER_NAME"
|
echo "1.1: 停止并删除运行中的容器: $CONTAINER_NAME"
|
||||||
docker stop $CONTAINER_NAME || true
|
docker stop $CONTAINER_NAME || true
|
||||||
docker rm $CONTAINER_NAME || true
|
docker rm $CONTAINER_NAME || true
|
||||||
else
|
else
|
||||||
@@ -28,6 +30,7 @@ steps:
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# 2. 删除同前缀的旧镜像(排除当前构建标签)
|
# 2. 删除同前缀的旧镜像(排除当前构建标签)
|
||||||
|
echo "-----------------------"
|
||||||
echo "清理旧镜像..."
|
echo "清理旧镜像..."
|
||||||
docker images --filter "reference=$APP_NAME:*" --format "{{.ID}} {{.Tag}}" | \
|
docker images --filter "reference=$APP_NAME:*" --format "{{.ID}} {{.Tag}}" | \
|
||||||
while read -r image_id tag; do
|
while read -r image_id tag; do
|
||||||
@@ -38,6 +41,7 @@ steps:
|
|||||||
done
|
done
|
||||||
|
|
||||||
# 3. 清理悬空镜像(构建失败产生的none标签镜像)
|
# 3. 清理悬空镜像(构建失败产生的none标签镜像)
|
||||||
|
echo "-----------------------"
|
||||||
echo "清理悬空镜像..."
|
echo "清理悬空镜像..."
|
||||||
dangling_images=$(docker images -f "dangling=true" -q)
|
dangling_images=$(docker images -f "dangling=true" -q)
|
||||||
if [ -n "$dangling_images" ]; then
|
if [ -n "$dangling_images" ]; then
|
||||||
@@ -46,41 +50,30 @@ steps:
|
|||||||
echo "没有悬空镜像需要清理"
|
echo "没有悬空镜像需要清理"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 步骤1:安装Node.js依赖
|
# 第一步:准备环境并安装依赖
|
||||||
- name: install-dependencies
|
- name: install-dependencies
|
||||||
image: node:18-bullseye-slim
|
image: node:18-bullseye
|
||||||
commands:
|
commands:
|
||||||
|
- apt-get update && apt-get install -y iproute2
|
||||||
|
- ip -V
|
||||||
- npm install
|
- npm install
|
||||||
volumes:
|
volumes:
|
||||||
- name: node_modules
|
- name: node_modules
|
||||||
path: /app/node_modules
|
path: /app/node_modules
|
||||||
|
|
||||||
# 步骤2:构建Docker镜像
|
# 第三步:构建Docker镜像
|
||||||
- name: build-app-image
|
- name: build-image
|
||||||
image: docker:20.10
|
image: docker:20.10
|
||||||
volumes:
|
volumes:
|
||||||
- name: docker-socket
|
- name: docker-socket
|
||||||
path: /var/run/docker.sock
|
path: /var/run/docker.sock
|
||||||
- name: node_modules
|
- name: node_modules
|
||||||
path: /drone/src/node_modules
|
path: /app/node_modules
|
||||||
commands:
|
commands:
|
||||||
- docker build -t node-app:${DRONE_COMMIT_SHA:0:8} -f Dockerfile .
|
- docker build -t node-app:${DRONE_COMMIT_SHA:0:8} -f Dockerfile .
|
||||||
|
|
||||||
# 步骤3:测试容器运行
|
# 第四步:使用host网络运行应用
|
||||||
- name: test-container
|
- name: run-with-host-network
|
||||||
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
|
image: docker:20.10
|
||||||
volumes:
|
volumes:
|
||||||
- name: docker-socket
|
- name: docker-socket
|
||||||
@@ -110,3 +103,4 @@ trigger:
|
|||||||
event:
|
event:
|
||||||
- push
|
- push
|
||||||
- pull_request
|
- pull_request
|
||||||
|
|
||||||
Reference in New Issue
Block a user