build-push.yml 4.9 KB

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