Blame view

deploy/prod2.sh 2.05 KB
zhongnnahuang authored
1
#!/bin/bash
2
# 变量定义
3
4
LAST_TAG="1.0.66"
TAG="1.0.67"
zhongnnahuang authored
5
6
7
8
9
TARGET_PATH="/root/web/canrud-outside-nuxt-front"
DOCKERFILE_PATH="/root/web/canrud-outside-nuxt-front/canrud-nuxt-front"
IMAGE_NAME="canrud-outside-front"
TAR_FILE="${TARGET_PATH}/${IMAGE_NAME}_${TAG}.tar"
DOCKER_COMPOSE_FILE="${TARGET_PATH}/docker-compose.yml"
10
zhongnnahuang authored
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 定义错误处理函数
handle_error() {
    echo "Error occurred at step: $1. Exiting."
    exit 1
}

# 步骤-1:修改 docker-compose.yml 文件
echo "Updating docker-compose.yml file..."
cat > "$DOCKER_COMPOSE_FILE" <<EOL
version: '3.8'
services:
  canrud-outside-front:
    image: ${IMAGE_NAME}:${TAG}  # 使用指定的镜像和标签
    container_name: canrud-outside-front
    ports:
      - "3000:3000"  # 映射端口
    environment:
      - BASE_URL=http://47.89.254.121:8002/shop
    restart: unless-stopped  # 如果容器退出,自动重启容器,除非是用户手动停止
EOL

# 检查文件是否已更新
if [ $? -ne 0 ]; then
    handle_error "Update docker-compose.yml"
fi

# 步骤0:拉取代码
echo "Pulling latest code..."
cd "${DOCKERFILE_PATH}" || handle_error "Change directory to ${DOCKERFILE_PATH}"
git pull || handle_error "Git pull"
41
42

# 步骤1: 构建 Docker 镜像
zhongnnahuang authored
43
44
echo "Building Docker image..."
docker build -t ${IMAGE_NAME}:${TAG} . || handle_error "Docker build"
45
46

# 步骤2: 将 Docker 镜像导出为 tar 文件
zhongnnahuang authored
47
48
echo "Saving Docker image to tar file..."
docker save -o ${TAR_FILE} ${IMAGE_NAME}:${TAG} || handle_error "Docker save"
49
50

# 步骤3:加载镜像
zhongnnahuang authored
51
52
echo "Loading Docker image from tar file..."
docker load -i "$TAR_FILE" || handle_error "Docker load"
53
54

# 步骤4:删除当前运行的容器
zhongnnahuang authored
55
56
57
echo "Stopping and removing the current container..."
docker stop canrud-outside-front || handle_error "Docker stop"
docker rm canrud-outside-front || handle_error "Docker rm"
58
59

# 步骤5:运行当前镜像
zhongnnahuang authored
60
61
62
63
64
echo "Starting container with the new image..."
cd "${TARGET_PATH}" || handle_error "Change directory to ${TARGET_PATH}"
docker-compose up -d || handle_error "Docker-compose up"

echo "All tasks completed successfully!"
65