docker.yml 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. SOURCE_TAG="${{ github.event.release.tag_name }}"
  38. fi
  39. if [[ -z "$SOURCE_TAG" ]]; then
  40. echo "Error: Could not determine a valid tag source. Input tag and context tag (github.ref_name) are both empty."
  41. exit 1
  42. fi
  43. if [[ "$SOURCE_TAG" =~ ^v[0-9]+\.[0-9] ]]; then
  44. IMAGE_TAG="${SOURCE_TAG#v}"
  45. else
  46. IMAGE_TAG="$SOURCE_TAG"
  47. fi
  48. echo "Docker image tag: '$IMAGE_TAG'."
  49. echo "IMAGE_TAG=$IMAGE_TAG" >>${GITHUB_ENV}
  50. LATEST=false
  51. if [[ "${{ github.event_name }}" == "release" && "${{ github.event.release.prerelease }}" == "false" ]] || [[ "${{ github.event_name }}" == "workflow_dispatch" && "${{ github.event.inputs.latest }}" == "true" ]]; then
  52. LATEST=true
  53. fi
  54. echo "Latest: '$LATEST'."
  55. echo "LATEST=$LATEST" >>${GITHUB_ENV}
  56. - name: Checkout code
  57. uses: actions/checkout@v5
  58. - name: Set up QEMU
  59. uses: docker/setup-qemu-action@v3
  60. - name: Set up Docker Buildx
  61. uses: docker/setup-buildx-action@v3
  62. - name: Login to GitHub Container Registry
  63. uses: docker/login-action@v3
  64. with:
  65. registry: ghcr.io
  66. username: ${{ github.repository_owner }}
  67. password: ${{ secrets.GITHUB_TOKEN }}
  68. - name: Build Docker image (main architectures)
  69. id: build_main_arches
  70. uses: docker/build-push-action@v6
  71. with:
  72. context: .
  73. file: .github/docker/Dockerfile
  74. platforms: |
  75. linux/amd64
  76. linux/arm/v7
  77. linux/arm64/v8
  78. linux/ppc64le
  79. linux/s390x
  80. provenance: false
  81. outputs: type=image,name=${{ env.FULL_IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
  82. - name: Build Docker image (additional architectures)
  83. id: build_additional_arches
  84. uses: docker/build-push-action@v6
  85. with:
  86. context: .
  87. file: .github/docker/Dockerfile.usa
  88. platforms: |
  89. linux/386
  90. linux/arm/v6
  91. linux/riscv64
  92. linux/loong64
  93. provenance: false
  94. outputs: type=image,name=${{ env.FULL_IMAGE_NAME }},push-by-digest=true,name-canonical=true,push=true
  95. - name: Create manifest list and push
  96. run: |
  97. echo "Creating multi-arch manifest with tag: '${{ env.FULL_IMAGE_NAME }}:${{ env.IMAGE_TAG }}'."
  98. docker buildx imagetools create \
  99. --tag ${{ env.FULL_IMAGE_NAME }}:${{ env.IMAGE_TAG }} \
  100. ${{ env.FULL_IMAGE_NAME }}@${{ steps.build_main_arches.outputs.digest }} \
  101. ${{ env.FULL_IMAGE_NAME }}@${{ steps.build_additional_arches.outputs.digest }}
  102. if [[ "${{ env.LATEST }}" == "true" ]]; then
  103. echo "Adding 'latest' tag to manifest: '${{ env.FULL_IMAGE_NAME }}:latest'."
  104. docker buildx imagetools create \
  105. --tag ${{ env.FULL_IMAGE_NAME }}:latest \
  106. ${{ env.FULL_IMAGE_NAME }}:${{ env.IMAGE_TAG }}
  107. fi
  108. - name: Inspect image
  109. run: |
  110. docker buildx imagetools inspect ${{ env.FULL_IMAGE_NAME }}:${{ env.IMAGE_TAG }}
  111. if [[ "${{ env.LATEST }}" == "true" ]]; then
  112. docker buildx imagetools inspect ${{ env.FULL_IMAGE_NAME }}:latest
  113. fi