134 lines
4.6 KiB
PowerShell
134 lines
4.6 KiB
PowerShell
[CmdletBinding()]
|
||
param (
|
||
[Alias('target')]
|
||
[string]$BuildTarget = '',
|
||
[string]$MavenOutputDir = "./target",
|
||
[string]$DockerImgName = "usvisartrackapi",
|
||
[string]$DockerServerName = "usvisartrackapi",
|
||
[string]$CodeServerPort = "8282",
|
||
[string]$PublishServerPort = "8383",
|
||
[string]$BuildVerison = "0.0.5-snapshot",
|
||
[string]$PushServer = "repo-docker.ossez.com",
|
||
[string]$PushPath = "/docker-hub/"
|
||
)
|
||
|
||
#腾讯云镜像仓库
|
||
#[string]$PushServer = "ccr.ccs.tencentyun.com",
|
||
#[string]$PushPath = "/kiler398/"
|
||
|
||
#[string]$PushServer = "repo-docker.ossez.com",
|
||
#[string]$PushPath = "/docker-hub/"
|
||
|
||
function build {
|
||
ExecuteShellText "mvn clean package -DskipTests" "源码Maven编译打包"
|
||
}
|
||
|
||
function publishDockerToRemoteServer {
|
||
publishDockImage
|
||
|
||
$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" "清理本地无用镜像"
|
||
|
||
}
|
||
|
||
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容器"
|
||
}
|
||
|
||
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""" "导出镜像文件"
|
||
}
|
||
|
||
function exportProjectDockerDeployShell
|
||
{
|
||
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"
|
||
}
|
||
|
||
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
|
||
}
|
||
|