Net Core 半自動備份與發佈

 

目標

實現Net Core 基於Vs Code開發,半自動備份與發佈至Linux,採用Nginx做反向代理。

經歷

接觸.Net Core 一年多了,之前都是基於Windows平臺開發,服務器採用的是IIS,在很早就有Mono跨平臺,當時研究開發過一些Demo和小工具,如:數據庫操作類,但一直沒有正式運用到項目中。

一個偶然的機會,讓我放棄了使用多年的Windows,從而嘗試Ubuntu操作系統。幾經摸索,搭起了一套開發環境。IDE採用的是VS Code,布屬於Linux平臺,採用Nginx做代理。雖然,VS Code跨平臺,但功能並沒有visual studio那麼好用,很多操作都需要用命令來完成,比如:引用,添加項目依賴等,可以說在易用性上是不如windows平臺的。當然也有一方面因素是不熟練導致,畢竟VS Code 只有幾十兆。Visual Studio好幾個G,所以也是可以理解的。

經過一年多的摸索與項目中的實踐,現在對VS Code相對熟練,也因爲它是基於Linux平臺,可以通過批處理腳本來實現了半自動化部屬,相比以往的純人力打包發佈要來得簡單輕鬆。效率也能提高不少。現在,將這些分享給大家,希望對大家有所幫助。

環境準備

Linux 、Mysql、Mac(或Ubuntu)、VS Code、Nginx、Git、UnZip

步驟

搭建Linux上的發佈環境(.Net Core)

# rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm

# yum update
# yum install -y libunwind libicu
# yum install -y dotnet-sdk-2.1
# dotnet --version

資料參考:https://www.microsoft.com/net/download/linux-package-manager/ubuntu16-04/sdk-current

安裝Nginx

網上資料很多,不多說。

安裝Git

# yum install -y git

創建示例項目

  • 創建webapi項目
$ mkdir test.webapi
$ cd test.webapi/
$ dotnet new webapi
The template "ASP.NET Core Web API" was created successfully.
Processing post-creation actions...
Running 'dotnet restore' on ./test.webapi/test.webapi.csproj...
... 
Restore succeeded.
  • 添加到git倉庫

test.webapi$ git init
Initialized empty Git repository in /xxxx/project/test.webapi/.git/


test.webapi$ git add .
test.webapi$ git commit -m "init project"


[master (root-commit) ee71712] init project
 18 files changed, 11829 insertions(+)
 create mode 100644 Controllers/ValuesController.cs
 create mode 100644 Program.cs
 create mode 100644 Properties/launchSettings.json
 create mode 100644 Startup.cs
 create mode 100644 appsettings.Development.json
 create mode 100644 appsettings.json
 create mode 100644 obj/Debug/netcoreapp2.1/test.webapi.AssemblyInfo.cs
 create mode 100644 obj/Debug/netcoreapp2.1/test.webapi.AssemblyInfoInputs.cache
 create mode 100644 obj/Debug/netcoreapp2.1/test.webapi.RazorAssemblyInfo.cache
 create mode 100644 obj/Debug/netcoreapp2.1/test.webapi.RazorAssemblyInfo.cs
 create mode 100644 obj/Debug/netcoreapp2.1/test.webapi.assets.cache
 create mode 100644 obj/Debug/netcoreapp2.1/test.webapi.csproj.CoreCompileInputs.cache
 create mode 100644 obj/Debug/netcoreapp2.1/test.webapi.csprojResolveAssemblyReference.cache
 create mode 100644 obj/project.assets.json
 create mode 100644 obj/test.webapi.csproj.nuget.cache
 create mode 100644 obj/test.webapi.csproj.nuget.g.props
 create mode 100644 obj/test.webapi.csproj.nuget.g.targets
 create mode 100644 test.webapi.csproj


