分享一个dotnet自动发布的脚本
作者:互联网
前置
准备一台CentOS7
安装 git、docker、docker-register本地仓库、dotnet
项目中添加Dockerfile、docker-stack.yml
脚本原理:
通过git拉取分支最新提交,执行dotnet publish 编译项目,
运行docker build 和docker push推到本地仓库中
运行docker stack deploy 发布 到Swarm集群
扩容缩容都是Swarm集群提供
可通过给Swarm集群中的机器打标签,通过docker-stack.yml 只发布到指定标签的机器
只是最近研究的demo代码,不应做为生产使用,出现问题,概不负责
#!/bin/bash # ========================================== # !!!项目根目录下必须包含 Dockerfile 和 docker-stack.yml 文件!!! # # ========================================== #@echo off echo work-dir: pwd # ========================================== # 项目Git地址 eg http://1.git projectGitPath=http://1.git # 分支名 eg master branchName=master # 项目名 projectName=my_maintain # dotnet发布项目 eg ~/projects/app1/app1.csproj dotnetProjectPath=./Project/MyMaintain/MyMaintain.csproj # dotnet机器RID eg linux-x64 dotnetRID=linux-x64 # dotnetSelfContained eg false dotnetSelfContained=false # dotnet发布目录 eg d:/dotnetpublish/app1 dotnetPublishOutput=/data/dotnetpublish/${projectName}/${branchName} # docker本地仓库地址 192.168.1.1:5000 dockerRegister=192.168.97.168:5000 # dockerStack配置 eg xxx.yml dockerStackCfgName=docker-stack.yml # dockerStack名 eg xxx_stack dockerStackName=${projectName}_stack # ========================================== git status # 切换当前分支 echo process GitBranch: ${branchName} git checkout ${branchName} if [ $? != 0 ] then exit 1 fi # 拉取当前分支 git pull if [ $? != 0 ] then exit 1 fi # 创建发布目录 if [ ! -d ${dotnetPublishOutput} ] then echo mkdir -p ${dotnetPublishOutput} mkdir -p ${dotnetPublishOutput} fi # 执行打包dotnet命令 echo dotnet publish ${dotnetProjectPath} -c Release -r ${dotnetRID} --self-contained ${dotnetSelfContained} -o ${dotnetPublishOutput} dotnet publish ${dotnetProjectPath} -c Release -r ${dotnetRID} --self-contained ${dotnetSelfContained} -o ${dotnetPublishOutput} if [ $? != 0 ] then exit 1 fi # 切换到发布目录 echo change work-dir to ${dotnetPublishOutput} cd ${dotnetPublishOutput} echo work-dir: pwd # 执行docker build echo docker build》》》》》》》》》》》》》》》》》 docker build -t ${dockerRegister}/${projectName} . if [ $? != 0 ] then exit 1 fi # 执行推送docker 本地仓库 echo docker push》》》》》》》》》》》》》》》》》 docker push ${dockerRegister}/${projectName} if [ $? != 0 ] then exit 1 fi # 执行docker stack echo docker stack》》》》》》》》》》》》》》》》》 docker stack deploy -c ${dockerStackCfgName} ${dockerStackName} if [ $? != 0 ] then exit 1 fi # 验证发布 echo process ok!
执行过程如下:
[root@box168 XXXLib]# /bin/sh ../auto-deployLF.sh work-dir: /home/gitRespo/XXXLib 位于分支 master 您的分支与上游分支 'origin/master' 一致。 无文件要提交,干净的工作区 process GitBranch: master 已经位于 'master' 您的分支与上游分支 'origin/master' 一致。 Username for 'https://e.coding.net': XXX@sina.com Password for 'https://XXX@sina.com@e.coding.net': 已经是最新的。 mkdir -p /data/dotnetpublish/my_maintain/master dotnet publish ./Project/MyMaintain/MyMaintain.csproj -c Release -r linux-x64 --self-contained false -o /data/dotnetpublish/my_maintain/master 用于 .NET 的 Microsoft (R) 生成引擎版本 17.0.0+c9eb9dd64 版权所有(C) Microsoft Corporation。保留所有权利。 正在确定要还原的项目… 所有项目均是最新的,无法还原。 HTools -> /home/gitRespo/XXXLib/HTools/bin/Release/netstandard2.0/HTools.dll MyExt.NLogLib -> /home/gitRespo/XXXLib/MyExt.NLog/bin/Release/netstandard2.1/MyExt.NLogLib.dll HDBLite -> /home/gitRespo/XXXLib/HDBLite/bin/Release/netstandard2.1/HDBLite.dll MyExt.JsonLib -> /home/gitRespo/XXXLib/MyExt.JsonLib/bin/Release/netstandard2.0/MyExt.JsonLib.dll MyExt.HttpLib -> /home/gitRespo/XXXLib/MyExt.HttpLib/bin/Release/netstandard2.0/MyExt.HttpLib.dll MyExt.AspectCore -> /home/gitRespo/XXXLib/MyExt.AspectCore/bin/Release/net6.0/MyExt.AspectCore.dll MyExt.Core -> /home/gitRespo/XXXLib/MyExt.Core/bin/Release/net6.0/MyExt.Core.dll MyExt.Mongo -> /home/gitRespo/XXXLib/MyExt.Mongo/bin/Release/net5.0/MyExt.Mongo.dll MyMaintain -> /home/gitRespo/XXXLib/Project/MyMaintain/bin/Release/net6.0/linux-x64/MyMaintain.dll MyMaintain -> /data/dotnetpublish/my_maintain/master/ change work-dir to /data/dotnetpublish/my_maintain/master work-dir: /data/dotnetpublish/my_maintain/master docker build Sending build context to Docker daemon 8.753MB Step 1/7 : FROM mcr.microsoft.com/dotnet/runtime:5.0 5.0: Pulling from dotnet/runtime ffbb094f4f9e: Pull complete b540880c2595: Pull complete e3271fa62d88: Pull complete c5b959ef17e2: Pull complete Digest: sha256:dfe9bea70fb6d4c13ee0f0b9f66bc8b8ba594d53c789e174d904dddba404cc72 Status: Downloaded newer image for mcr.microsoft.com/dotnet/runtime:5.0 ---> 613587686d47 Step 2/7 : RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ---> Running in 3cfe3df12e12 Removing intermediate container 3cfe3df12e12 ---> 7c251a333f7b Step 3/7 : RUN cd /home ---> Running in b46201004e74 Removing intermediate container b46201004e74 ---> 6e5580e760c7 Step 4/7 : RUN mkdir my_maintain ---> Running in 67da9ce263bb Removing intermediate container 67da9ce263bb ---> 05e014521033 Step 5/7 : WORKDIR /home/my_maintain ---> Running in 040b7404ca24 Removing intermediate container 040b7404ca24 ---> 4eb399106c22 Step 6/7 : COPY ./ ./ ---> 814277870aa7 Step 7/7 : CMD ["dotnet", "MyMaintain.dll"] ---> Running in aa1bbb923255 Removing intermediate container aa1bbb923255 ---> b829c7d13212 Successfully built b829c7d13212 Successfully tagged 192.168.97.168:5000/my_maintain:latest docker push Using default tag: latest The push refers to repository [192.168.97.168:5000/my_maintain] b1afe348f194: Pushed a3991b7262d4: Pushed d0fd4c6cc142: Pushed b2ddd18a75ff: Pushed edbd8c09a53a: Pushed 02ca0745b1e8: Pushed 0672f64e044d: Pushed f11bbd657c82: Pushed latest: digest: sha256:efc7ca2001d3a3bede7642d68436e6cf6eab5cc1d14e9d75838d2e9c131835b2 size: 1992 docker stack Creating network my_maintain_stack_default Creating service my_maintain_stack_web process ok! [root@box168 XXXLib]#
附录:
Dockerfile
# 基于 microsoft-dotnet镜像,TAG为latest 构建Docker Image FROM mcr.microsoft.com/dotnet/runtime:5.0 #修改时区,默认基于FROM镜像的时区 #Debian语法 RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime # 进入docker中的/home目录 RUN cd /home # 创建DockerWebAPI目录 RUN mkdir my_maintain # 设置工作路径 WORKDIR /home/my_maintain # 将当前文件夹下的所有文件全部复制到工作目录 COPY ./ ./ # 容器向外界暴露指定端口 #EXPOSE 8090 # 执行dotnet xxx.dll命令 CMD ["dotnet", "MyMaintain.dll"]
docker-stack.yml
version: "3" services: web: image: 192.168.97.168:5000/my_maintain:latest stop_grace_period: 10s deploy: replicas: 1 #resources: #limits: #cpus: "0.1" #memory: 50M restart_policy: condition: on-failure placement: constraints: #- 'node.role == worker' #- 'node.labels.platform == arm64' - 'node.labels.lblName == x86'
标签:脚本,maintain,dotnet,home,docker,分享,my,MyExt 来源: https://www.cnblogs.com/huawublog/p/15698286.html