build.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // BuildAgent builds a new Docker container image for a new version of an application
  9. type BuildAgent struct {
  10. *SharedOpts
  11. client *api.Client
  12. imageRepo string
  13. env map[string]string
  14. imageExists bool
  15. }
  16. // BuildDocker uses the local Docker daemon to build the image
  17. func (b *BuildAgent) BuildDocker(dockerAgent *docker.Agent, dst, tag string) error {
  18. opts := &docker.BuildOpts{
  19. ImageRepo: b.imageRepo,
  20. Tag: tag,
  21. BuildContext: dst,
  22. Env: b.env,
  23. }
  24. return dockerAgent.BuildLocal(
  25. opts,
  26. b.LocalDockerfile,
  27. )
  28. }
  29. // BuildPack uses the cloud-native buildpack client to build a container image
  30. func (b *BuildAgent) BuildPack(dockerAgent *docker.Agent, dst, tag string) error {
  31. // retag the image with "pack-cache" tag so that it doesn't re-pull from the registry
  32. if b.imageExists {
  33. err := dockerAgent.TagImage(
  34. fmt.Sprintf("%s:%s", b.imageRepo, tag),
  35. fmt.Sprintf("%s:%s", b.imageRepo, "pack-cache"),
  36. )
  37. if err != nil {
  38. return err
  39. }
  40. }
  41. // create pack agent and build opts
  42. packAgent := &pack.Agent{}
  43. opts := &docker.BuildOpts{
  44. ImageRepo: b.imageRepo,
  45. // We tag the image with a stable param "pack-cache" so that pack can use the
  46. // local image without attempting to re-pull from registry. We handle getting
  47. // registry credentials and pushing/pulling the image.
  48. Tag: "pack-cache",
  49. BuildContext: dst,
  50. Env: b.env,
  51. }
  52. // call builder
  53. err := packAgent.Build(opts)
  54. if err != nil {
  55. return err
  56. }
  57. return dockerAgent.TagImage(
  58. fmt.Sprintf("%s:%s", b.imageRepo, "pack-cache"),
  59. fmt.Sprintf("%s:%s", b.imageRepo, tag),
  60. )
  61. }