docker-build.yml 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. name: Build and Push Docker Image
  2. on:
  3. push:
  4. branches:
  5. - main
  6. tags:
  7. - 'v*'
  8. workflow_dispatch:
  9. env:
  10. DOCKER_REPO: t0ngyu/new-api-alpha
  11. jobs:
  12. build-and-push:
  13. runs-on: ubuntu-latest
  14. steps:
  15. - name: Checkout code
  16. uses: actions/checkout@v5
  17. - name: Save version info
  18. run: |
  19. if [[ "${{ github.ref }}" == refs/tags/* ]]; then
  20. # For tags, use the tag name
  21. echo "${GITHUB_REF#refs/tags/}" > VERSION
  22. else
  23. # For branches, use date and commit hash
  24. echo "$(date +'%Y%m%d')-$(git rev-parse --short HEAD)" > VERSION
  25. fi
  26. cat VERSION
  27. - name: Set up QEMU
  28. uses: docker/setup-qemu-action@v4
  29. - name: Set up Docker Buildx
  30. uses: docker/setup-buildx-action@v4
  31. with:
  32. driver-opts: |
  33. image=moby/buildkit:latest
  34. network=host
  35. - name: Login to Docker Hub
  36. uses: docker/login-action@v4
  37. with:
  38. username: ${{ secrets.DOCKER_USERNAME }}
  39. password: ${{ secrets.DOCKER_PASSWORD }}
  40. - name: Extract metadata
  41. id: meta
  42. uses: docker/metadata-action@v6
  43. with:
  44. images: ${{ env.DOCKER_REPO }}
  45. tags: |
  46. type=ref,event=branch
  47. type=ref,event=pr
  48. type=semver,pattern={{version}}
  49. type=semver,pattern={{major}}.{{minor}}
  50. type=semver,pattern={{major}}
  51. type=sha,prefix={{branch}}-
  52. type=raw,value=latest,enable={{is_default_branch}}
  53. - name: Build and push Docker image
  54. id: build
  55. uses: docker/build-push-action@v7
  56. with:
  57. context: .
  58. file: ./Dockerfile
  59. platforms: linux/amd64
  60. push: true
  61. tags: ${{ steps.meta.outputs.tags }}
  62. labels: ${{ steps.meta.outputs.labels }}
  63. # Enable BuildKit cache mounts and GHA cache
  64. cache-from: type=gha
  65. cache-to: type=gha,mode=max
  66. build-args: |
  67. BUILDKIT_INLINE_CACHE=1
  68. - name: Image summary
  69. run: |
  70. echo "### Docker Image Built Successfully! 🚀" >> $GITHUB_STEP_SUMMARY
  71. echo "" >> $GITHUB_STEP_SUMMARY
  72. echo "**Tags:**" >> $GITHUB_STEP_SUMMARY
  73. echo '```' >> $GITHUB_STEP_SUMMARY
  74. echo "${{ steps.meta.outputs.tags }}" >> $GITHUB_STEP_SUMMARY
  75. echo '```' >> $GITHUB_STEP_SUMMARY
  76. echo "" >> $GITHUB_STEP_SUMMARY
  77. echo "**Digest:** \`${{ steps.build.outputs.digest }}\`" >> $GITHUB_STEP_SUMMARY