2
0

build.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. rawConfig := make(map[string]any)
  77. err := mapstructure.Decode(config, &rawConfig)
  78. if err != nil {
  79. return nil, err
  80. }
  81. return &types.Resource{
  82. Name: fmt.Sprintf("%s-build-image", b.GetName(appName)),
  83. Driver: "build-image",
  84. Source: map[string]any{
  85. "name": "web",
  86. },
  87. Target: map[string]any{
  88. "app_name": b.GetName(appName),
  89. "namespace": namespace,
  90. },
  91. DependsOn: []string{
  92. "get-env",
  93. },
  94. Config: rawConfig,
  95. }, nil
  96. }
  97. func GetBuildImageDriverName(appName string) string {
  98. return fmt.Sprintf("%s-build-image", appName)
  99. }
  100. func (b *Build) getV1PushImage(appName string, namespace string) (*types.Resource, error) {
  101. config := &preview.PushDriverConfig{}
  102. config.Push.Image = fmt.Sprintf("{ .%s.image }", GetBuildImageDriverName(appName))
  103. rawConfig := make(map[string]any)
  104. err := mapstructure.Decode(config, &rawConfig)
  105. if err != nil {
  106. return nil, err
  107. }
  108. return &types.Resource{
  109. Name: fmt.Sprintf("%s-push-image", b.GetName(appName)),
  110. Driver: "push-image",
  111. DependsOn: []string{
  112. "get-env",
  113. GetBuildImageDriverName(appName),
  114. },
  115. Target: map[string]any{
  116. "app_name": b.GetName(appName),
  117. "namespace": namespace,
  118. },
  119. Config: rawConfig,
  120. }, nil
  121. }