builder.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package docker
  2. import (
  3. "archive/tar"
  4. "bytes"
  5. "context"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "os"
  10. "time"
  11. "github.com/docker/docker/api/types"
  12. "github.com/docker/docker/pkg/archive"
  13. "github.com/moby/moby/pkg/jsonmessage"
  14. "github.com/moby/moby/pkg/stringid"
  15. "github.com/moby/term"
  16. "github.com/pkg/errors"
  17. )
  18. type BuildOpts struct {
  19. ImageRepo string
  20. Tag string
  21. CurrentTag string
  22. BuildContext string
  23. DockerfilePath string
  24. IsDockerfileInCtx bool
  25. Env map[string]string
  26. }
  27. // BuildLocal
  28. func (a *Agent) BuildLocal(opts *BuildOpts) error {
  29. dockerfilePath := opts.DockerfilePath
  30. tar, err := archive.TarWithOptions(opts.BuildContext, &archive.TarOptions{})
  31. if err != nil {
  32. return err
  33. }
  34. if !opts.IsDockerfileInCtx {
  35. dockerfileCtx, err := os.Open(dockerfilePath)
  36. if err != nil {
  37. return errors.Errorf("unable to open Dockerfile: %v", err)
  38. }
  39. defer dockerfileCtx.Close()
  40. // add the dockerfile to the build context
  41. tar, dockerfilePath, err = AddDockerfileToBuildContext(dockerfileCtx, tar)
  42. if err != nil {
  43. return err
  44. }
  45. }
  46. buildArgs := make(map[string]*string)
  47. for key, val := range opts.Env {
  48. valCopy := val
  49. buildArgs[key] = &valCopy
  50. }
  51. out, err := a.client.ImageBuild(context.Background(), tar, types.ImageBuildOptions{
  52. Dockerfile: dockerfilePath,
  53. BuildArgs: buildArgs,
  54. Tags: []string{
  55. fmt.Sprintf("%s:%s", opts.ImageRepo, opts.Tag),
  56. },
  57. CacheFrom: []string{
  58. fmt.Sprintf("%s:%s", opts.ImageRepo, opts.CurrentTag),
  59. },
  60. Remove: true,
  61. })
  62. if err != nil {
  63. return err
  64. }
  65. defer out.Body.Close()
  66. termFd, isTerm := term.GetFdInfo(os.Stderr)
  67. return jsonmessage.DisplayJSONMessagesStream(out.Body, os.Stderr, termFd, isTerm, nil)
  68. }
  69. // AddDockerfileToBuildContext from a ReadCloser, returns a new archive and
  70. // the relative path to the dockerfile in the context.
  71. func AddDockerfileToBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.ReadCloser) (io.ReadCloser, string, error) {
  72. file, err := ioutil.ReadAll(dockerfileCtx)
  73. dockerfileCtx.Close()
  74. if err != nil {
  75. return nil, "", err
  76. }
  77. now := time.Now()
  78. hdrTmpl := &tar.Header{
  79. Mode: 0600,
  80. Uid: 0,
  81. Gid: 0,
  82. ModTime: now,
  83. Typeflag: tar.TypeReg,
  84. AccessTime: now,
  85. ChangeTime: now,
  86. }
  87. randomName := ".dockerfile." + stringid.GenerateRandomID()[:20]
  88. buildCtx = archive.ReplaceFileTarWrapper(buildCtx, map[string]archive.TarModifierFunc{
  89. // Add the dockerfile with a random filename
  90. randomName: func(_ string, h *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
  91. return hdrTmpl, file, nil
  92. },
  93. // Update .dockerignore to include the random filename
  94. ".dockerignore": func(_ string, h *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
  95. if h == nil {
  96. h = hdrTmpl
  97. }
  98. b := &bytes.Buffer{}
  99. if content != nil {
  100. if _, err := b.ReadFrom(content); err != nil {
  101. return nil, nil, err
  102. }
  103. } else {
  104. b.WriteString(".dockerignore")
  105. }
  106. b.WriteString("\n" + randomName + "\n")
  107. return h, b.Bytes(), nil
  108. },
  109. })
  110. return buildCtx, randomName, nil
  111. }