service_docker-build-and-publish.yml 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. env:
  42. SKIP_DOWNLOAD: false
  43. - name: Ensure our PHP Versions file exists.
  44. run: |
  45. if [ ! -f "${{ inputs.php-versions-file }}" ]; then
  46. echo "PHP Versions file does not exist. Exiting."
  47. exit 1
  48. else
  49. cat ${{ inputs.php-versions-file }}
  50. fi
  51. - name: Assemble PHP versions into the matrix. 😎
  52. id: get-php-versions
  53. run: |
  54. 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)}')
  55. echo "php-version-map-json=${MATRIX_JSON}" >> $GITHUB_OUTPUT
  56. echo "${MATRIX_JSON}" | jq '.'
  57. - name: Upload the php-versions.yml file
  58. uses: actions/upload-artifact@v4
  59. with:
  60. name: php-versions.yml
  61. path: ${{ inputs.php-versions-file }}
  62. docker-publish:
  63. needs: setup-matrix
  64. runs-on:
  65. - runs-on
  66. - runner=4cpu-linux-x64
  67. - run-id=${{ github.run_id }}
  68. strategy:
  69. matrix: ${{fromJson(needs.setup-matrix.outputs.php-version-map-json)}}
  70. steps:
  71. - name: Check out code.
  72. uses: actions/checkout@v4
  73. - name: Download PHP Versions file
  74. uses: actions/download-artifact@v4
  75. with:
  76. name: php-versions.yml
  77. path: ./artifacts
  78. - name: Move PHP Versions file
  79. run: mv ./artifacts/php-versions.yml ${{ inputs.php-versions-file }}
  80. ##
  81. # Docker build & publish
  82. ##
  83. - name: Login to DockerHub
  84. uses: docker/login-action@v3
  85. with:
  86. username: ${{ secrets.DOCKER_HUB_USERNAME }}
  87. password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
  88. - name: Login to GitHub Container Registry
  89. uses: docker/login-action@v3
  90. if: ${{ inputs.authenticate_with_ghcr }}
  91. with:
  92. registry: ghcr.io
  93. username: ${{ github.actor }}
  94. password: ${{ secrets.GITHUB_TOKEN }}
  95. - name: Set up QEMU
  96. uses: docker/setup-qemu-action@v3
  97. - name: Set up Docker Buildx
  98. uses: docker/setup-buildx-action@v3
  99. - name: "πŸ“¦ Assemble the Docker Tags"
  100. run: |
  101. chmod +x ./scripts/assemble-docker-tags.sh
  102. ./scripts/assemble-docker-tags.sh
  103. env:
  104. PHP_VERSIONS_FILE: "${{ inputs.php-versions-file }}"
  105. DEFAULT_IMAGE_VARIATION: ${{ inputs.default-image-variation }}
  106. PHP_BUILD_VERSION: ${{ matrix.patch_version }}
  107. PHP_BUILD_VARIATION: ${{ matrix.php_variation }}
  108. PHP_BUILD_BASE_OS: ${{ matrix.base_os }}
  109. DOCKER_TAG_PREFIX: ${{ inputs.tag-prefix }}
  110. DOCKER_REGISTRY_REPOSITORIES: ${{ inputs.registry-repositories }}
  111. RELEASE_TYPE: ${{ inputs.release-type }}
  112. GITHUB_RELEASE_TAG: ${{ github.ref_name }}
  113. GITHUB_REF_TYPE: ${{ github.ref_type }}
  114. - name: Set REPOSITORY_BUILD_VERSION
  115. id: set_version
  116. run: |
  117. if [ "${{ github.ref_type }}" == "tag" ]; then
  118. echo "πŸš€ Setting REPOSITORY_BUILD_VERSION to Tag"
  119. echo "REPOSITORY_BUILD_VERSION=${{ github.ref_name }}" >> $GITHUB_ENV
  120. else
  121. echo "πŸ‘¨β€πŸ”¬ Setting REPOSITORY_BUILD_VERSION to GIT Short SHA and GitHub Run ID"
  122. SHORT_SHA=$(echo ${{ github.sha }} | cut -c1-7)
  123. echo "REPOSITORY_BUILD_VERSION=git-${SHORT_SHA}-${{ github.run_id }}" >> $GITHUB_ENV
  124. fi
  125. - name: Build and push
  126. uses: docker/build-push-action@v6
  127. with:
  128. file: src/variations/${{ matrix.php_variation }}/Dockerfile
  129. cache-from: type=s3,blobs_prefix=cache/${{ github.repository }}/,manifests_prefix=cache/${{ github.repository }}/,region=${{ env.RUNS_ON_AWS_REGION }},bucket=${{ env.RUNS_ON_S3_BUCKET_CACHE }}
  130. cache-to: type=s3,blobs_prefix=cache/${{ github.repository }}/,manifests_prefix=cache/${{ github.repository }}/,region=${{ env.RUNS_ON_AWS_REGION }},bucket=${{ env.RUNS_ON_S3_BUCKET_CACHE }},mode=max
  131. build-args: |
  132. BASE_OS_VERSION=${{ matrix.base_os }}
  133. PHP_VERSION=${{ matrix.patch_version }}
  134. PHP_VARIATION=${{ matrix.php_variation }}
  135. REPOSITORY_BUILD_VERSION=${{ env.REPOSITORY_BUILD_VERSION }}
  136. platforms: |
  137. linux/amd64
  138. linux/arm/v7
  139. linux/arm64/v8
  140. pull: true
  141. push: true
  142. tags: ${{ env.DOCKER_TAGS }}
  143. 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