2022-10-08 11:14:23 -04:00
|
|
|
|
[CmdletBinding()]
|
|
|
|
|
param (
|
|
|
|
|
[Alias('target')]
|
|
|
|
|
[string]$BuildTarget = '',
|
|
|
|
|
[string]$MavenOutputDir = "./target",
|
|
|
|
|
[string]$DockerImgName = "usvisartrackapi",
|
|
|
|
|
[string]$DockerServerName = "usvisartrackapi",
|
|
|
|
|
[string]$CodeServerPort = "8282",
|
|
|
|
|
[string]$PublishServerPort = "8383",
|
2022-11-05 23:40:48 -04:00
|
|
|
|
[string]$BuildVerison = "0.0.5-snapshot",
|
2022-10-19 19:13:16 -04:00
|
|
|
|
[string]$PushServer = "repo-docker.ossez.com",
|
|
|
|
|
[string]$PushPath = "/docker-hub/"
|
2022-10-08 11:14:23 -04:00
|
|
|
|
)
|
|
|
|
|
|
2022-10-23 10:46:36 -04:00
|
|
|
|
#腾讯云镜像仓库
|
|
|
|
|
#[string]$PushServer = "ccr.ccs.tencentyun.com",
|
|
|
|
|
#[string]$PushPath = "/kiler398/"
|
|
|
|
|
|
2022-10-30 10:49:22 -04:00
|
|
|
|
#[string]$PushServer = "repo-docker.ossez.com",
|
|
|
|
|
#[string]$PushPath = "/docker-hub/"
|
|
|
|
|
|
2022-10-08 11:14:23 -04:00
|
|
|
|
function build {
|
2022-10-17 19:16:10 -04:00
|
|
|
|
ExecuteShellText "mvn clean package -DskipTests" "源码Maven编译打包"
|
|
|
|
|
}
|
2022-10-08 11:14:23 -04:00
|
|
|
|
|
2022-10-17 19:16:10 -04:00
|
|
|
|
function publishDockerToRemoteServer {
|
2022-10-08 11:14:23 -04:00
|
|
|
|
publishDockImage
|
|
|
|
|
|
2022-10-17 19:16:10 -04:00
|
|
|
|
$fullImageName = BuildDockerFullImageName $DockerImgName $BuildVerison
|
|
|
|
|
|
|
|
|
|
ExecuteShellText "docker login $PushServer" "登录远程Docker仓库"
|
|
|
|
|
ExecuteShellText "docker tag $fullImageName $PushServer$PushPath$fullImageName" "Docker镜像打标签"
|
|
|
|
|
ExecuteShellText "docker push $PushServer$PushPath$fullImageName" "Docker镜像推送到远程服务器"
|
|
|
|
|
ExecuteShellText "docker image prune -f" "清理本地无用镜像"
|
|
|
|
|
|
2022-10-08 11:14:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function publishDockImage()
|
|
|
|
|
{
|
|
|
|
|
BuildDockerImageToLocal "Dockerfile"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function runDockerContainer()
|
|
|
|
|
{
|
|
|
|
|
$split = ":"
|
|
|
|
|
ExecuteShellText "docker stop $DockerServerName" "停止Docker容器"
|
|
|
|
|
ExecuteShellText "docker rm $DockerServerName" "删除Docker容器"
|
|
|
|
|
$fullImageName = BuildDockerFullImageName $DockerImgName $BuildVerison
|
|
|
|
|
ExecuteShellText "docker run -p $PublishServerPort$split$CodeServerPort --name $DockerServerName -d $fullImageName " "启动Docker容器"
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-12 09:02:27 -04:00
|
|
|
|
function BuildDockerImageToLocal()
|
|
|
|
|
{
|
|
|
|
|
ExecuteShellText "docker image prune -f" "清理本地无用镜像"
|
|
|
|
|
$fullImageName = BuildDockerFullImageName $DockerImgName $BuildVerison
|
|
|
|
|
ExecuteShellText "docker build -f Dockerfile -t=""$fullImageName"" --label ""$DockerImgName"" --label ""$BuildVerison"" ." "编译镜像 $fullImageName"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function BuildDockerFullImageName($imageName, $buildImageVersionName)
|
|
|
|
|
{
|
|
|
|
|
return "$imageName`:$buildImageVersionName"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 导出项目Docker镜像
|
|
|
|
|
function exportProjectDockerImage
|
|
|
|
|
{
|
|
|
|
|
write-host ("开始导出项目docker镜像.")
|
|
|
|
|
|
|
|
|
|
$fullhostdockerImageFilePath = Join-Path $MavenOutputDir "$DockerImgName-$BuildVerison.tar"
|
|
|
|
|
|
|
|
|
|
Write-Host "导出镜像文件路径: $fullhostdockerImageFilePath"
|
|
|
|
|
|
|
|
|
|
$fullImageName = BuildDockerFullImageName $DockerImgName $BuildVerison
|
|
|
|
|
|
|
|
|
|
ExecuteShellText "docker save -o $fullhostdockerImageFilePath ""$fullImageName""" "导出镜像文件"
|
2022-10-08 11:14:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-12 09:02:27 -04:00
|
|
|
|
function exportProjectDockerDeployShell
|
2022-10-08 11:14:23 -04:00
|
|
|
|
{
|
2022-10-12 09:02:27 -04:00
|
|
|
|
write-host ("开始生成项目docker镜像部署脚本.")
|
|
|
|
|
|
|
|
|
|
$fullhostdockerDeployShellFilePath = Join-Path $MavenOutputDir "deploy.sh"
|
|
|
|
|
|
|
|
|
|
$fullImageFileName = "$DockerImgName-$BuildVerison.tar"
|
|
|
|
|
|
|
|
|
|
$dockImageName = BuildDockerFullImageName $DockerImgName $BuildVerison
|
|
|
|
|
|
|
|
|
|
$dockerImageDeployScript = "#!bin/sh`
|
|
|
|
|
# -------------------------------------------- Deploy Script Start --------------------------------------------
|
|
|
|
|
docker load -i $fullImageFileName `
|
|
|
|
|
docker stop $DockerImgName `
|
|
|
|
|
docker rm $DockerImgName `
|
|
|
|
|
docker run -p $PublishServerPort`:$CodeServerPort --restart=unless-stopped -e APP_DATABASE_HOST=`"localhost`" -e APP_DATABASE_PORT=`"3306`" -e APP_DATABASE_NAME=`"northtecom.usvisatrack`" -e APP_DATABASE_USERNAME=`"root`" -e APP_DATABASE_PASSWORD=`"asdf@123`" --name $DockerImgName -d $dockImageName `
|
|
|
|
|
# -------------------------------------------- Deploy Script End -------------------------------------------- "
|
|
|
|
|
|
|
|
|
|
echo $dockerImageDeployScript > $fullhostdockerDeployShellFilePath
|
|
|
|
|
|
|
|
|
|
Write-Host $dockerImageDeployScript
|
|
|
|
|
|
|
|
|
|
Write-Host "导出镜像部署脚本文件路径: $fullhostdockerDeployShellFilePath"
|
2022-10-08 11:14:23 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function buildDockerFile()
|
|
|
|
|
{
|
|
|
|
|
write-host ("执行 buildDockerFile")
|
|
|
|
|
$dockerFile = Join-Path $MavenOutputDir "Dockerfile"
|
|
|
|
|
$dockerFileContent = 'FROM bellsoft/liberica-openjdk-alpine:8u345-1' + "`n"
|
|
|
|
|
$dockerFileContent += 'VOLUME /tmp' + "`n"
|
|
|
|
|
$dockerFileContent += 'COPY target/*.jar app.jar' + "`n"
|
|
|
|
|
$dockerFileContent += 'EXPOSE 8282' + "`n"
|
|
|
|
|
$dockerFileContent += '# ENTRYPOINT ["java","-jar","/app.jar"]' + "`n"
|
|
|
|
|
write-host ("写入 Dockerfile 内容到 $dockerFile")
|
|
|
|
|
echo $dockerFileContent > $dockerFile
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# 执行文本命令
|
|
|
|
|
function ExecuteShellText($shellText,$shellMsg)
|
|
|
|
|
{
|
|
|
|
|
write-host ("$shellMsg 执行脚本:$shellText")
|
|
|
|
|
Invoke-Expression "& $shellText"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if($BuildTarget -eq '')
|
|
|
|
|
{
|
|
|
|
|
write-host ("未发现发现编译目标:$BuildTarget,操作跳过。")
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
& $BuildTarget
|
|
|
|
|
}
|
|
|
|
|
|