OpenAuth.Net/newdocs/docs/notes/core/deployapi.md

126 lines
4.2 KiB
Markdown
Raw Normal View History

2025-04-23 23:37:58 +08:00
---
title: 部署API
createTime: 2025/04/23 21:03:10
permalink: /core/deployapi/
---
## 生成发布文件
* 修改部署环境的连接字符串信息,特别注意是`appsettings.Production.json`文件:
![20220101232738](http://img.openauth.net.cn/20220101232738.png)
::: warning 注意
决定系统部署后读取`appsettings.json`还是`appsettings.Production.json`是通过操作系统的环境变量`ASPNETCORE_ENVIRONMENT`来控制的。
在centos切换成正式可以用
```shell
export ASPNETCORE_ENVIRONMENT=Production
```
或者修改/etc/profile配置在结尾添加
```shell
ASPNETCORE_ENVIRONMENT=Production
export ASPNETCORE_ENVIRONMENT
```
然后刷新:
```shell
source /etc/profile
```
在Widows系统中增加对应环境变量即可
:::
* 直接在解决方案资源管理器中选中OpenAuth.WebApi右键【发布】出现下面的配置框使用文件系统即可
![20220101232912](http://img.openauth.net.cn/20220101232912.png)
* 发布完成后可以在输出目录看到发布详情(红色框内即为发布的文件夹):
![20220101233049](http://img.openauth.net.cn/20220101233049.png)
* 将Debug目录中的OpenAuth.WebApi.xml拷贝到发布文件夹publish:
![20220101235436](http://img.openauth.net.cn/20220101235436.png)
2025-04-24 14:29:56 +08:00
## 部署&启动
2025-04-23 23:37:58 +08:00
2025-04-24 14:29:56 +08:00
如果是windows系统可以直接运行`OpenAuth.Mvc.exe`启动。
如果是linux系统将发布后的文件拷贝到服务器文件夹。直接使用`dotnet OpenAuth.WebApi.dll` 命令启动。
启动成功后使用浏览器打开[http://localhost:52789/swagger/index.html](http://localhost:52789/swagger/index.html) 即可访问,如下图所示:
2025-04-23 23:37:58 +08:00
![20220101233542](http://img.openauth.net.cn/20220101233542.png)
2025-04-24 14:29:56 +08:00
## docker部署
2025-04-23 23:37:58 +08:00
2025-04-24 14:29:56 +08:00
框架自带的dockerfile文件会同时生成webapi、mvc、identity三个项目可以根据需要调整后使用。使用方式如下
```shell
# 构建镜像
docker build -f Dockerfile -t openauthapi-img .
# 运行容器
docker run --network="host" -d -p 52789:52789 -p 1802:1802 -p 12796:12796 openauthapi-img
```
## jenkins无容器部署
2025-04-23 23:37:58 +08:00
OpenAuth.Net采用的是gitee托管源码只需使用Gitee WebHook构建触发器。配置如下
2025-04-24 00:36:50 +08:00
![2025-04-24-00-33-15](http://img.openauth.net.cn/2025-04-24-00-33-15.png)
2025-04-23 23:37:58 +08:00
做好上面的配置后代码提交时就会触发jenkins工作。剩下的就是编写自己的构建脚本。增加构建步骤选择执行Shell。并输入以下脚本
```shell
#!/bin/bash
kill -9 $(ps -ef|grep OpenAuth.WebApi.dll|grep -v grep|awk '{print $2}')
#export BUILD_ID=dontKillMe这一句很重要这样指定了项目启动之后才不会被Jenkins杀掉。
export BUILD_ID=dontKillMe
pwd
echo $PATH
dotnet restore
cd ./OpenAuth.WebApi
pwd
echo '============================begin build======================================='
dotnet build # 为了生成XML注释文件 用于swagger注释
rm -rf /data/openauthapi
mkdir /data/openauthapi
cp ./bin/Debug/netcoreapp3.1/OpenAuth.Repository.xml /data/openauthapi/
cp ./bin/Debug/netcoreapp3.1/OpenAuth.App.xml /data/openauthapi/
cp ./bin/Debug/netcoreapp3.1/Infrastructure.xml /data/openauthapi/
dotnet publish -c:Release -o /data/openauthapi # 如果服务器上有多个.NET版本加上目标版本号-f net6.0
nohup dotnet /data/openauthapi/OpenAuth.WebApi.dll &
#cp ./bin/Debug/netcoreapp2.0/您的项目路径.xml $WORKSPACE/jenkins_publish/ # 拷贝swagger注释
echo '============================end build======================================='
```
2025-04-24 14:29:56 +08:00
## jenkins容器部署
如果需要jenkins配合自动部署启动可以将上面的shell调整如下
```shell
# 停止并删除所有与 openauthapi-img 镜像相关的容器
docker ps -a --filter "ancestor=openauthapi-img" --format "{{.ID}}" | xargs -r docker stop
# 删除所有与 openauthapi-img 镜像相关的容器
docker ps -a --filter "ancestor=openauthapi-img" --format "{{.ID}}" | xargs -r docker rm
#docker rmi $(docker images | grep "^<none>" | awk "{print $3}")
cd /var/lib/jenkins/workspace/openauth.webapi
# 构建镜像
docker build -f Dockerfile -t openauthapi-img .
# 运行容器
docker run --network="host" -d -p 52789:52789 -p 1802:1802 -p 12796:12796 openauthapi-img
```
2025-04-23 23:37:58 +08:00