docker-bake.hcl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. variable "IMAGE_NAME" {
  2. default = "dunglas/frankenphp"
  3. }
  4. variable "VERSION" {
  5. default = "dev"
  6. }
  7. variable "PHP_VERSION" {
  8. default = "8.2,8.3"
  9. }
  10. variable "GO_VERSION" {
  11. default = "1.22"
  12. }
  13. variable "SHA" {}
  14. variable "LATEST" {
  15. default = true
  16. }
  17. variable "CACHE" {
  18. default = ""
  19. }
  20. variable DEFAULT_PHP_VERSION {
  21. default = "8.3"
  22. }
  23. function "tag" {
  24. params = [version, os, php-version, tgt]
  25. result = [
  26. version == "" ? "" : "${IMAGE_NAME}:${trimprefix("${version}${tgt == "builder" ? "-builder" : ""}-php${php-version}-${os}", "latest-")}",
  27. php-version == DEFAULT_PHP_VERSION && os == "bookworm" && version != "" ? "${IMAGE_NAME}:${trimprefix("${version}${tgt == "builder" ? "-builder" : ""}", "latest-")}" : "",
  28. php-version == DEFAULT_PHP_VERSION && version != "" ? "${IMAGE_NAME}:${trimprefix("${version}${tgt == "builder" ? "-builder" : ""}-${os}", "latest-")}" : "",
  29. os == "bookworm" && version != "" ? "${IMAGE_NAME}:${trimprefix("${version}${tgt == "builder" ? "-builder" : ""}-php${php-version}", "latest-")}" : "",
  30. ]
  31. }
  32. # cleanTag ensures that the tag is a valid Docker tag
  33. # cleanTag ensures that the tag is a valid Docker tag
  34. # see https://github.com/distribution/distribution/blob/v2.8.2/reference/regexp.go#L37
  35. function "clean_tag" {
  36. params = [tag]
  37. result = substr(regex_replace(regex_replace(tag, "[^\\w.-]", "-"), "^([^\\w])", "r$0"), 0, 127)
  38. }
  39. # semver adds semver-compliant tag if a semver version number is passed, or returns the revision itself
  40. # see https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
  41. function "semver" {
  42. params = [rev]
  43. result = __semver(_semver(regexall("^v?(?P<major>0|[1-9]\\d*)\\.(?P<minor>0|[1-9]\\d*)\\.(?P<patch>0|[1-9]\\d*)(?:-(?P<prerelease>(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\\.(?:0|[1-9]\\d*|\\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\\.[0-9a-zA-Z-]+)*))?$", rev)))
  44. }
  45. function "_semver" {
  46. params = [matches]
  47. result = length(matches) == 0 ? {} : matches[0]
  48. }
  49. function "__semver" {
  50. params = [v]
  51. result = v == {} ? [clean_tag(VERSION)] : v.prerelease == null ? [v.major, "${v.major}.${v.minor}", "${v.major}.${v.minor}.${v.patch}"] : ["${v.major}.${v.minor}.${v.patch}-${v.prerelease}"]
  52. }
  53. function "php_version" {
  54. params = [v]
  55. result = _php_version(v, regexall("(?P<major>\\d+)\\.(?P<minor>\\d+)", v)[0])
  56. }
  57. function "_php_version" {
  58. params = [v, m]
  59. result = "${m.major}.${m.minor}" == DEFAULT_PHP_VERSION ? [v, "${m.major}.${m.minor}", "${m.major}"] : [v, "${m.major}.${m.minor}"]
  60. }
  61. target "default" {
  62. name = "${tgt}-php-${replace(php-version, ".", "-")}-${os}"
  63. matrix = {
  64. os = ["bookworm", "alpine"]
  65. php-version = split(",", PHP_VERSION)
  66. tgt = ["builder", "runner"]
  67. }
  68. contexts = {
  69. php-base = "docker-image://php:${php-version}-zts-${os}"
  70. # FIXME: temporary workaround for https://github.com/dunglas/symfony-docker/issues/646
  71. golang-base = "docker-image://golang:${os == "alpine" ? "1.22.4" : GO_VERSION}-${os}"
  72. }
  73. dockerfile = os == "alpine" ? "alpine.Dockerfile" : "Dockerfile"
  74. context = "./"
  75. target = tgt
  76. # arm/v6 is only available for Alpine: https://github.com/docker-library/golang/issues/502
  77. platforms = os == "alpine" ? [
  78. "linux/amd64",
  79. "linux/386",
  80. "linux/arm/v6",
  81. "linux/arm/v7",
  82. "linux/arm64",
  83. ] : [
  84. "linux/amd64",
  85. "linux/386",
  86. "linux/arm/v7",
  87. "linux/arm64"
  88. ]
  89. tags = distinct(flatten(
  90. [for pv in php_version(php-version) : flatten([
  91. LATEST ? tag("latest", os, pv, tgt) : [],
  92. tag(SHA == "" || VERSION != "dev" ? "" : "sha-${substr(SHA, 0, 7)}", os, pv, tgt),
  93. VERSION == "dev" ? [] : [for v in semver(VERSION) : tag(v, os, pv, tgt)]
  94. ])
  95. ]))
  96. labels = {
  97. "org.opencontainers.image.created" = "${timestamp()}"
  98. "org.opencontainers.image.version" = VERSION
  99. "org.opencontainers.image.revision" = SHA
  100. }
  101. args = {
  102. FRANKENPHP_VERSION = VERSION
  103. }
  104. }
  105. target "static-builder" {
  106. contexts = {
  107. golang-base = "docker-image://golang:${GO_VERSION}-alpine"
  108. }
  109. dockerfile = "static-builder.Dockerfile"
  110. context = "./"
  111. platforms = [
  112. "linux/amd64",
  113. "linux/arm64",
  114. ]
  115. tags = distinct(flatten([
  116. LATEST ? "${IMAGE_NAME}:static-builder" : "",
  117. SHA == "" || VERSION != "dev" ? "" : "${IMAGE_NAME}:static-builder-sha-${substr(SHA, 0, 7)}",
  118. VERSION == "dev" ? [] : [for v in semver(VERSION) : "${IMAGE_NAME}:static-builder-${v}"]
  119. ]))
  120. labels = {
  121. "org.opencontainers.image.created" = "${timestamp()}"
  122. "org.opencontainers.image.version" = VERSION
  123. "org.opencontainers.image.revision" = SHA
  124. }
  125. args = {
  126. FRANKENPHP_VERSION = VERSION
  127. }
  128. secret = ["id=github-token,env=GITHUB_TOKEN"]
  129. }