service_docker-build-and-publish.yml 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. name: Build and Publish
  2. on:
  3. workflow_call:
  4. inputs:
  5. authenticate_with_ghcr:
  6. default: true
  7. type: boolean
  8. description: 'Whether to authenticate with DockerHub.'
  9. tag-prefix:
  10. required: true
  11. type: string
  12. description: 'The prefix to use for the Docker image tags.'
  13. php-versions-file:
  14. type: string
  15. default: 'scripts/conf/php-versions.yml'
  16. description: 'The path to the PHP versions file.'
  17. default-image-variation:
  18. type: string
  19. default: 'cli'
  20. description: 'The default PHP variation to use for the Docker image.'
  21. registry-repositories:
  22. type: string
  23. required: true
  24. description: 'The Docker registry repositories to push the images to. Separate multiple repositories with a comma (example: docker.io/serversideup/php,ghcr.io/serversideup/php)'
  25. release-type:
  26. type: string
  27. default: 'testing'
  28. description: 'The type of release to create. Options: testing, latest'
  29. jobs:
  30. setup-matrix:
  31. runs-on: ubuntu-22.04
  32. outputs:
  33. php-version-map-json: ${{ steps.get-php-versions.outputs.php-version-map-json }}
  34. steps:
  35. - name: Check out code
  36. uses: actions/checkout@v4
  37. - name: Prepare PHP versions for the matrix. 😎
  38. run: |
  39. chmod +x ./scripts/get-php-versions.sh
  40. ./scripts/get-php-versions.sh
  41. - name: Ensure our PHP Versions file exists.
  42. run: |
  43. if [ ! -f "${{ inputs.php-versions-file }}" ]; then
  44. echo "PHP Versions file does not exist. Exiting."
  45. exit 1
  46. else
  47. cat ${{ inputs.php-versions-file }}
  48. fi
  49. - name: Assemble PHP versions into the matrix. 😎
  50. id: get-php-versions
  51. run: |
  52. MATRIX_JSON=$(yq -o=json scripts/conf/php-versions.yml | jq -c '{include: [(.php_variations[] | {name, supported_os: (.supported_os // ["alpine", "bullseye", "bookworm"])} ) as $variation | .php_versions[] | .minor_versions[] | .patch_versions[] as $patch | .base_os[] as $os | select($variation.supported_os | if length == 0 then . else . | index($os.name) end) | {patch_version: $patch, base_os: $os.name, php_variation: $variation.name}]} | {include: (.include | sort_by(.patch_version | split(".") | map(tonumber) | . as $nums | ($nums[0]*10000 + $nums[1]*100 + $nums[2])) | reverse)}')
  53. echo "php-version-map-json=${MATRIX_JSON}" >> $GITHUB_OUTPUT
  54. echo "${MATRIX_JSON}" | jq '.'
  55. - name: Upload the php-versions.yml file
  56. uses: actions/upload-artifact@v4
  57. with:
  58. name: php-versions.yml
  59. path: ${{ inputs.php-versions-file }}
  60. docker-publish:
  61. needs: setup-matrix
  62. runs-on: ubuntu-22.04
  63. strategy:
  64. matrix: ${{fromJson(needs.setup-matrix.outputs.php-version-map-json)}}
  65. steps:
  66. - name: Check out code.
  67. uses: actions/checkout@v4
  68. - name: Download PHP Versions file
  69. uses: actions/download-artifact@v4
  70. with:
  71. name: php-versions.yml
  72. path: ./artifacts
  73. - name: Move PHP Versions file
  74. run: mv ./artifacts/php-versions.yml ${{ inputs.php-versions-file }}
  75. ##
  76. # Docker build & publish
  77. ##
  78. - name: Login to DockerHub
  79. uses: docker/login-action@v3
  80. with:
  81. username: ${{ secrets.DOCKER_HUB_USERNAME }}
  82. password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
  83. - name: Login to GitHub Container Registry
  84. uses: docker/login-action@v3
  85. if: ${{ inputs.authenticate_with_ghcr }}
  86. with:
  87. registry: ghcr.io
  88. username: ${{ github.actor }}
  89. password: ${{ secrets.GITHUB_TOKEN }}
  90. - name: Set up QEMU
  91. uses: docker/setup-qemu-action@v3
  92. - name: Set up Docker Buildx
  93. uses: docker/setup-buildx-action@v3
  94. - name: "πŸ“¦ Assemble the Docker Tags"
  95. run: |
  96. chmod +x ./scripts/assemble-docker-tags.sh
  97. ./scripts/assemble-docker-tags.sh
  98. env:
  99. PHP_VERSIONS_FILE: "${{ inputs.php-versions-file }}"
  100. DEFAULT_IMAGE_VARIATION: ${{ inputs.default-image-variation }}
  101. PHP_BUILD_VERSION: ${{ matrix.patch_version }}
  102. PHP_BUILD_VARIATION: ${{ matrix.php_variation }}
  103. PHP_BUILD_BASE_OS: ${{ matrix.base_os }}
  104. DOCKER_TAG_PREFIX: ${{ inputs.tag-prefix }}
  105. DOCKER_REGISTRY_REPOSITORIES: ${{ inputs.registry-repositories }}
  106. RELEASE_TYPE: ${{ inputs.release-type }}
  107. GITHUB_RELEASE_TAG: ${{ github.ref_name }}
  108. GITHUB_REF_TYPE: ${{ github.ref_type }}
  109. - name: Set REPOSITORY_BUILD_VERSION
  110. id: set_version
  111. run: |
  112. if [ "${{ github.ref_type }}" == "tag" ]; then
  113. echo "πŸš€ Setting REPOSITORY_BUILD_VERSION to Tag"
  114. echo "REPOSITORY_BUILD_VERSION=${{ github.ref_name }}" >> $GITHUB_ENV
  115. else
  116. echo "πŸ‘¨β€πŸ”¬ Setting REPOSITORY_BUILD_VERSION to GIT Short SHA and GitHub Run ID"
  117. SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-7)
  118. echo "REPOSITORY_BUILD_VERSION=git-${SHORT_SHA}-${{ github.run_id }}" >> $GITHUB_ENV
  119. fi
  120. - name: Build and push
  121. uses: docker/build-push-action@v6
  122. with:
  123. file: src/variations/${{ matrix.php_variation }}/Dockerfile
  124. cache-from: type=gha
  125. cache-to: type=gha,mode=max
  126. build-args: |
  127. BASE_OS_VERSION=${{ matrix.base_os }}
  128. PHP_VERSION=${{ matrix.patch_version }}
  129. PHP_VARIATION=${{ matrix.php_variation }}
  130. REPOSITORY_BUILD_VERSION=${{ env.REPOSITORY_BUILD_VERSION }}
  131. platforms: |
  132. linux/amd64
  133. linux/arm/v7
  134. linux/arm64/v8
  135. pull: true
  136. push: true
  137. tags: ${{ env.DOCKER_TAGS }}
  138. outputs: type=image,name=target,annotation-index.org.opencontainers.image.description=Supercharge your PHP experience with Docker images that are based off the official PHP images but are optimized to be run in production environments for Laravel and WordPress and more