ff
This commit is contained in:
76
scripts/docker/build-push.ps1
Normal file
76
scripts/docker/build-push.ps1
Normal file
@@ -0,0 +1,76 @@
|
||||
#Requires -Version 5.1
|
||||
<#
|
||||
.SYNOPSIS
|
||||
构建官网镜像并推送到私有 Registry(对齐 bd-server/scripts/push-main-image.ps1)
|
||||
|
||||
.DESCRIPTION
|
||||
使用前: docker login docker-registry.tianyuanapi.com
|
||||
默认镜像: docker-registry.tianyuanapi.com/haiyushuke/website:latest
|
||||
|
||||
.EXAMPLE
|
||||
.\scripts\docker\build-push.ps1
|
||||
.\scripts\docker\build-push.ps1 -ExportTar
|
||||
#>
|
||||
param(
|
||||
[string]$Registry = "docker-registry.tianyuanapi.com",
|
||||
[string]$ImagePath = "haiyushuke/website",
|
||||
[string]$SiteUrl = "https://www.haiyushuke.com",
|
||||
[string]$BaiduVerification = "",
|
||||
[string]$NodeImage = "docker.m.daocloud.io/library/node:20-alpine",
|
||||
[switch]$ExportTar
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
$Tag = "latest"
|
||||
$ProjectRoot = Resolve-Path (Join-Path $PSScriptRoot "../..")
|
||||
$FullImage = "${Registry}/${ImagePath}:${Tag}"
|
||||
$LocalName = "haiyushuke-website:build"
|
||||
$TarPath = Join-Path $PSScriptRoot "haiyushuke-website-latest.tar"
|
||||
|
||||
Set-Location $ProjectRoot
|
||||
|
||||
Write-Host "==> Pre-pull base image: $NodeImage"
|
||||
docker pull $NodeImage
|
||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||
|
||||
Write-Host "==> build $LocalName"
|
||||
$buildArgs = @(
|
||||
"build",
|
||||
"-t", $LocalName,
|
||||
"--build-arg", "NODE_IMAGE=$NodeImage",
|
||||
"--build-arg", "NEXT_PUBLIC_SITE_URL=$SiteUrl"
|
||||
)
|
||||
if ($BaiduVerification) {
|
||||
$buildArgs += @("--build-arg", "BAIDU_SITE_VERIFICATION=$BaiduVerification")
|
||||
}
|
||||
$buildArgs += "."
|
||||
docker @buildArgs
|
||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||
|
||||
if ($ExportTar) {
|
||||
Write-Host "==> tag $FullImage (for save)"
|
||||
docker tag $LocalName $FullImage
|
||||
Write-Host "==> save $TarPath"
|
||||
docker save -o $TarPath $FullImage
|
||||
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
|
||||
Write-Host "Done (tar): $TarPath"
|
||||
exit 0
|
||||
}
|
||||
|
||||
Write-Host "==> tag $FullImage"
|
||||
docker tag $LocalName $FullImage
|
||||
|
||||
Write-Host "==> push $FullImage"
|
||||
docker push $FullImage
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
Write-Host ""
|
||||
Write-Host "Push failed. Check:" -ForegroundColor Yellow
|
||||
Write-Host " docker login $Registry" -ForegroundColor Yellow
|
||||
Write-Host (' curl -sI "https://{0}/v2/{1}/manifests/latest"' -f $Registry, $ImagePath) -ForegroundColor Yellow
|
||||
Write-Host "Registry host must be docker-registry.tianyuanapi.com (not docker-register...)." -ForegroundColor Yellow
|
||||
exit $LASTEXITCODE
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "Done. Pushed: $FullImage"
|
||||
Write-Host "Deploy host: cd scripts/docker && ./deploy-pull.sh"
|
||||
15
scripts/docker/deploy-load-tar.sh
Normal file
15
scripts/docker/deploy-load-tar.sh
Normal file
@@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
# 无法 pull 时:本机 build-push.ps1 -ExportTar,拷 tar 到服务器后执行
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
TAR_FILE="${1:-${SCRIPT_DIR}/haiyushuke-website-latest.tar}"
|
||||
ENV_FILE="${ENV_FILE:-${SCRIPT_DIR}/deploy.env}"
|
||||
COMPOSE_FILE="${SCRIPT_DIR}/docker-compose.yml"
|
||||
|
||||
[[ -f "${ENV_FILE}" ]] || { echo "missing ${ENV_FILE}" >&2; exit 1; }
|
||||
[[ -f "${TAR_FILE}" ]] || { echo "missing tar: ${TAR_FILE}" >&2; exit 1; }
|
||||
|
||||
cd "${SCRIPT_DIR}"
|
||||
docker load -i "${TAR_FILE}"
|
||||
docker compose --env-file "${ENV_FILE}" -f "${COMPOSE_FILE}" up -d
|
||||
42
scripts/docker/deploy-pull.sh
Normal file
42
scripts/docker/deploy-pull.sh
Normal file
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env bash
|
||||
# 部署机拉取 latest 并启动(对齐 bd-server/scripts/pull-main-on-deploy.sh)
|
||||
# 自测: curl -sI "https://docker-registry.tianyuanapi.com/v2/haiyushuke/website/manifests/latest"
|
||||
#
|
||||
# 用法:
|
||||
# cp deploy.env.example deploy.env
|
||||
# ./deploy-pull.sh
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
COMPOSE_FILE="${SCRIPT_DIR}/docker-compose.yml"
|
||||
ENV_FILE="${ENV_FILE:-${SCRIPT_DIR}/deploy.env}"
|
||||
|
||||
if [[ ! -f "${ENV_FILE}" ]]; then
|
||||
echo "missing ${ENV_FILE} (cp deploy.env.example deploy.env)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cd "${SCRIPT_DIR}"
|
||||
|
||||
echo "==> pull website (latest)"
|
||||
docker compose --env-file "${ENV_FILE}" -f "${COMPOSE_FILE}" pull
|
||||
|
||||
echo "==> up -d website"
|
||||
docker compose --env-file "${ENV_FILE}" -f "${COMPOSE_FILE}" up -d
|
||||
|
||||
# shellcheck disable=SC1090
|
||||
set -a
|
||||
source "${ENV_FILE}"
|
||||
set +a
|
||||
HOST_PORT="${HOST_PORT:-3000}"
|
||||
|
||||
echo "==> health http://127.0.0.1:${HOST_PORT}/"
|
||||
sleep 2
|
||||
if curl -fsS -o /dev/null "http://127.0.0.1:${HOST_PORT}/"; then
|
||||
echo "OK"
|
||||
else
|
||||
echo "WARN: curl failed; docker compose logs -f website"
|
||||
fi
|
||||
|
||||
echo "Done: ${REGISTRY}/${IMAGE_PATH}:latest"
|
||||
6
scripts/docker/deploy.env
Normal file
6
scripts/docker/deploy.env
Normal file
@@ -0,0 +1,6 @@
|
||||
REGISTRY=docker-registry.tianyuanapi.com
|
||||
IMAGE_PATH=haiyushuke/website
|
||||
CONTAINER_NAME=haiyushuke-website
|
||||
HOST_PORT=3000
|
||||
NEXT_PUBLIC_SITE_URL=https://www.haiyushuke.com
|
||||
BAIDU_SITE_VERIFICATION=
|
||||
9
scripts/docker/deploy.env.example
Normal file
9
scripts/docker/deploy.env.example
Normal file
@@ -0,0 +1,9 @@
|
||||
# cp deploy.env.example deploy.env
|
||||
# 与 bd-server 相同 Registry 主机名;镜像路径为命名空间/名称
|
||||
|
||||
REGISTRY=docker-registry.tianyuanapi.com
|
||||
IMAGE_PATH=haiyushuke/website
|
||||
CONTAINER_NAME=haiyushuke-website
|
||||
HOST_PORT=3000
|
||||
NEXT_PUBLIC_SITE_URL=https://www.haiyushuke.com
|
||||
BAIDU_SITE_VERIFICATION=
|
||||
14
scripts/docker/docker-compose.yml
Normal file
14
scripts/docker/docker-compose.yml
Normal file
@@ -0,0 +1,14 @@
|
||||
# 服务器部署:docker compose pull && up(镜像由 build-push.ps1 推送)
|
||||
# 自测: curl -sI "https://docker-registry.tianyuanapi.com/v2/haiyushuke/website/manifests/latest"
|
||||
|
||||
services:
|
||||
website:
|
||||
image: ${REGISTRY}/${IMAGE_PATH}:latest
|
||||
container_name: ${CONTAINER_NAME}
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "${HOST_PORT}:3000"
|
||||
environment:
|
||||
NODE_ENV: production
|
||||
NEXT_PUBLIC_SITE_URL: ${NEXT_PUBLIC_SITE_URL}
|
||||
BAIDU_SITE_VERIFICATION: ${BAIDU_SITE_VERIFICATION:-}
|
||||
3
scripts/docker/docker-daemon.snippet.json
Normal file
3
scripts/docker/docker-daemon.snippet.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"insecure-registries": []
|
||||
}
|
||||
Reference in New Issue
Block a user