pack.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package pack
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "path/filepath"
  7. "strings"
  8. "github.com/buildpacks/pack"
  9. "github.com/porter-dev/porter/api/types"
  10. "github.com/porter-dev/porter/cli/cmd/docker"
  11. "github.com/porter-dev/porter/cli/cmd/github"
  12. "k8s.io/client-go/util/homedir"
  13. )
  14. type Agent struct{}
  15. func (a *Agent) Build(opts *docker.BuildOpts, buildConfig *types.BuildConfig) error {
  16. //create a context object
  17. context := context.Background()
  18. //initialize a pack client
  19. client, err := pack.NewClient(pack.WithLogger(newPackLogger()))
  20. if err != nil {
  21. return err
  22. }
  23. absPath, err := filepath.Abs(opts.BuildContext)
  24. if err != nil {
  25. return err
  26. }
  27. buildOpts := pack.BuildOptions{
  28. RelativeBaseDir: filepath.Dir(absPath),
  29. Image: fmt.Sprintf("%s:%s", opts.ImageRepo, opts.Tag),
  30. Builder: "paketobuildpacks/builder:full",
  31. AppPath: opts.BuildContext,
  32. TrustBuilder: true,
  33. Env: opts.Env,
  34. }
  35. if buildConfig != nil {
  36. buildOpts.Builder = buildConfig.Builder
  37. for i := range buildConfig.Buildpacks {
  38. bp := buildConfig.Buildpacks[i]
  39. u, err := url.Parse(bp)
  40. if err == nil {
  41. // could be a git repository containing the buildpack
  42. dstDir := filepath.Join(homedir.HomeDir(), ".porter")
  43. bpCustomName := strings.ReplaceAll(u.Path, "/", "-")
  44. downloader := &github.ZIPDownloader{
  45. ZipFolderDest: dstDir,
  46. AssetFolderDest: dstDir,
  47. ZipName: fmt.Sprintf("%s.zip", bpCustomName),
  48. RemoveAfterDownload: true,
  49. }
  50. err = downloader.DownloadToFile(bp)
  51. if err != nil {
  52. return err
  53. }
  54. err = downloader.UnzipToDir()
  55. if err != nil {
  56. return err
  57. }
  58. buildOpts.Buildpacks = append(buildOpts.Buildpacks, filepath.Join(dstDir, bpCustomName))
  59. } else {
  60. buildOpts.Buildpacks = append(buildOpts.Buildpacks, bp)
  61. }
  62. }
  63. // FIXME: use all the config vars
  64. }
  65. if strings.HasPrefix(buildOpts.Builder, "heroku") {
  66. buildOpts.Buildpacks = append(buildOpts.Buildpacks, "heroku/procfile")
  67. }
  68. return client.Build(context, buildOpts)
  69. }