builder.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // attach BUILDKIT_INLINE_CACHE=1 by default, to take advantage of caching
  52. inlineCacheVal := "1"
  53. buildArgs["BUILDKIT_INLINE_CACHE"] = &inlineCacheVal
  54. out, err := a.client.ImageBuild(context.Background(), tar, types.ImageBuildOptions{
  55. Dockerfile: dockerfilePath,
  56. BuildArgs: buildArgs,
  57. Tags: []string{
  58. fmt.Sprintf("%s:%s", opts.ImageRepo, opts.Tag),
  59. },
  60. CacheFrom: []string{
  61. fmt.Sprintf("%s:%s", opts.ImageRepo, opts.CurrentTag),
  62. },
  63. Remove: true,
  64. Platform: "linux/amd64",
  65. })
  66. if err != nil {
  67. return err
  68. }
  69. defer out.Body.Close()
  70. termFd, isTerm := term.GetFdInfo(os.Stderr)
  71. return jsonmessage.DisplayJSONMessagesStream(out.Body, os.Stderr, termFd, isTerm, nil)
  72. }
  73. // AddDockerfileToBuildContext from a ReadCloser, returns a new archive and
  74. // the relative path to the dockerfile in the context.
  75. func AddDockerfileToBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.ReadCloser) (io.ReadCloser, string, error) {
  76. file, err := ioutil.ReadAll(dockerfileCtx)
  77. dockerfileCtx.Close()
  78. if err != nil {
  79. return nil, "", err
  80. }
  81. now := time.Now()
  82. hdrTmpl := &tar.Header{
  83. Mode: 0600,
  84. Uid: 0,
  85. Gid: 0,
  86. ModTime: now,
  87. Typeflag: tar.TypeReg,
  88. AccessTime: now,
  89. ChangeTime: now,
  90. }
  91. randomName := ".dockerfile." + stringid.GenerateRandomID()[:20]
  92. buildCtx = archive.ReplaceFileTarWrapper(buildCtx, map[string]archive.TarModifierFunc{
  93. // Add the dockerfile with a random filename
  94. randomName: func(_ string, h *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
  95. return hdrTmpl, file, nil
  96. },
  97. // Update .dockerignore to include the random filename
  98. ".dockerignore": func(_ string, h *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
  99. if h == nil {
  100. h = hdrTmpl
  101. }
  102. b := &bytes.Buffer{}
  103. if content != nil {
  104. if _, err := b.ReadFrom(content); err != nil {
  105. return nil, nil, err
  106. }
  107. } else {
  108. b.WriteString(".dockerignore")
  109. }
  110. b.WriteString("\n" + randomName + "\n")
  111. return h, b.Bytes(), nil
  112. },
  113. })
  114. return buildCtx, randomName, nil
  115. }