docker-publish.yml 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. name: Build Latest Docker Image on Release
  2. on:
  3. release:
  4. types: [published]
  5. workflow_dispatch:
  6. permissions:
  7. contents: read
  8. id-token: write
  9. packages: write
  10. jobs:
  11. build-and-push:
  12. runs-on: ubuntu-latest
  13. steps:
  14. - name: Checkout code
  15. uses: actions/checkout@v6
  16. with:
  17. fetch-depth: 0 # Fetch all history for tags
  18. - name: Set up Python
  19. uses: actions/setup-python@v6
  20. with:
  21. python-version: "3.x"
  22. - name: Set up Docker Buildx
  23. uses: docker/setup-buildx-action@v4
  24. - name: Login to GitHub Container Registry
  25. uses: docker/login-action@v4
  26. with:
  27. registry: ghcr.io
  28. username: ${{ github.actor }}
  29. password: ${{ secrets.GITHUB_TOKEN }}
  30. - name: Install cosign
  31. uses: sigstore/cosign-installer@6f9f17788090df1f26f669e9d70d6ae9567deba6 # v4.1.2
  32. - name: Get latest tag
  33. id: get_tag
  34. run: |
  35. if [ "${{ github.event_name }}" = "release" ] && [ -n "${{ github.event.release.tag_name }}" ]; then
  36. TAG="${{ github.event.release.tag_name }}"
  37. else
  38. TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
  39. fi
  40. if [ -z "$TAG" ]; then
  41. echo "No git tag found for docker publish"
  42. exit 1
  43. fi
  44. PACKAGE_VERSION="${TAG#v}"
  45. echo "Found tag: $TAG"
  46. echo "tag=$TAG" >> $GITHUB_OUTPUT
  47. echo "package_version=$PACKAGE_VERSION" >> $GITHUB_OUTPUT
  48. - name: Check if pre-release
  49. id: check_prerelease
  50. run: |
  51. TAG="${{ steps.get_tag.outputs.tag }}"
  52. if [[ "$TAG" == *"rc"* ]] || [[ "$TAG" == *"dev"* ]]; then
  53. echo "is_prerelease=true" >> $GITHUB_OUTPUT
  54. echo "This is a pre-release version: $TAG"
  55. else
  56. echo "is_prerelease=false" >> $GITHUB_OUTPUT
  57. echo "This is a stable release: $TAG"
  58. fi
  59. - name: Update version definitions
  60. run: |
  61. python scripts/release/set_version.py --core-version "${{ steps.get_tag.outputs.package_version }}"
  62. echo "Updated version definitions with ${{ steps.get_tag.outputs.package_version }}"
  63. grep '__version__ = ' lightrag/_version.py
  64. - name: Extract metadata for Docker
  65. id: meta
  66. uses: docker/metadata-action@v6
  67. with:
  68. images: ghcr.io/${{ github.repository }}
  69. tags: |
  70. type=raw,value=${{ steps.get_tag.outputs.tag }}
  71. type=raw,value=latest,enable=${{ steps.check_prerelease.outputs.is_prerelease == 'false' }}
  72. - name: Build and push Docker image
  73. id: build-and-push
  74. uses: docker/build-push-action@v7
  75. with:
  76. context: .
  77. file: ./Dockerfile
  78. platforms: linux/amd64,linux/arm64
  79. push: true
  80. tags: ${{ steps.meta.outputs.tags }}
  81. labels: ${{ steps.meta.outputs.labels }}
  82. cache-from: type=gha
  83. cache-to: type=gha,mode=max
  84. - name: Sign Docker image
  85. if: steps.build-and-push.outputs.digest != ''
  86. env:
  87. DIGEST: ${{ steps.build-and-push.outputs.digest }}
  88. TAGS: ${{ steps.meta.outputs.tags }}
  89. run: |
  90. set -euo pipefail
  91. echo "Signing manifest digest: $DIGEST"
  92. while IFS= read -r tag; do
  93. if [ -z "$tag" ]; then
  94. continue
  95. fi
  96. echo "Signing ${tag}@${DIGEST}"
  97. cosign sign --yes "${tag}@${DIGEST}"
  98. done <<< "$TAGS"
  99. - name: Output image details
  100. run: |
  101. echo "Docker image built and pushed successfully!"
  102. echo "Image tags:"
  103. echo " - ghcr.io/${{ github.repository }}:${{ steps.get_tag.outputs.tag }}"
  104. echo " - ghcr.io/${{ github.repository }}:latest"
  105. echo "Signed manifest digest: ${{ steps.build-and-push.outputs.digest }}"
  106. echo "Latest Git tag used: ${{ steps.get_tag.outputs.tag }}"