2
0

build.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package stack
  2. import (
  3. "fmt"
  4. "github.com/mitchellh/mapstructure"
  5. "github.com/porter-dev/porter/internal/integrations/preview"
  6. "github.com/porter-dev/switchboard/pkg/types"
  7. )
  8. func (b *Build) GetName(appName string) string {
  9. if b == nil {
  10. return ""
  11. }
  12. return appName
  13. }
  14. func (b *Build) GetContext() string {
  15. if b == nil || b.Context == nil || *b.Context == "" {
  16. return "."
  17. }
  18. return *b.Context
  19. }
  20. func (b *Build) GetMethod() string {
  21. if b == nil || b.Method == nil {
  22. return ""
  23. }
  24. return *b.Method
  25. }
  26. func (b *Build) GetBuilder() string {
  27. if b == nil || b.Builder == nil {
  28. return ""
  29. }
  30. return *b.Builder
  31. }
  32. func (b *Build) GetBuildpacks() []string {
  33. if b == nil || b.Buildpacks == nil {
  34. return []string{}
  35. }
  36. var bp []string
  37. for _, b := range b.Buildpacks {
  38. if b == nil {
  39. continue
  40. }
  41. bp = append(bp, *b)
  42. }
  43. return bp
  44. }
  45. func (b *Build) GetDockerfile() string {
  46. if b == nil || b.Dockerfile == nil {
  47. return ""
  48. }
  49. return *b.Dockerfile
  50. }
  51. func (b *Build) GetImage() string {
  52. if b == nil || b.Image == nil {
  53. return ""
  54. }
  55. return *b.Image
  56. }
  57. func (b *Build) getV1BuildImage(appName string, env map[string]string, namespace string) (*types.Resource, error) {
  58. config := &preview.BuildDriverConfig{}
  59. if b.GetMethod() == "pack" {
  60. config.Build.Method = "pack"
  61. config.Build.Builder = b.GetBuilder()
  62. config.Build.Buildpacks = b.GetBuildpacks()
  63. } else if b.GetMethod() == "docker" {
  64. config.Build.Method = "docker"
  65. config.Build.Dockerfile = b.GetDockerfile()
  66. } else if b.GetMethod() == "registry" {
  67. config.Build.Method = "registry"
  68. config.Build.Image = b.GetImage()
  69. } else { // default to pack
  70. config.Build.Method = "pack"
  71. config.Build.Builder = b.GetBuilder()
  72. config.Build.Buildpacks = b.GetBuildpacks()
  73. }
  74. config.Build.Context = b.GetContext()
  75. config.Build.Env = CopyEnv(env)
  76. config.Build.Builder = "heroku/buildpacks:20"
  77. config.Build.Buildpacks = []string{"heroku/nodejs"}
  78. rawConfig := make(map[string]any)
  79. err := mapstructure.Decode(config, &rawConfig)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return &types.Resource{
  84. Name: fmt.Sprintf("%s-build-image", b.GetName(appName)),
  85. Driver: "build-image",
  86. Source: map[string]any{
  87. "name": "web",
  88. },
  89. Target: map[string]any{
  90. "app_name": b.GetName(appName),
  91. "namespace": namespace,
  92. },
  93. DependsOn: []string{
  94. "get-env",
  95. },
  96. Config: rawConfig,
  97. }, nil
  98. }
  99. func GetBuildImageDriverName(appName string) string {
  100. return fmt.Sprintf("%s-build-image", appName)
  101. }
  102. func (b *Build) getV1PushImage(appName string, namespace string) (*types.Resource, error) {
  103. config := &preview.PushDriverConfig{}
  104. config.Push.Image = fmt.Sprintf("{ .%s.image }", GetBuildImageDriverName(appName))
  105. rawConfig := make(map[string]any)
  106. err := mapstructure.Decode(config, &rawConfig)
  107. if err != nil {
  108. return nil, err
  109. }
  110. return &types.Resource{
  111. Name: fmt.Sprintf("%s-push-image", b.GetName(appName)),
  112. Driver: "push-image",
  113. DependsOn: []string{
  114. "get-env",
  115. GetBuildImageDriverName(appName),
  116. },
  117. Target: map[string]any{
  118. "app_name": b.GetName(appName),
  119. "namespace": namespace,
  120. },
  121. Config: rawConfig,
  122. }, nil
  123. }