background
Program deployment has always been a very troublesome and boring thing. Companies generally have devops solutions. The entire ci/cd process involves many tools, which is quite troublesome to build. So what should I do for some of my own small projects and don't want to build such an environment... I tried Alibaba Cloud Effect pipeline + gitee + ecs some time ago, but it is quite convenient. It is mainly free ^ ^. You can also use self-built servers or other servers. Let's share how to use them below.
Code preparation
Prepare a random demo project and submit it to gitee
## 创建aspnetcore web项目
dotnet new web -o aspnetcoredemo

server environment
First, I go to the server to install the dotnet runtime. I use centos here.
参考官方文档 在 CentOS 上安装 .NET - .NET | Microsoft Docs
## 安装 .NET 之前,请运行以下命令,将 Microsoft 包签名密钥添加到受信任密钥列表,并添加 Microsoft 包存储库。 打开终端并运行以下命令:
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
## 通过 ASP.NET Core 运行时,可以运行使用 .NET 开发且未提供运行时的应用。 以下命令将安装 ASP.NET Core 运行时,这是与 .NET 最兼容的运行时。 在终端中,运行以下命令:
sudo yum install aspnetcore-runtime-6.0
Installation complete:

automatic deployment
Enter the cloud efficiency platform assembly line

Select the. NET Core pipeline template and create

Configure the pipeline
- The first step is to configure the pipeline source
Choose the code source: code cloud (of course, you can also choose something else, github, self-built git, etc.)
Authorize it, and then select your code warehouse, the default branch name. Write one of the following working directories, for example: demo

- The second step is configuration and construction


It is mainly about executing commands and packaging paths. Note that other options will work by default.
## cd到项目目录
cd aspnetcoredemo
## 还原项目
dotnet restore
## 发布项目
dotnet publish -c Release -o out
- Step 3 Configuration and deployment
I choose Alibaba Cloud ecs for the host group (you can also choose other non-Alibaba Cloud hosts with plug-ins installed)

Add a server connection and create authorization

Select the host, and next step, save (I only have one machine here, or I can deploy multiple machines)

Deployment script:

## 创建目录
mkdir -p /home/admin/aspnetcoredemo/
## 解压文件到 /home/admin/aspnetcoredemo/ 目录
tar zxvf /home/admin/aspnetcoredemo/package.tgz -C /home/admin/aspnetcoredemo/
## 执行部署脚本
sh /home/admin/aspnetcoredemo/deploy.sh restart
deployment script
这个 deploy.sh 加到项目代码中,这个脚本的大概内容就是 杀死进程->重新启动程序->健康检查->部署完成
The content is as follows:
#!/bin/bash
# 修改APP_NAME为云效上的应用名
APP_NAME=aspnetcoredemo
PROG_NAME=$0
ACTION=$1
APP_START_TIMEOUT=20 # 等待应用启动的时间
APP_PORT=5000 # 应用端口
HEALTH_CHECK_URL=http://127.0.0.1:${APP_PORT}/HealthChecks # 应用健康检查URL
HEALTH_CHECK_FILE_DIR=/home/admin/status # 脚本会在这个目录下生成nginx-status文件
APP_HOME=/home/admin/${APP_NAME} # 从package.tgz中解压出来的dll放到这个目录下
DLL_NAME=${APP_HOME}/${APP_NAME}.dll # dll的名字
DLL_OUT=${APP_HOME}/logs/start.log #应用的启动日志
# 创建出相关目录
mkdir -p ${HEALTH_CHECK_FILE_DIR}
mkdir -p ${APP_HOME}
mkdir -p ${APP_HOME}/logs
usage() {
echo "Usage: $PROG_NAME {start|stop|restart}"
exit 2
}
health_check() {
exptime=0
echo "checking ${HEALTH_CHECK_URL}"
while true
do
status_code=`/usr/bin/curl -L -o /dev/null --connect-timeout 5 -s -w %{http_code} ${HEALTH_CHECK_URL}`
if [ "$?" != "0" ]; then
echo -n -e "\rapplication not started"
else
echo "code is $status_code"
if [ "$status_code" == "200" ];then
break
fi
fi
sleep 1
((exptime++))
echo -e "\rWait app to pass health check: $exptime..."
if [ $exptime -gt ${APP_START_TIMEOUT} ]; then
echo 'app start failed'
exit 1
fi
done
echo "check ${HEALTH_CHECK_URL} success"
}
start_application() {
echo "starting dotnet process"
# chmod +x ${DLL_NAME}
# chmod +x ${APP_HOME}/appsettings.json
# nohup dotnet ${DLL_NAME} Urls=http://*:${APP_PORT} > ${DLL_OUT} 2>&1 &
cd ${APP_HOME}
nohup dotnet ${APP_NAME}.dll Urls=http://*:${APP_PORT} > ${DLL_OUT} 2>&1 &
echo "started dotnet process"
}
stop_application() {
checkdotnetpid=`ps -ef | grep dotnet | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'`
if [[ ! $checkdotnetpid ]];then
echo -e "\rno dotnet process"
return
fi
echo "stop dotnet process"
times=60
for e in $(seq 60)
do
sleep 1
COSTTIME=$(($times - $e ))
checkdotnetpid=`ps -ef | grep dotnet | grep ${APP_NAME} | grep -v grep |grep -v 'deploy.sh'| awk '{print$2}'`
if [[ $checkdotnetpid ]];then
kill -9 $checkdotnetpid
echo -e "\r -- stopping dotnet lasts `expr $COSTTIME` seconds."
else
echo -e "\rdotnet process has exited"
break;
fi
done
echo ""
}
start() {
start_application
health_check
}
stop() {
stop_application
}
case "$ACTION" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
usage
;;
esac

Remember to copy to the output directory:

Add a HealthChecks interface for health checks of deployment scripts:

manual building
Click on the pipeline to run. If there are no problems with the previous configuration, you can see that the build and deployment were successful.

Visit, ok:

automated build
The following is configured through webhook to implement submitting code and automatically build and deploy
Pipeline, select trigger configuration, and open webhook trigger:

Copy this webhook address, configure it into your gitee repository, and save:

Next, modify the code casually and test it:

Pipeline build and deployment are automatically triggered after submitting the code:

ok:

end
Happy coding ...
-This article is edited using [Typora]+[EasyBlogImageForTypora]
Welcome to pay attention to my public account and learn together.
If this article is helpful to you, you can click the [Recommended] button at the bottom right to support it; if there is anything inappropriate in the article, please correct it. Thank you very much!!!