test.webapi$ git remote add origin https://gitee.com/f1fjj/test.webapi.git
test.webapi$ git push -u origin master --force
Counting objects: 25, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (25/25), 93.24 KiB | 3.33 MiB/s, done.
Total 25 (delta 2), reused 0 (delta 0)
remote: Powered By Gitee.com
To https://gitee.com/f1fjj/test.webapi.git
 + 75ed2ff...ee71712 master -> master (forced update
Branch 'master' set up to track remote branch 'master' from 'origin

注:因爲是首次提交,所以在push時採用的是加了--force參數,強制將本地同步至遠程

 git push -u origin master --force

編寫自動發佈腳本

在項目開發中,項目文件一般會很多,導致整個項目很大。如果每次更新都將項目文件上傳到服務器,效率不是最好的。可以採用git的特點,在服務端拉取修改的部份代碼進行編譯,然後發佈至應用目錄。而且,這部份可以採用腳本來完成。當程序需要發佈更新時,只需執行一句命令便能快捷的完成:拉取,編譯,備份,切換,啓動。站點受影響也就切換的幾秒。

  • 服務端git配置
# git clone https://gitee.com/f1fjj/test.webapi.git test.webapi
Cloning into 'test.webapi'...
remote: Enumerating objects: 25, done.
remote: Counting objects: 100% (25/25), done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 25 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (25/25), done.
  • 創建站點臨時的發佈目錄
# mkdir /data/www/test.webapi/release -p
  • vi publish.sh

前提是在服務端需要搭建好dotnet 的運行環境。

# mkdir /data/www/test.webapi
# cd /data/www/test.webapi/
# vi publish.sh 


#!/bin/bash
cd /data/git/test.webapi/
git checkout .
git pull


echo "刪除發佈目錄"
sudo rm -rf /data/www/test.webapi/release/


sudo mkdir /data/www/test.webapi/release -p


sudo dotnet publish -o /data/www/test.webapi/release -c release


current_date=`date -d "-1 day" "+%Y%m%d%H%M"`


echo "bak path is:$current_date"


echo "刪除15天前的日誌文件"
sudo find /data/www/test.webapi/logs/ -mtime +15 -name "*.log" -exec rm -rf {} \;


sudo echo "刪除15天前的備份">>restart.log
sudo find /data/www/ -mtime +15 -name "20*.zip" -exec rm -rf {} \;


echo "清空nohup.out文件"
sudo echo /dev/null > /data/www/test.webapi/nohup.out
echo "開始備份"
sudo echo "做爲版本記錄:$current_date" > /data/www/test.webapi/version.out
sudo zip -r /data/www/$current_date.zip /data/www/test.webapi/*  > /dev/null 2>&1


echo "備份完成"
id=`sudo ps -aux |grep "dotnet"| awk '{if($12 == "'/data/www/test.webapi/test.webapi.dll'") {print $2}}'`
if [ "$id" == "" ]; then
        echo "dotnet test.webapi.dll is not running..."
else
        kill -9 $id
        echo "already kill test.webapi.dll."
fi
echo "將發佈後的文件拷貝到着站點"
sudo \cp -r /data/www/test.webapi/release/* /data/www/test.webapi/


cd /data/www/test.webapi

sudo rm -rf /data/www/test.webapi/version.out

echo "準備啓動"
sudo nohup /usr/share/dotnet/dotnet /data/www/test.webapi/test.webapi.dll &


echo "成功後打印進程"
ps -aux|grep dotnet


echo "臨控日誌"

:wq 保存退出

測試

  • 修改點內容Program.cs,改個端口吧
public static IWebHostBuilder CreateWebHostBuilder (string[] args) =>
            WebHost.CreateDefaultBuilder (args)
            .UseUrls ("http://0.0.0.0:52088")
            .UseStartup<Startup> ();

修改好後git提交併同步。

# bash publish.sh 
[root@iZk1a6ytdlnw9ome76tdlhZ test.webapi]# bash publish.sh 
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From https://gitee.com/f1fjj/test.webapi
   ee71712..bb513ff  master     -> origin/master
Updating ee71712..bb513ff
Fast-forward
 Program.cs | 1 +
 1 file changed, 1 insertion(+)
刪除發佈目錄
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restoring packages for /data/git/test.webapi/test.webapi.csproj...
  Generating MSBuild file /data/git/test.webapi/obj/test.webapi.csproj.nuget.g.props.
  Generating MSBuild file /data/git/test.webapi/obj/test.webapi.csproj.nuget.g.targets.
  Restore completed in 620.13 ms for /data/git/test.webapi/test.webapi.csproj.
  test.webapi -> /data/git/test.webapi/bin/release/netcoreapp2.1/test.webapi.dll
  test.webapi -> /data/www/test.webapi/release/
bak path is:201907221531
刪除15天前的日誌文件
find: ‘/data/www/test.webapi/logs/’: No such file or directory
清空nohup.out文件
開始備份
備份完成
dotnet test.webapi.dll is not running...
將發佈後的文件拷貝到着站點
準備啓動
成功後打印進程
nohup: appending output to ‘nohup.out’
root     26722  0.0  0.0 241168  4612 pts/4    S+   15:31   0:00 sudo nohup /usr/share/dotnet/dotnet /data/www/test.webapi/test.webapi.dll
root     26724  0.0  0.0 112708   972 pts/4    S+   15:31   0:00 grep test.webapi
臨控日誌
/dev/null
Hosting environment: Production
Content root path: /data/www/test.webapi
Now listening on: http://0.0.0.0:52088
Application started. Press Ctrl+C to shut down.

成功,以後更新代碼時,只需要執行bash publish.sh即可。如果發生問題,也可以通過命令腳本很快的回到前面的版本。

  • Nginx代理

此步驟省略

相關源代碼

項目:https://gitee.com/f1fjj/test.webapi

publish 文件:https://gitee.com/f1fjj/test.webapi/blob/master/publish.sh

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