build.go 1.9 KB

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