11package build
22
33import (
4+ "archive/tar"
45 "bufio"
6+ "bytes"
57 "fmt"
68 "io"
79 "io/ioutil"
10+ "net/http"
811 "os"
912 "os/exec"
1013 "path/filepath"
1114 "runtime"
1215 "strings"
13-
14- "archive/tar"
15- "bytes"
1616 "time"
1717
18+ "github.com/docker/docker/builder/remotecontext/git"
1819 "github.com/docker/docker/pkg/archive"
1920 "github.com/docker/docker/pkg/fileutils"
20- "github.com/docker/docker/pkg/gitutils"
21- "github.com/docker/docker/pkg/httputils"
2221 "github.com/docker/docker/pkg/ioutils"
2322 "github.com/docker/docker/pkg/progress"
2423 "github.com/docker/docker/pkg/streamformatter"
@@ -143,7 +142,7 @@ func GetContextFromGitURL(gitURL, dockerfileName string) (string, string, error)
143142 if _ , err := exec .LookPath ("git" ); err != nil {
144143 return "" , "" , errors .Wrapf (err , "unable to find 'git'" )
145144 }
146- absContextDir , err := gitutils .Clone (gitURL )
145+ absContextDir , err := git .Clone (gitURL )
147146 if err != nil {
148147 return "" , "" , errors .Wrapf (err , "unable to 'git clone' to temporary context directory" )
149148 }
@@ -161,7 +160,7 @@ func GetContextFromGitURL(gitURL, dockerfileName string) (string, string, error)
161160// Returns the tar archive used for the context and a path of the
162161// dockerfile inside the tar.
163162func GetContextFromURL (out io.Writer , remoteURL , dockerfileName string ) (io.ReadCloser , string , error ) {
164- response , err := httputils . Download (remoteURL )
163+ response , err := getWithStatusError (remoteURL )
165164 if err != nil {
166165 return nil , "" , errors .Errorf ("unable to download remote context %s: %v" , remoteURL , err )
167166 }
@@ -173,6 +172,24 @@ func GetContextFromURL(out io.Writer, remoteURL, dockerfileName string) (io.Read
173172 return GetContextFromReader (ioutils .NewReadCloserWrapper (progReader , func () error { return response .Body .Close () }), dockerfileName )
174173}
175174
175+ // getWithStatusError does an http.Get() and returns an error if the
176+ // status code is 4xx or 5xx.
177+ func getWithStatusError (url string ) (resp * http.Response , err error ) {
178+ if resp , err = http .Get (url ); err != nil {
179+ return nil , err
180+ }
181+ if resp .StatusCode < 400 {
182+ return resp , nil
183+ }
184+ msg := fmt .Sprintf ("failed to GET %s with status %s" , url , resp .Status )
185+ body , err := ioutil .ReadAll (resp .Body )
186+ resp .Body .Close ()
187+ if err != nil {
188+ return nil , errors .Wrapf (err , msg + ": error reading body" )
189+ }
190+ return nil , errors .Errorf (msg + ": %s" , bytes .TrimSpace (body ))
191+ }
192+
176193// GetContextFromLocalDir uses the given local directory as context for a
177194// `docker build`. Returns the absolute path to the local context directory,
178195// the relative path of the dockerfile in that context directory, and a non-nil
0 commit comments