From 4ef0aafc7bd267f9b2fb18e6014a33cb1a23d057 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Thu, 9 May 2019 15:48:05 -0400 Subject: [PATCH] fetch and build any GitHub repo --- cmd/buildpack/main.go | 26 ++++++++++++++++++-------- deploy.sh | 12 ++++++------ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/cmd/buildpack/main.go b/cmd/buildpack/main.go index 5205161..3d7691a 100644 --- a/cmd/buildpack/main.go +++ b/cmd/buildpack/main.go @@ -31,10 +31,7 @@ func init() { projectID = p } -const ( - source = "https://github.com/buildpack/sample-java-app/archive/master.tar.gz" - base = "packs/run:v3alpha2" -) +const base = "packs/run:v3alpha2" func main() { ctx := context.Background() @@ -71,9 +68,9 @@ func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch { case path == "": // API Version check. w.Header().Set("Docker-Distribution-API-Version", "registry/2.0") - case strings.HasPrefix(path, "buildpack/") && strings.Contains(path, "/manifests/"): + case strings.Contains(path, "/manifests/"): s.serveBuildpackManifest(w, r) - case strings.HasPrefix(path, "buildpack/blobs/") && strings.Contains(path, "/blobs/"): + case strings.Contains(path, "/blobs/"): pkg.ServeBlob(w, r) default: http.Error(w, "not found", http.StatusNotFound) @@ -100,8 +97,20 @@ func (s *server) serveBuildpackManifest(w http.ResponseWriter, r *http.Request) os.Setenv("HOME", "/home/") }() + // Determine source repo and revision. + path := strings.TrimPrefix(r.URL.Path, "/v2/ko/") + parts := strings.Split(path, "/") + repo := strings.Join(parts[2:len(parts)-2], "/") + if repo == "" || repo == "buildpack" { + repo = "buildpack/sample-java-app" + } + revision := parts[len(parts)-1] + if revision == "latest" { + revision = "master" + } + // Fetch, detect and build source. - image, err := s.fetchAndBuild(src, layers) + image, err := s.fetchAndBuild(src, layers, repo, revision) if err != nil { s.error.Println("ERROR:", err) http.Error(w, err.Error(), http.StatusInternalServerError) @@ -158,8 +167,9 @@ func (s *server) prepareWorkspace() (string, string, error) { return src, layers, nil } -func (s *server) fetchAndBuild(src, layers string) (string, error) { +func (s *server) fetchAndBuild(src, layers, repo, revision string) (string, error) { image := fmt.Sprintf("gcr.io/%s/built-%d", projectID, time.Now().Unix) + source := fmt.Sprintf("https://github.com/%s/archive/%s.tar.gz", repo, revision) for _, cmd := range []string{ fmt.Sprintf("chown -R %d:%d %s", os.Geteuid(), os.Getgid(), src), diff --git a/deploy.sh b/deploy.sh index cb6fafb..1efd0bd 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,11 +1,11 @@ #!/bin/bash -KO_DOCKER_REPO=gcr.io/kontainme ko publish -P ./cmd/ko && \ -gcloud --project=kontainme beta run deploy app \ - --image=gcr.io/kontainme/github.com/imjasonh/kontain.me/cmd/ko \ - --memory=2Gi \ - --concurrency=1 \ - --region=us-central1 +#KO_DOCKER_REPO=gcr.io/kontainme ko publish -P ./cmd/ko && \ +#gcloud --project=kontainme beta run deploy app \ +# --image=gcr.io/kontainme/github.com/imjasonh/kontain.me/cmd/ko \ +# --memory=2Gi \ +# --concurrency=1 \ +# --region=us-central1 KO_DOCKER_REPO=gcr.io/kontainme ko publish -P ./cmd/buildpack && \ gcloud --project=kontainme beta run deploy buildpack \