ServerChanPush2TelegramBot/ServerChanPush2TelegramBot_DockerBuilder.ps1
yshtcn c82fb1871d ### 更新日志 - 2024年6月15日
#### 新增功能
1. **配置文件路径调整**:
   - `bot_config.json`的读取路径移动到`/data/`目录,`bot_config_example.json`保留在原目录。
   - 日志文件存储路径移动到`/data/log/`目录。

2. **目录自动创建**:
   - 在程序启动时自动创建必要的目录(`/data`和`/data/log`),确保目录存在。

3. **配置文件不存在时自动复制**:
   - 当`bot_config.json`不存在时,自动将`bot_config_example.json`复制到指定位置,并写入日志,然后退出程序。

4. **API URL和代理配置读取**:
   - 将`api_url`和`proxies`配置移动到`bot_config.json`中读取,支持灵活配置反向代理。

5. **端口配置支持**:
   - 支持从配置文件中读取端口配置,默认端口为5000。注意:Docker环境中Gunicorn默认监听5000端口,建议使用端口映射指定端口。

6. **Docker支持**:
   - 编写Dockerfile以支持Docker环境运行。
   - 在Dockerfile中固定Gunicorn监听端口为5000,并确保Gunicorn绑定到`0.0.0.0`,从而对外部可访问。

#### 修复
1. **路径兼容性**:
   - 修复了Windows环境下目录和文件操作的兼容性问题,确保在不同平台下正常运行。

2. **日志和数据文件存储路径调整**:
   - 调整`received_data`和`sent_data`文件的存储路径到`/data/log`目录,统一管理日志文件。

#### 优化
1. **代码结构优化**:
   - 提升了代码的可读性和可维护性,简化了配置文件的读取和日志管理逻辑。
2024-06-15 03:56:36 +08:00

75 lines
2.5 KiB
PowerShell

# Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
# 检查是否以管理员权限运行
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
# 请求管理员权限
Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs
exit
}
# 切换到脚本所在目录
Set-Location $PSScriptRoot
Write-Host "当前目录已切换为脚本所在目录: $PSScriptRoot"
# 获取当前日期和时间
$dateTime = Get-Date -Format "yyyyMMdd"
Write-Host "当前日期: $dateTime"
# 输入提示并获取版本的最后一位
$revision = Read-Host -Prompt "请输入今天的版本次 ($dateTime,如果没有次,请直接回车)"
Write-Host "输入的版本次: $revision"
# 构建版本号
if ([string]::IsNullOrWhiteSpace($revision)) {
$version = "$dateTime"
} else {
$version = "$dateTime" + "_$revision"
}
Write-Host "构建的版本号: $version"
# 构建并打上版本号标签的 Docker 镜像
Write-Host "正在构建 Docker 镜像..."
$tempFileBuild = [System.IO.Path]::GetTempFileName()
docker build -t yshtcn/serverchanpush2telegrambot:$version . 2> $tempFileBuild
if ($LASTEXITCODE -ne 0) {
Write-Host "Docker 镜像构建失败" -ForegroundColor Red
Write-Host (Get-Content $tempFileBuild) -ForegroundColor Red
Remove-Item $tempFileBuild
exit
}
Write-Host "Docker 镜像构建成功"
Remove-Item $tempFileBuild
# 推送带有版本号标签的 Docker 镜像到 Docker Hub
Write-Host "正在推送 Docker 镜像到 Docker Hub..."
$tempFilePush = [System.IO.Path]::GetTempFileName()
docker push yshtcn/serverchanpush2telegrambot:$version 2> $tempFilePush
if ($LASTEXITCODE -ne 0) {
Write-Host "Docker 镜像推送失败" -ForegroundColor Red
Write-Host (Get-Content $tempFilePush) -ForegroundColor Red
Remove-Item $tempFilePush
exit
}
Write-Host "Docker 镜像推送成功"
Remove-Item $tempFilePush
# 为镜像打上 'latest' 标签并推送
Write-Host "正在为镜像打上 'latest' 标签并推送..."
$tempFilePushLatest = [System.IO.Path]::GetTempFileName()
docker tag yshtcn/serverchanpush2telegrambot:$version yshtcn/serverchanpush2telegrambot:latest
docker push yshtcn/serverchanpush2telegrambot:latest 2> $tempFilePushLatest
if ($LASTEXITCODE -ne 0) {
Write-Host "Docker 镜像 'latest' 标签推送失败" -ForegroundColor Red
Write-Host (Get-Content $tempFilePushLatest) -ForegroundColor Red
Remove-Item $tempFilePushLatest
exit
}
Write-Host "Docker 镜像 'latest' 标签推送成功"
Remove-Item $tempFilePushLatest
Write-Host "Docker 镜像构建和推送流程全部完成"