.drone.yml 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. kind: pipeline
  2. type: docker
  3. name: ym-gateway-prod
  4. volumes: # 声明数据卷
  5. - name: targetDir
  6. host:
  7. path: /ym-gateway
  8. clone:
  9. disable: false # 启用代码拉取
  10. steps:
  11. - name: build-project
  12. image: mcr.microsoft.com/dotnet/sdk:7.0
  13. pull: if-not-exists
  14. depends_on: [clone] # 依赖的步骤
  15. volumes: # 挂载数据卷
  16. - name: targetDir
  17. path: /drone/src/dist/
  18. commands: # 执行命令
  19. - echo '' > ~/.nuget/NuGet/NuGet.Config
  20. - echo '<?xml version="1.0" encoding="utf-8"?><configuration><packageSources><add key="nuget.org" value="https://nuget.cdn.azure.cn/v3/index.json" protocolVersion="3" /></packageSources></configuration>' > ~/.nuget/NuGet/NuGet.Config
  21. - rm -rf /drone/src/dist/*
  22. - dotnet publish -c release -o ./publish -r linux-x64
  23. - cp -r ./publish/* /drone/src/dist # 复制文件
  24. when:
  25. status:
  26. - success # 当前步骤成功时执行
  27. - name: scp-project
  28. image: appleboy/drone-scp
  29. pull: if-not-exists
  30. depends_on: [build-project] # 依赖的步骤
  31. volumes: # 挂载数据卷
  32. - name: targetDir # 数据卷名称
  33. path: /ym-gateway # 容器内目录 绝对路径
  34. settings:
  35. host:
  36. from_secret: ym-gateway-host-a
  37. username:
  38. from_secret: username
  39. password:
  40. from_secret: userpwd
  41. port: 22
  42. command_timeout: 2m
  43. target: /home/ABServer/deploy-ready/
  44. source:
  45. - echo ====开始拷贝=======
  46. - /ym-gateway/*
  47. - echo ====结束拷贝=======
  48. when:
  49. status:
  50. - success # 当前步骤成功时执行
  51. - name: deploy-project
  52. image: appleboy/drone-ssh
  53. pull: if-not-exists
  54. depends_on: [scp-project] # 依赖的步骤
  55. settings:
  56. host:
  57. from_secret: ym-gateway-host-a
  58. username:
  59. from_secret: username
  60. password:
  61. from_secret: userpwd
  62. port: 22
  63. command_timeout: 2m
  64. script:
  65. - echo ====备份线上文件=======
  66. - cp -r /home/ABServer/webroot/ym-gateway /home/ABServer/deploy-bak/
  67. - echo ====开始部署=======
  68. - cd /home/ABServer/
  69. - cp -r /home/ABServer/deploy-ready/ym-gateway /home/ABServer/webroot/
  70. - docker restart ym-gateway
  71. - echo ====等待容器启动=======
  72. - sleep 10 # 等待容器启动
  73. - echo ====部署成功=======
  74. when:
  75. status:
  76. - success # 当前步骤成功时执行
  77. - name: scp-project-b
  78. image: appleboy/drone-scp
  79. pull: if-not-exists
  80. depends_on: [deploy-project] # 依赖的步骤
  81. volumes: # 挂载数据卷
  82. - name: targetDir # 数据卷名称
  83. path: /ym-gateway # 容器内目录 绝对路径
  84. settings:
  85. host:
  86. from_secret: ym-gateway-host-b
  87. username:
  88. from_secret: username
  89. password:
  90. from_secret: userpwd
  91. port: 22
  92. command_timeout: 10m
  93. target: /home/ABServer/deploy-ready/
  94. source:
  95. - echo ====开始拷贝=======
  96. - /ym-gateway/*
  97. - echo ====结束拷贝=======
  98. when:
  99. status:
  100. - success # 当前步骤成功时执行
  101. - name: deploy-project-b
  102. image: appleboy/drone-ssh
  103. pull: if-not-exists
  104. depends_on: [scp-project-b] # 依赖的步骤
  105. settings:
  106. host:
  107. from_secret: ym-gateway-host-b
  108. username:
  109. from_secret: username
  110. password:
  111. from_secret: userpwd
  112. port: 22
  113. command_timeout: 10m
  114. script:
  115. - echo ====备份线上文件=======
  116. - cp -r /home/ABServer/webroot/ym-gateway /home/ABServer/deploy-bak/ym-gateway
  117. - echo ====开始部署=======
  118. - cd /home/ABServer/
  119. - cp -r /home/ABServer/deploy-ready/ym-gateway /home/ABServer/webroot/
  120. - docker restart ym-gateway
  121. # - docker restart nodeServer1
  122. - echo ====部署成功=======
  123. when:
  124. status:
  125. - success # 当前步骤成功时执行
  126. trigger:
  127. branch:
  128. - release-ym-gateway
  129. event:
  130. - push
  131. #排除合并请求
  132. exclude:
  133. - ref: refs/pull/*
  134. ---
  135. kind: pipeline
  136. type: docker
  137. name: ym-gateway-deploy
  138. volumes: # 声明数据卷
  139. - name: build-artifacts
  140. host:
  141. path: /home/ABServer/webroot/ym-gateway
  142. - name: dockerfile-source
  143. host:
  144. path: /home/ABServer/dockfile
  145. - name: timezone
  146. host:
  147. path: /etc/localtime
  148. - name: docker-socket
  149. host:
  150. path: /var/run/docker.sock
  151. clone:
  152. disable: false # 启用代码拉取
  153. steps:
  154. # 阶段1: 构建
  155. - name: build-project
  156. image: mcr.microsoft.com/dotnet/sdk:7.0
  157. volumes:
  158. - name: build-artifacts
  159. path: /drone/src/dist/
  160. pull: if-not-exists
  161. depends_on: [clone] # 依赖的步骤
  162. commands: # 执行命令
  163. - echo '' > ~/.nuget/NuGet/NuGet.Config
  164. - echo '<?xml version="1.0" encoding="utf-8"?><configuration><packageSources><add key="nuget.org" value="https://nuget.cdn.azure.cn/v3/index.json" protocolVersion="3" /></packageSources></configuration>' > ~/.nuget/NuGet/NuGet.Config
  165. - rm -rf /drone/src/dist/*
  166. - dotnet publish -c release -o ./publish -r linux-x64
  167. - cp -r ./publish/* /drone/src/dist/ # 复制文件
  168. - ls -alR /drone/src/dist/
  169. when:
  170. status:
  171. - success # 当前步骤成功时执行
  172. # 阶段2: 准备Docker上下文(关键修正部分)
  173. - name: prepare-docker-context
  174. image: alpine:3.21.3
  175. depends_on: [ build-project ]
  176. volumes:
  177. - name: build-artifacts
  178. path: /context
  179. - name: dockerfile-source
  180. path: /dockerfiles
  181. commands:
  182. - cp /dockerfiles/template.dockerfile /context/Dockerfile
  183. - cp -r /dockerfiles/fonts /context/fonts
  184. - sed -i 's/{ImageName}/OmegaGateway/g' /context/Dockerfile
  185. # - echo -e "*\n!*.dll\n!lib/\n!config/\n!Dockerfile" > /context/.dockerignore
  186. - ls -alR /context
  187. # 阶段3: 构建并推送镜像
  188. - name: docker-build-push
  189. image: docker:24.0-cli
  190. depends_on: [ prepare-docker-context ]
  191. volumes:
  192. - name: build-artifacts
  193. path: /context
  194. - name: timezone
  195. path: /etc/localtime
  196. - name: docker-socket
  197. path: /var/run/docker.sock
  198. environment:
  199. HARBOR_USER:
  200. from_secret: harbor_robot
  201. HARBOR_PASS:
  202. from_secret: harbor_token
  203. commands:
  204. - docker login -u "$HARBOR_USER" -p "$HARBOR_PASS" harbor.seasidecity.cn
  205. - cd /context
  206. - ls .
  207. - TAG=prod-$(date +%Y%m%d%H%M%S)
  208. - docker build -t "harbor.seasidecity.cn/omega_lib/ym-gateway:$TAG" .
  209. - docker push "harbor.seasidecity.cn/omega_lib/ym-gateway:$TAG"
  210. - docker rmi "harbor.seasidecity.cn/omega_lib/ym-gateway:$TAG"
  211. trigger:
  212. branch:
  213. - prod-gateway
  214. event:
  215. - push