2
0

builder.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. Platform: "linux/amd64",
  62. })
  63. if err != nil {
  64. return err
  65. }
  66. defer out.Body.Close()
  67. termFd, isTerm := term.GetFdInfo(os.Stderr)
  68. return jsonmessage.DisplayJSONMessagesStream(out.Body, os.Stderr, termFd, isTerm, nil)
  69. }
  70. // AddDockerfileToBuildContext from a ReadCloser, returns a new archive and
  71. // the relative path to the dockerfile in the context.
  72. func AddDockerfileToBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.ReadCloser) (io.ReadCloser, string, error) {
  73. file, err := ioutil.ReadAll(dockerfileCtx)
  74. dockerfileCtx.Close()
  75. if err != nil {
  76. return nil, "", err
  77. }
  78. now := time.Now()
  79. hdrTmpl := &tar.Header{
  80. Mode: 0600,
  81. Uid: 0,
  82. Gid: 0,
  83. ModTime: now,
  84. Typeflag: tar.TypeReg,
  85. AccessTime: now,
  86. ChangeTime: now,
  87. }
  88. randomName := ".dockerfile." + stringid.GenerateRandomID()[:20]
  89. buildCtx = archive.ReplaceFileTarWrapper(buildCtx, map[string]archive.TarModifierFunc{
  90. // Add the dockerfile with a random filename
  91. randomName: func(_ string, h *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
  92. return hdrTmpl, file, nil
  93. },
  94. // Update .dockerignore to include the random filename
  95. ".dockerignore": func(_ string, h *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
  96. if h == nil {
  97. h = hdrTmpl
  98. }
  99. b := &bytes.Buffer{}
  100. if content != nil {
  101. if _, err := b.ReadFrom(content); err != nil {
  102. return nil, nil, err
  103. }
  104. } else {
  105. b.WriteString(".dockerignore")
  106. }
  107. b.WriteString("\n" + randomName + "\n")
  108. return h, b.Bytes(), nil
  109. },
  110. })
  111. return buildCtx, randomName, nil
  112. }