build.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package deploy
  2. import (
  3. "fmt"
  4. "github.com/porter-dev/porter/cli/cmd/api"
  5. "github.com/porter-dev/porter/cli/cmd/docker"
  6. "github.com/porter-dev/porter/cli/cmd/pack"
  7. )
  8. type BuildAgent struct {
  9. *SharedOpts
  10. client *api.Client
  11. imageRepo string
  12. env map[string]string
  13. imageExists bool
  14. }
  15. func (b *BuildAgent) BuildDocker(dockerAgent *docker.Agent, dst, tag string) error {
  16. opts := &docker.BuildOpts{
  17. ImageRepo: b.imageRepo,
  18. Tag: tag,
  19. BuildContext: dst,
  20. Env: b.env,
  21. }
  22. return dockerAgent.BuildLocal(
  23. opts,
  24. b.LocalDockerfile,
  25. )
  26. }
  27. func (b *BuildAgent) BuildPack(dockerAgent *docker.Agent, dst, tag string) error {
  28. // retag the image with "pack-cache" tag so that it doesn't re-pull from the registry
  29. if b.imageExists {
  30. err := dockerAgent.TagImage(
  31. fmt.Sprintf("%s:%s", b.imageRepo, tag),
  32. fmt.Sprintf("%s:%s", b.imageRepo, "pack-cache"),
  33. )
  34. if err != nil {
  35. return err
  36. }
  37. }
  38. // create pack agent and build opts
  39. packAgent := &pack.Agent{}
  40. opts := &docker.BuildOpts{
  41. ImageRepo: b.imageRepo,
  42. // We tag the image with a stable param "pack-cache" so that pack can use the
  43. // local image without attempting to re-pull from registry. We handle getting
  44. // registry credentials and pushing/pulling the image.
  45. Tag: "pack-cache",
  46. BuildContext: dst,
  47. Env: b.env,
  48. }
  49. // call builder
  50. err := packAgent.Build(opts)
  51. if err != nil {
  52. return err
  53. }
  54. return dockerAgent.TagImage(
  55. fmt.Sprintf("%s:%s", b.imageRepo, "pack-cache"),
  56. fmt.Sprintf("%s:%s", b.imageRepo, tag),
  57. )
  58. }