build-push.yml 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. name: Build and Push API & Web
  2. on:
  3. push:
  4. branches:
  5. - "main"
  6. - "deploy/dev"
  7. - "deploy/enterprise"
  8. tags:
  9. - "*"
  10. concurrency:
  11. group: build-push-${{ github.head_ref || github.run_id }}
  12. cancel-in-progress: true
  13. env:
  14. DOCKERHUB_USER: ${{ secrets.DOCKERHUB_USER }}
  15. DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
  16. DIFY_WEB_IMAGE_NAME: ${{ vars.DIFY_WEB_IMAGE_NAME || 'langgenius/dify-web' }}
  17. DIFY_API_IMAGE_NAME: ${{ vars.DIFY_API_IMAGE_NAME || 'langgenius/dify-api' }}
  18. jobs:
  19. build:
  20. runs-on: ${{ matrix.platform == 'linux/arm64' && 'arm64_runner' || 'ubuntu-latest' }}
  21. if: github.repository == 'langgenius/dify'
  22. strategy:
  23. matrix:
  24. include:
  25. - service_name: "build-api-amd64"
  26. image_name_env: "DIFY_API_IMAGE_NAME"
  27. context: "api"
  28. platform: linux/amd64
  29. - service_name: "build-api-arm64"
  30. image_name_env: "DIFY_API_IMAGE_NAME"
  31. context: "api"
  32. platform: linux/arm64
  33. - service_name: "build-web-amd64"
  34. image_name_env: "DIFY_WEB_IMAGE_NAME"
  35. context: "web"
  36. platform: linux/amd64
  37. - service_name: "build-web-arm64"
  38. image_name_env: "DIFY_WEB_IMAGE_NAME"
  39. context: "web"
  40. platform: linux/arm64
  41. steps:
  42. - name: Prepare
  43. run: |
  44. platform=${{ matrix.platform }}
  45. echo "PLATFORM_PAIR=${platform//\//-}" >> $GITHUB_ENV
  46. - name: Login to Docker Hub
  47. uses: docker/login-action@v3
  48. with:
  49. username: ${{ env.DOCKERHUB_USER }}
  50. password: ${{ env.DOCKERHUB_TOKEN }}
  51. - name: Set up QEMU
  52. uses: docker/setup-qemu-action@v3
  53. - name: Set up Docker Buildx
  54. uses: docker/setup-buildx-action@v3
  55. - name: Extract metadata for Docker
  56. id: meta
  57. uses: docker/metadata-action@v5
  58. with:
  59. images: ${{ env[matrix.image_name_env] }}
  60. - name: Build Docker image
  61. id: build
  62. uses: docker/build-push-action@v6
  63. with:
  64. context: "{{defaultContext}}:${{ matrix.context }}"
  65. platforms: ${{ matrix.platform }}
  66. build-args: COMMIT_SHA=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }}
  67. labels: ${{ steps.meta.outputs.labels }}
  68. outputs: type=image,name=${{ env[matrix.image_name_env] }},push-by-digest=true,name-canonical=true,push=true
  69. cache-from: type=gha,scope=${{ matrix.service_name }}
  70. cache-to: type=gha,mode=max,scope=${{ matrix.service_name }}
  71. - name: Export digest
  72. env:
  73. DIGEST: ${{ steps.build.outputs.digest }}
  74. run: |
  75. mkdir -p /tmp/digests
  76. sanitized_digest=${DIGEST#sha256:}
  77. touch "/tmp/digests/${sanitized_digest}"
  78. - name: Upload digest
  79. uses: actions/upload-artifact@v4
  80. with:
  81. name: digests-${{ matrix.context }}-${{ env.PLATFORM_PAIR }}
  82. path: /tmp/digests/*
  83. if-no-files-found: error
  84. retention-days: 1
  85. create-manifest:
  86. needs: build
  87. runs-on: ubuntu-latest
  88. if: github.repository == 'langgenius/dify'
  89. strategy:
  90. matrix:
  91. include:
  92. - service_name: "merge-api-images"
  93. image_name_env: "DIFY_API_IMAGE_NAME"
  94. context: "api"
  95. - service_name: "merge-web-images"
  96. image_name_env: "DIFY_WEB_IMAGE_NAME"
  97. context: "web"
  98. steps:
  99. - name: Download digests
  100. uses: actions/download-artifact@v4
  101. with:
  102. path: /tmp/digests
  103. pattern: digests-${{ matrix.context }}-*
  104. merge-multiple: true
  105. - name: Login to Docker Hub
  106. uses: docker/login-action@v3
  107. with:
  108. username: ${{ env.DOCKERHUB_USER }}
  109. password: ${{ env.DOCKERHUB_TOKEN }}
  110. - name: Extract metadata for Docker
  111. id: meta
  112. uses: docker/metadata-action@v5
  113. with:
  114. images: ${{ env[matrix.image_name_env] }}
  115. tags: |
  116. type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/') && !contains(github.ref, '-') }}
  117. type=ref,event=branch
  118. type=sha,enable=true,priority=100,prefix=,suffix=,format=long
  119. type=raw,value=${{ github.ref_name }},enable=${{ startsWith(github.ref, 'refs/tags/') }}
  120. - name: Create manifest list and push
  121. working-directory: /tmp/digests
  122. env:
  123. IMAGE_NAME: ${{ env[matrix.image_name_env] }}
  124. run: |
  125. docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
  126. $(printf "$IMAGE_NAME@sha256:%s " *)
  127. - name: Inspect image
  128. env:
  129. IMAGE_NAME: ${{ env[matrix.image_name_env] }}
  130. IMAGE_VERSION: ${{ steps.meta.outputs.version }}
  131. run: |
  132. docker buildx imagetools inspect "$IMAGE_NAME:$IMAGE_VERSION"