build.go 2.8 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() string {
  9. if b == nil {
  10. return ""
  11. }
  12. return getBuildImageName()
  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(env map[string]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 {
  70. return nil, fmt.Errorf("invalid build method: %s", b.GetMethod())
  71. }
  72. config.Build.Context = b.GetContext()
  73. config.Build.Env = CopyEnv(env)
  74. rawConfig := make(map[string]any)
  75. err := mapstructure.Decode(config, &rawConfig)
  76. if err != nil {
  77. return nil, err
  78. }
  79. return &types.Resource{
  80. Name: fmt.Sprintf("%s-build-image", b.GetName()),
  81. Driver: "build-image",
  82. Source: map[string]any{
  83. "name": "web",
  84. },
  85. Target: map[string]any{
  86. "app_name": b.GetName(),
  87. },
  88. DependsOn: []string{
  89. "get-env",
  90. },
  91. Config: rawConfig,
  92. }, nil
  93. }
  94. func getBuildImageName() string {
  95. return "base-image"
  96. }
  97. func GetBuildImageDriverName() string {
  98. return fmt.Sprintf("%s-build-image", getBuildImageName())
  99. }
  100. func (b *Build) getV1PushImage() (*types.Resource, error) {
  101. config := &preview.PushDriverConfig{}
  102. config.Push.Image = fmt.Sprintf("{ .%s.image }", GetBuildImageDriverName())
  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: b.GetName(),
  110. Driver: "push-image",
  111. DependsOn: []string{
  112. "get-env",
  113. GetBuildImageDriverName(),
  114. },
  115. Target: map[string]any{
  116. "app_name": b.GetName(),
  117. },
  118. Config: rawConfig,
  119. }, nil
  120. }