docker-bake.hcl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.21"
  12. }
  13. variable "SHA" {}
  14. variable "LATEST" {
  15. default = false
  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 != "" ? format("%s:%s%s-php%s-%s", IMAGE_NAME, version, tgt == "builder" ? "-builder" : "", php-version, os) : "",
  27. php-version == DEFAULT_PHP_VERSION && os == "bookworm" && version != "" ? format("%s:%s%s", IMAGE_NAME, version, tgt == "builder" ? "-builder" : "") : "",
  28. php-version == DEFAULT_PHP_VERSION && version != "" ? format("%s:%s%s-%s", IMAGE_NAME, version, tgt == "builder" ? "-builder" : "", os) : "",
  29. php-version == DEFAULT_PHP_VERSION && version == "latest" ? format("%s:%s%s", IMAGE_NAME, os, tgt == "builder" ? "-builder" : "") : "",
  30. os == "bookworm" && version != "" ? format("%s:%s%s-php%s", IMAGE_NAME, version, tgt == "builder" ? "-builder" : "", php-version) : "",
  31. ]
  32. }
  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 ? ["latest", 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. golang-base = "docker-image://golang:${GO_VERSION}-${os}"
  71. }
  72. dockerfile = os == "alpine" ? "alpine.Dockerfile" : "Dockerfile"
  73. context = "./"
  74. target = tgt
  75. # arm/v6 is only available for Alpine: https://github.com/docker-library/golang/issues/502
  76. platforms = os == "alpine" ? [
  77. "linux/amd64",
  78. "linux/386",
  79. "linux/arm/v6",
  80. "linux/arm/v7",
  81. "linux/arm64",
  82. ] : [
  83. "linux/amd64",
  84. "linux/386",
  85. "linux/arm/v7",
  86. "linux/arm64"
  87. ]
  88. tags = distinct(flatten(
  89. [for pv in php_version(php-version) : flatten([
  90. LATEST ? tag("latest", os, pv, tgt) : [],
  91. tag(SHA == "" ? "" : "sha-${substr(SHA, 0, 7)}", os, pv, tgt),
  92. [for v in semver(VERSION) : tag(v, os, pv, tgt)]
  93. ])
  94. ]))
  95. labels = {
  96. "org.opencontainers.image.created" = "${timestamp()}"
  97. "org.opencontainers.image.version" = VERSION
  98. "org.opencontainers.image.revision" = SHA
  99. }
  100. args = {
  101. FRANKENPHP_VERSION = VERSION
  102. }
  103. }
  104. target "static-builder" {
  105. contexts = {
  106. golang-base = "docker-image://golang:${GO_VERSION}-alpine"
  107. }
  108. dockerfile = "static-builder.Dockerfile"
  109. context = "./"
  110. platforms = [
  111. "linux/amd64",
  112. "linux/arm64",
  113. ]
  114. tags = distinct(flatten([
  115. LATEST ? "${IMAGE_NAME}:static-builder" : "",
  116. SHA == "" ? "" : "${IMAGE_NAME}:static-builder-sha-${substr(SHA, 0, 7)}",
  117. [for v in semver(VERSION) : v == "latest" ? "${IMAGE_NAME}:static-builder": "${IMAGE_NAME}:static-builder-${v}"]
  118. ]))
  119. labels = {
  120. "org.opencontainers.image.created" = "${timestamp()}"
  121. "org.opencontainers.image.version" = VERSION
  122. "org.opencontainers.image.revision" = SHA
  123. }
  124. args = {
  125. FRANKENPHP_VERSION = VERSION
  126. }
  127. secret = ["id=github-token,env=GITHUB_TOKEN"]
  128. }