109 lines
3.0 KiB
YAML
109 lines
3.0 KiB
YAML
name: 'Reusable Docker Compose Deploy'
|
||
description: '通用的 Docker Compose 部署逻辑,用于在各仓库中复用'
|
||
|
||
inputs:
|
||
compose_file:
|
||
description: 'Docker Compose 文件路径(相对于当前工作目录或 compose_dir)'
|
||
required: true
|
||
compose_dir:
|
||
description: 'Docker Compose 工作目录(相对于当前工作目录,默认使用 compose_file 所在目录)'
|
||
required: false
|
||
default: ''
|
||
startup_wait:
|
||
description: '服务启动后等待时间(秒)'
|
||
required: false
|
||
default: '0'
|
||
|
||
runs:
|
||
using: 'composite'
|
||
steps:
|
||
- name: Prepare compose paths
|
||
id: prepare
|
||
shell: bash
|
||
run: |
|
||
set -e
|
||
|
||
COMPOSE_FILE="${{ inputs.compose_file }}"
|
||
COMPOSE_DIR_INPUT="${{ inputs.compose_dir }}"
|
||
|
||
if [ -z "$COMPOSE_DIR_INPUT" ]; then
|
||
DIR="$(dirname "$COMPOSE_FILE")"
|
||
if [ "$DIR" = "." ]; then
|
||
DIR="."
|
||
fi
|
||
else
|
||
DIR="$COMPOSE_DIR_INPUT"
|
||
fi
|
||
|
||
echo "compose_file=$COMPOSE_FILE" >> "$GITHUB_OUTPUT"
|
||
echo "compose_dir=$DIR" >> "$GITHUB_OUTPUT"
|
||
|
||
echo "Compose file: $COMPOSE_FILE"
|
||
echo "Compose dir : $DIR"
|
||
|
||
- name: Stop old services
|
||
shell: bash
|
||
run: |
|
||
echo "====== 停止旧服务 ======"
|
||
|
||
cd "${{ steps.prepare.outputs.compose_dir }}"
|
||
|
||
# 停止并移除容器
|
||
docker compose -f "${{ steps.prepare.outputs.compose_file }}" down || true
|
||
|
||
echo "✓ 旧服务已停止"
|
||
echo ""
|
||
|
||
- name: Deploy services
|
||
shell: bash
|
||
run: |
|
||
echo "====== 部署服务 ======"
|
||
|
||
cd "${{ steps.prepare.outputs.compose_dir }}"
|
||
|
||
echo "启动服务..."
|
||
docker compose -f "${{ steps.prepare.outputs.compose_file }}" up -d
|
||
|
||
echo "✓ 服务已启动"
|
||
echo ""
|
||
|
||
- name: Wait for services
|
||
if: ${{ inputs.startup_wait != '0' }}
|
||
shell: bash
|
||
run: |
|
||
echo "====== 等待服务初始化 ======"
|
||
echo "等待时间: ${{ inputs.startup_wait }} 秒"
|
||
sleep ${{ inputs.startup_wait }}
|
||
echo "✓ 等待完成"
|
||
echo ""
|
||
|
||
- name: Verify services
|
||
shell: bash
|
||
run: |
|
||
echo "====== 验证服务状态 ======"
|
||
|
||
cd "${{ steps.prepare.outputs.compose_dir }}"
|
||
|
||
docker compose -f "${{ steps.prepare.outputs.compose_file }}" ps
|
||
echo ""
|
||
|
||
# 检查是否有失败的服务
|
||
FAILED_SERVICES=$(docker compose -f "${{ steps.prepare.outputs.compose_file }}" ps --status exited --format json | jq -r '.Service' 2>/dev/null || true)
|
||
|
||
if [ -n "$FAILED_SERVICES" ]; then
|
||
echo "⚠ 以下服务启动失败:"
|
||
echo "$FAILED_SERVICES"
|
||
echo ""
|
||
echo "查看失败服务的日志:"
|
||
for service in $FAILED_SERVICES; do
|
||
echo ""
|
||
echo "=== $service 日志 ==="
|
||
docker compose -f "${{ steps.prepare.outputs.compose_file }}" logs --tail=50 "$service"
|
||
done
|
||
exit 1
|
||
else
|
||
echo "✓ 所有服务运行正常"
|
||
fi
|
||
|
||
|