docker-build.yml 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. name: Build and Push Docker Image
  2. on:
  3. push:
  4. branches: [master, main]
  5. # 仅当构建相关文件变更时触发,避免无意义排队
  6. paths:
  7. - "backend/**"
  8. - "frontend/**"
  9. - ".github/workflows/docker-build.yml"
  10. - "docker-compose*.yml"
  11. workflow_dispatch:
  12. inputs:
  13. build_backend:
  14. description: "手动触发时是否构建后端"
  15. required: false
  16. default: true
  17. type: boolean
  18. build_frontend:
  19. description: "手动触发时是否构建前端"
  20. required: false
  21. default: true
  22. type: boolean
  23. # 同一分支只保留最新一次,减少等待队列
  24. concurrency:
  25. group: docker-build-${{ github.ref }}
  26. cancel-in-progress: true
  27. jobs:
  28. changes:
  29. runs-on: ubuntu-latest
  30. outputs:
  31. backend: ${{ steps.filter.outputs.backend }}
  32. frontend: ${{ steps.filter.outputs.frontend }}
  33. steps:
  34. - name: Checkout code
  35. uses: actions/checkout@v4
  36. - name: Detect changed paths
  37. id: filter
  38. uses: dorny/paths-filter@v3
  39. with:
  40. filters: |
  41. backend:
  42. - "backend/**"
  43. - ".github/workflows/docker-build.yml"
  44. - "docker-compose*.yml"
  45. frontend:
  46. - "frontend/**"
  47. - ".github/workflows/docker-build.yml"
  48. - "docker-compose*.yml"
  49. build-backend:
  50. runs-on: ubuntu-latest
  51. needs: changes
  52. if: |
  53. github.event_name == 'workflow_dispatch' && inputs.build_backend == true ||
  54. github.event_name != 'workflow_dispatch' && needs.changes.outputs.backend == 'true'
  55. steps:
  56. - name: Checkout code
  57. uses: actions/checkout@v4
  58. - name: Set up Docker Buildx
  59. uses: docker/setup-buildx-action@v3
  60. - name: Log in to Docker Hub
  61. uses: docker/login-action@v3
  62. with:
  63. username: ${{ secrets.DOCKERHUB_USERNAME }}
  64. password: ${{ secrets.DOCKERHUB_TOKEN }}
  65. - name: Build and push backend
  66. uses: docker/build-push-action@v6
  67. with:
  68. context: ./backend
  69. file: ./backend/Dockerfile
  70. push: true
  71. tags: |
  72. 537yaha/ai-cs-backend:latest
  73. platforms: linux/amd64,linux/arm64
  74. # 使用 GitHub Actions 缓存构建层,显著减少重复构建时间
  75. cache-from: type=gha
  76. cache-to: type=gha,mode=max
  77. build-frontend:
  78. runs-on: ubuntu-latest
  79. needs: changes
  80. if: |
  81. github.event_name == 'workflow_dispatch' && inputs.build_frontend == true ||
  82. github.event_name != 'workflow_dispatch' && needs.changes.outputs.frontend == 'true'
  83. steps:
  84. - name: Checkout code
  85. uses: actions/checkout@v4
  86. - name: Set up Docker Buildx
  87. uses: docker/setup-buildx-action@v3
  88. - name: Log in to Docker Hub
  89. uses: docker/login-action@v3
  90. with:
  91. username: ${{ secrets.DOCKERHUB_USERNAME }}
  92. password: ${{ secrets.DOCKERHUB_TOKEN }}
  93. - name: Build and push frontend
  94. uses: docker/build-push-action@v6
  95. with:
  96. context: ./frontend
  97. file: ./frontend/Dockerfile
  98. push: true
  99. tags: |
  100. 537yaha/ai-cs-frontend:latest
  101. platforms: linux/amd64,linux/arm64
  102. # 前端统一走同域 /api(由反向代理转发到后端),无需在构建时注入 localhost/端口
  103. cache-from: type=gha
  104. cache-to: type=gha,mode=max