docker.yml 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. name: Build and Push Docker Image
  2. on:
  3. release:
  4. types:
  5. - published
  6. - released
  7. workflow_dispatch:
  8. inputs:
  9. tag:
  10. description: "Docker image tag:"
  11. required: true
  12. latest:
  13. description: "Set to latest"
  14. type: boolean
  15. default: false
  16. jobs:
  17. build-and-push:
  18. if: (github.event.action != 'published') || (github.event.action == 'published' && github.event.release.prerelease == true)
  19. runs-on: ubuntu-latest
  20. permissions:
  21. contents: read
  22. packages: write
  23. steps:
  24. - name: Set repository and image name to lowercase
  25. env:
  26. IMAGE_NAME: "${{ github.repository }}"
  27. run: |
  28. echo "IMAGE_NAME=${IMAGE_NAME,,}" >>${GITHUB_ENV}
  29. echo "FULL_IMAGE_NAME=ghcr.io/${IMAGE_NAME,,}" >>${GITHUB_ENV}
  30. - name: Validate and extract tag
  31. run: |
  32. SOURCE_TAG="${{ github.event.inputs.tag }}"
  33. if [[ -z "$SOURCE_TAG" ]]; then
  34. SOURCE_TAG="${{ github.ref_name }}"
  35. fi
  36. if [[ -z "$SOURCE_TAG" ]]; then
  37. echo "Error: Could not determine a valid tag source. Input tag and context tag (github.ref_name) are both empty."
  38. exit 1
  39. fi
  40. if [[ "$SOURCE_TAG" =~ ^v[0-9]+\.[0-9] ]]; then
  41. IMAGE_TAG="${SOURCE_TAG#v}"
  42. else
  43. IMAGE_TAG="$SOURCE_TAG"
  44. fi
  45. echo "Docker image tag: '$IMAGE_TAG'."
  46. echo "IMAGE_TAG=$IMAGE_TAG" >>${GITHUB_ENV}
  47. LATEST=false
  48. if [[ "${{ github.event_name }}" == "release" && "${{ github.event.release.prerelease }}" == "false" ]] || [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ github.event.inputs.latest }}" == "true" ]]; then
  49. LATEST=true
  50. fi
  51. echo "Latest: '$LATEST'."
  52. echo "LATEST=$LATEST" >>${GITHUB_ENV}
  53. - name: Checkout code
  54. uses: actions/checkout@v4
  55. - name: Set up QEMU
  56. uses: docker/setup-qemu-action@v3
  57. - name: Set up Docker Buildx
  58. uses: docker/setup-buildx-action@v3
  59. - name: Login to GitHub Container Registry
  60. uses: docker/login-action@v3
  61. with:
  62. registry: ghcr.io
  63. username: ${{ github.repository_owner }}
  64. password: ${{ secrets.GITHUB_TOKEN }}
  65. - name: Build Docker image (main architectures)
  66. id: build_main_arches
  67. uses: docker/build-push-action@v6
  68. with:
  69. context: .
  70. file: .github/docker/Dockerfile
  71. platforms: |
  72. linux/amd64
  73. linux/arm/v7
  74. linux/arm64/v8
  75. linux/ppc64le
  76. linux/s390x
  77. provenance: false
  78. outputs: type=image,name=${{ env.FULL_IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
  79. - name: Build Docker image (additional architectures)
  80. id: build_additional_arches
  81. uses: docker/build-push-action@v6
  82. with:
  83. context: .
  84. file: .github/docker/Dockerfile.usa
  85. platforms: |
  86. linux/386
  87. linux/arm/v6
  88. linux/riscv64
  89. linux/loong64
  90. provenance: false
  91. outputs: type=image,name=${{ env.FULL_IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
  92. - name: Create manifest list and push
  93. run: |
  94. echo "Creating multi-arch manifest with tag: '${{ env.FULL_IMAGE_NAME }}:${{ env.IMAGE_TAG }}'."
  95. docker buildx imagetools create \
  96. --tag ${{ env.FULL_IMAGE_NAME }}:${{ env.IMAGE_TAG }} \
  97. ${{ env.FULL_IMAGE_NAME }}@${{ steps.build_main_arches.outputs.digest }} \
  98. ${{ env.FULL_IMAGE_NAME }}@${{ steps.build_additional_arches.outputs.digest }}
  99. if [[ "${{ env.LATEST }}" == "true" ]]; then
  100. echo "Adding 'latest' tag to manifest: '${{ env.FULL_IMAGE_NAME }}:latest'."
  101. docker buildx imagetools create \
  102. --tag ${{ env.FULL_IMAGE_NAME }}:latest \
  103. ${{ env.FULL_IMAGE_NAME }}:${{ env.IMAGE_TAG }}
  104. fi
  105. - name: Inspect image
  106. run: |
  107. docker buildx imagetools inspect ${{ env.FULL_IMAGE_NAME }}:${{ env.IMAGE_TAG }}
  108. if [[ "${{ env.LATEST }}" == "true" ]]; then
  109. docker buildx imagetools inspect ${{ env.FULL_IMAGE_NAME }}:latest
  110. fi