Bat批處理實現Docker下netcore一鍵發佈

背景:VS2017、VS2019,ftp一鍵發佈無效(症狀:ftp發佈提示發佈成功,實際文件未更新,勾選刪除原文件可以發佈,但是每次都需要全量發佈,靜態文件夾下保存有圖片等其他文件,不能刪除)

環境:Linux+Docker,Docker使用外部Ftp共享文件夾

隨有了使用批處理實現一鍵發佈的想法

思路:

1.dotnet命令行發佈(使用現有的發佈配置文件)

2.使用批處理按文件修改時間清理文件(只保留有修改的文件)

3.使用pscp上傳到服務器

4.使用plink遠程執行shell腳本重啓容器

腳本實現如下:

@echo off
@rem ======配置開始Begin==============================
@rem 【待發布項目路徑】
set "ProjectPath=%~dp0JXNH.Archives.WebHost.csproj"
@rem 【發佈配置文件路徑】
set "PublishConfigPath=%~dp0Properties\PublishProfiles\本機發布到文件夾.pubxml"
@rem 【發佈文件輸出路徑】
set "PublishPath=D:\publish\marridb發佈\JXNH.Archives\"
@rem 【測試機站點路徑】
set "SitePath=/home/JXNH.Archives"
@rem 【需執行shell腳本】
set "ShellCommand=sh /home/dockerscript/jxnh.archivesstart.sh"
set "ServerIP=10.0.0.1"
set "ServerUser=root"
set "ServerPassword=123"
@rem ======配置結束End===============================
echo --------發佈站點ing...-------------------
dotnet publish -o %PublishPath% %ProjectPath% /p:PublishProfile=%PublishConfigPath%
echo --------發佈成功!----------------------
echo --------清理文件ing...-------------------
@rem 刪除配置文件
del /f/s/q %PublishPath%\*.config
@rem 刪除pdb文件
del /f/s/q %PublishPath%\*.pdb
@rem 刪除3天前的文件
echo y |ForFiles /p %PublishPath% /s /d -3 /c "cmd /c del @file"
echo -------文件清理成功!-------------------------------
TIMEOUT /T 1
echo -------發佈到linux測試機ing...-----------------------
.\pscp -pw %ServerPassword% -r %PublishPath% %ServerUser%@%ServerIP%:%SitePath%
echo -------發佈到linux測試機成功!---------------------
echo -------執行shell腳本ing...----------------------------
.\plink -no-antispoof -pw %ServerPassword% %ServerUser%@%ServerIP% %ShellCommand%
echo -------shell腳本執行完畢!--------------------------
pause

shell腳本:jxnh.archivesstart.sh

#! /bin/bash
CID=`docker ps -f name=jxnh.archives |awk '{print $1}'|cut -d " " -f 2`
echo "get message:"$CID
if [ "$CID"x = "CONTAINER"x ];
then
 echo "not found container!"
 ACID=`docker ps -a -f name=jxnh.archives |awk '{print $1}'|cut -d " " -f 2`;
 DACID=$(echo $ACID |cut -d " " -f 2);
 docker rm $DACID;
 docker run --privileged -v /home/JXNH.Archives:/app --name jxnh.archives  -p 8010:8010 -d archives /usr/bin/dotnet /app/JXNH.Archives.WebHost.dll;
else
 DDS=$(echo $CID |cut -d " " -f 2)
 echo "get container id is:":$DDS
 docker stop $DDS
 docker rm $DDS
 docker run --privileged -v /home/JXNH.Archives:/app --name jxnh.archives  -p 8010:8010 -d archives /usr/bin/dotnet /app/JXNH.Archives.WebHost.dll
fi

CIDD=`docker ps -f name=jxnh.archives |awk '{print $1}'|cut -d " " -f 2`;
if [ "$CIDD"x = "CONTAINER"x ]
then
 echo "shell script run docker failed!"
else
 echo "run success!"
fi

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章