docker-bake.hcl 4.7 KB

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