#!/usr/bin/env bash set -Eeuo pipefail # shellcheck source=lib.sh source "$(dirname -- "${BASH_SOURCE[0]}")/lib.sh" env_file="${1:-/etc/lehuo/lehuo.env}" require_env_file "$env_file" bash "$(dirname -- "${BASH_SOURCE[0]}")/preflight.sh" "$env_file" image_tag="$(env_value "$env_file" IMAGE_TAG)" release_dir='/opt/lehuo/releases' install -d -m 0700 "$release_dir" current_tag='' if [[ -f "$release_dir/current" ]]; then current_tag="$(<"$release_dir/current")" fi echo "构建不可变镜像:${image_tag}" compose "$env_file" build --pull api web echo '启动数据服务并初始化私有对象存储。' compose "$env_file" up -d mysql redis minio minio-init if [[ -n "$current_tag" ]]; then echo '迁移前创建生产数据库备份。' bash "$(dirname -- "${BASH_SOURCE[0]}")/backup.sh" "$env_file" fi echo '执行数据库 migration。' compose "$env_file" --profile tools run --rm migrate echo '部署 API 与管理后台网关。' compose "$env_file" up -d --remove-orphans api web bash "$(dirname -- "${BASH_SOURCE[0]}")/smoke.sh" "$env_file" if [[ -n "$current_tag" && "$current_tag" != "$image_tag" ]]; then printf '%s\n' "$current_tag" > "$release_dir/previous" fi printf '%s\n' "$image_tag" > "$release_dir/current" chmod 600 "$release_dir/current" [[ ! -f "$release_dir/previous" ]] || chmod 600 "$release_dir/previous" printf '部署完成,当前镜像标签:%s\n' "$image_tag"