docker-bake.hcl 5.0 KB

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