目标
实现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