builder.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. UseCache bool
  26. Env map[string]string
  27. }
  28. // BuildLocal
  29. func (a *Agent) BuildLocal(opts *BuildOpts) error {
  30. dockerfilePath := opts.DockerfilePath
  31. tar, err := archive.TarWithOptions(opts.BuildContext, &archive.TarOptions{})
  32. if err != nil {
  33. return err
  34. }
  35. if !opts.IsDockerfileInCtx {
  36. dockerfileCtx, err := os.Open(dockerfilePath)
  37. if err != nil {
  38. return errors.Errorf("unable to open Dockerfile: %v", err)
  39. }
  40. defer dockerfileCtx.Close()
  41. // add the dockerfile to the build context
  42. tar, dockerfilePath, err = AddDockerfileToBuildContext(dockerfileCtx, tar)
  43. if err != nil {
  44. return err
  45. }
  46. }
  47. buildArgs := make(map[string]*string)
  48. for key, val := range opts.Env {
  49. valCopy := val
  50. buildArgs[key] = &valCopy
  51. }
  52. // attach BUILDKIT_INLINE_CACHE=1 by default, to take advantage of caching
  53. inlineCacheVal := "1"
  54. buildArgs["BUILDKIT_INLINE_CACHE"] = &inlineCacheVal
  55. out, err := a.ImageBuild(context.Background(), tar, types.ImageBuildOptions{
  56. Dockerfile: dockerfilePath,
  57. BuildArgs: buildArgs,
  58. Tags: []string{
  59. fmt.Sprintf("%s:%s", opts.ImageRepo, opts.Tag),
  60. },
  61. CacheFrom: []string{
  62. fmt.Sprintf("%s:%s", opts.ImageRepo, opts.CurrentTag),
  63. },
  64. Remove: true,
  65. Platform: "linux/amd64",
  66. })
  67. if err != nil {
  68. return err
  69. }
  70. defer out.Body.Close()
  71. termFd, isTerm := term.GetFdInfo(os.Stderr)
  72. return jsonmessage.DisplayJSONMessagesStream(out.Body, os.Stderr, termFd, isTerm, nil)
  73. }
  74. // AddDockerfileToBuildContext from a ReadCloser, returns a new archive and
  75. // the relative path to the dockerfile in the context.
  76. func AddDockerfileToBuildContext(dockerfileCtx io.ReadCloser, buildCtx io.ReadCloser) (io.ReadCloser, string, error) {
  77. file, err := ioutil.ReadAll(dockerfileCtx)
  78. dockerfileCtx.Close()
  79. if err != nil {
  80. return nil, "", err
  81. }
  82. now := time.Now()
  83. hdrTmpl := &tar.Header{
  84. Mode: 0600,
  85. Uid: 0,
  86. Gid: 0,
  87. ModTime: now,
  88. Typeflag: tar.TypeReg,
  89. AccessTime: now,
  90. ChangeTime: now,
  91. }
  92. randomName := ".dockerfile." + stringid.GenerateRandomID()[:20]
  93. buildCtx = archive.ReplaceFileTarWrapper(buildCtx, map[string]archive.TarModifierFunc{
  94. // Add the dockerfile with a random filename
  95. randomName: func(_ string, h *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
  96. return hdrTmpl, file, nil
  97. },
  98. // Update .dockerignore to include the random filename
  99. ".dockerignore": func(_ string, h *tar.Header, content io.Reader) (*tar.Header, []byte, error) {
  100. if h == nil {
  101. h = hdrTmpl
  102. }
  103. b := &bytes.Buffer{}
  104. if content != nil {
  105. if _, err := b.ReadFrom(content); err != nil {
  106. return nil, nil, err
  107. }
  108. } else {
  109. b.WriteString(".dockerignore")
  110. }
  111. b.WriteString("\n" + randomName + "\n")
  112. return h, b.Bytes(), nil
  113. },
  114. })
  115. return buildCtx, randomName, nil
  116. }