[AWS]codedpoly+ec2+cli+jenkins部署方式規範文件
阿新 • • 發佈:2021-01-07
技術標籤:自動化部署jenkinsaws codedepolyawsshelldevopsjenkinsci/cd
[AWS]codedpoly+ec2+cli+jenkins部署方式規範文件
AWS CodeDeploy 自動化部署
第三方外掛實現代理部署EC2 以下是Windows部署示例
目錄
[AWS]codedpoly+ec2+cli+jenkins部署方式規範文件
before-install.bat 部署前先關閉服務再拷貝
一.準備
1.建立角色arn
arn:aws-cn:iam::641382364915:role/CodeDeployExampleRole 用於部署組配置
注意: 下面只需加AWSCodeDeployRole 比如加了AWSCodeDeployRoleForECS策略會出現windows 路徑是/不是\\
2.建立EC2
新增標籤 用於部署組配置 ,
進入機器內 安裝codedeloy代理中國區使用的香港的 國外使用對應的
3. appspec.yml 配置
version: 0.0 os: windows files: - source: patch destination: C:\Sobey\XDCamAir hooks: BeforeInstall: - location: scripts\before-install.bat ApplicationStart: - location: scripts\start_server.bat timeout: 6 ValidateService: - location: scripts\validate_server.bat
4.指令碼準備
before-install.bat 部署前先關閉服務再拷貝
@echo off
set exename=XdApi.exe
TaskList|Findstr /i "%exename%">Nul
If ErrorLevel 1 (
echo "NO EXIT :不存在此應用"
)else (
echo "EXIT :存在此應用 殺死它"
taskkill /f /im %exename%
)
scripts\start_server.bat
@echo off
REM 後臺執行 不是會導致部署一直在這和啟動的服務一起
%1 mshta vbscript:CreateObject("WScript.Shell").Run("%~s0 ::",0,FALSE)(window.close)&&exit
set mypath=C:\Sobey\XDCamAir
set exename=XdApi.exe
cd %mypath%
start %exename%
echo "啟動服務命令已執行"
exit 0
scripts\validate_server.bat
@echo off
set exename=AuthorizeManage.exe
TaskList|Findstr /i "%exename%">Nul
If ErrorLevel 1 (
echo "NO EXIT :不存在此應用 讓其報異常"
taskkill /f /im %exename%
)else (
echo "EXIT DEPLOY:存在此 部署應用"
)
5.S3壓縮檔案目錄結構
放在s3下面的zip目錄結構:
二.aws控制檯部署
1.建立應用 應用程式
2.建立部署組 注意: 環境配置 選 Amazon EC2 例項 選本地的例項不可行
3.建立部署 新增對應的s3地址的zip
三.jenkins+CLI指令碼部署
#自定義專案名
ProjectName="auth"
version=saas.1.0.0
tag=$(date +%Y%m%d%H%M)
zipname=${ProjectName}-${version}-${tag}".zip"
echo ----------------------------------------------構建開始-------------------------------------------------
dotnet build -c Release AuthorizeManage.sln
cd AuthorizeManage
dotnet publish -c Release -o ../publish
cd ..
cp Auth.AmazonSecurityTokenService/bin/Release/netstandard2.0/Auth.AmazonSecurityTokenService.dll -r publish/ControllerPlugin/netstandard2.0
cp Auth.CheckPlugin/bin/Release/netstandard2.0/Auth.CheckPlugin.dll -r publish/ControllerPlugin/netstandard2.0
cp Auth.CloudFrontSDK/bin/Release/netstandard2.0/Auth.CloudFrontSDK.dll -r publish/ControllerPlugin/netstandard2.0
cp Auth.Core/bin/Release/netstandard2.0/Auth.Core.dll -r publish/ControllerPlugin/netstandard2.0
echo -------------------------------------------------------更新aws EC2服務---------------------------------------------
#-----------------整合釋出包-------------
my_dir=codedeploy_publish
if [ ! -d "$my_dir" ]; then
echo "建立資料夾"
mkdir $my_dir
else
echo $my_dir ":資料夾已存在"
fi
cd codedeploy_publish
my_dir=patch
if [ ! -d "$my_dir" ]; then
echo "建立資料夾"
mkdir $my_dir
else
rm -fr $my_dir
mkdir $my_dir
echo $my_dir ":資料夾已存在"
fi
cp ../Auth.AmazonSecurityTokenService/bin/Release/netstandard2.0/Auth.AmazonSecurityTokenService.dll -r patch
cp ../Auth.CheckPlugin/bin/Release/netstandard2.0/Auth.CheckPlugin.dll -r patch
cp ../Auth.CloudFrontSDK/bin/Release/netstandard2.0/Auth.CloudFrontSDK.dll -r patch
cp ../Auth.Core/bin/Release/netstandard2.0/Auth.Core.dll -r patch
#cp ../publish/* -r patch
cp /other/aws-tasks/codedeploy/${ProjectName}/* -r . #-------------ec2上執行指令碼
#壓縮為zip 推送到s3 刪除本地zip
zip -q -r $zipname .
aws s3 cp $zipname s3://codedeploy-patch/${ProjectName}/
rm -fr $zipname
#釋出新部署 獲取部署後的id 等待給狀態
deploymentId=`aws deploy create-deployment \
--application-name SaasApplicationEC2 \
--deployment-config-name CodeDeployDefault.OneAtATime \
--deployment-group-name SaasWinDepoly \
--description "jenkins codedeploy deployment" \
--s3-location bucket=codedeploy-patch,bundleType=zip,key="auth/"$zipname | jq ".deploymentId" | tr -d '"'`
#檢視部署狀態
aws deploy wait deployment-successful --deployment-id $deploymentId