2
0
Эх сурвалжийг харах

finish download + build deploy functionality from cli

Alexander Belanger 5 жил өмнө
parent
commit
21e22e2b2b

+ 51 - 15
cli/cmd/deploy.go

@@ -11,6 +11,7 @@ import (
 
 	"github.com/fatih/color"
 	"github.com/porter-dev/porter/cli/cmd/api"
+	"github.com/porter-dev/porter/cli/cmd/docker"
 	"github.com/porter-dev/porter/cli/cmd/github"
 	"github.com/spf13/cobra"
 )
@@ -261,36 +262,71 @@ func deployBuild(resp *api.AuthCheckResponse, client *api.Client, args []string)
 		}
 	}
 
+	zipResp, err := client.GetRepoZIPDownloadURL(
+		context.Background(),
+		config.Project,
+		release.GitActionConfig,
+	)
+
+	if err != nil {
+		return err
+	}
+
 	// download the repository from remote source into a temp directory
-	dst, err := downloadRepoToDir(client)
+	dst, err := downloadRepoToDir(zipResp.URLString)
 
 	if err != nil {
 		return err
 	}
 
-	fmt.Println("DEST IS", dst)
+	agent, err := docker.NewAgentFromEnv()
 
-	// agent, err := docker.NewAgentFromEnv()
+	if err != nil {
+		return err
+	}
+
+	err = pullCurrentReleaseImage(agent)
+
+	if err != nil {
+		return err
+	}
 
-	// if err != nil {
-	// 	return err
-	// }
+	// case on Dockerfile path
+	if release.GitActionConfig.DockerfilePath != "" {
+		return agent.BuildLocal(
+			release.GitActionConfig.DockerfilePath,
+			release.GitActionConfig.ImageRepoURI,
+			dst,
+		)
+	}
 
-	// return agent.BuildLocal("./docker/dev.Dockerfile", "test-porter:latest", "/Users/abelanger/porter/porter-server")
 	return nil
 }
 
-func downloadRepoToDir(client *api.Client) (string, error) {
-	resp, err := client.GetRepoZIPDownloadURL(
-		context.Background(),
-		config.Project,
-		release.GitActionConfig,
-	)
+func pullCurrentReleaseImage(agent *docker.Agent) error {
+	// pull the currently deployed image to use cache, if possible
+	imageConfig, err := getNestedMap(release.Config, "image")
 
 	if err != nil {
-		return "", err
+		return fmt.Errorf("could not get image config from release: %s", err.Error())
 	}
 
+	tagInterface, ok := imageConfig["tag"]
+
+	if !ok {
+		return fmt.Errorf("tag field does not exist for image")
+	}
+
+	tagStr, ok := tagInterface.(string)
+
+	if !ok {
+		return fmt.Errorf("could not cast image.tag field to string")
+	}
+
+	return agent.PullImage(fmt.Sprintf("%s:%s", release.GitActionConfig.ImageRepoURI, tagStr))
+}
+
+func downloadRepoToDir(downloadURL string) (string, error) {
 	dstDir := filepath.Join(home, ".porter")
 
 	downloader := &github.ZIPDownloader{
@@ -300,7 +336,7 @@ func downloadRepoToDir(client *api.Client) (string, error) {
 		RemoveAfterDownload: true,
 	}
 
-	err = downloader.DownloadToFile(resp.URLString)
+	err := downloader.DownloadToFile(downloadURL)
 
 	if err != nil {
 		return "", fmt.Errorf("Error downloading to file: %s", err.Error())

+ 20 - 9
internal/registry/registry.go

@@ -52,6 +52,9 @@ type Image struct {
 
 	// The name of the repository associated with the image.
 	RepositoryName string `json:"repository_name"`
+
+	// When the image was pushed
+	PushedAt *time.Time `json:"pushed_at"`
 }
 
 // ListRepositories lists the repositories for a registry
@@ -479,18 +482,26 @@ func (r *Registry) listECRImages(repoName string, repo repository.Repository) ([
 		return nil, err
 	}
 
+	describeResp, err := svc.DescribeImages(&ecr.DescribeImagesInput{
+		RepositoryName: &repoName,
+		ImageIds:       resp.ImageIds,
+	})
+
+	if err != nil {
+		return nil, err
+	}
+
 	res := make([]*Image, 0)
 
-	for _, img := range resp.ImageIds {
-		if img.ImageTag == nil {
-			continue
+	for _, img := range describeResp.ImageDetails {
+		for _, tag := range img.ImageTags {
+			res = append(res, &Image{
+				Digest:         *img.ImageDigest,
+				Tag:            *tag,
+				RepositoryName: repoName,
+				PushedAt:       img.ImagePushedAt,
+			})
 		}
-
-		res = append(res, &Image{
-			Digest:         *img.ImageDigest,
-			Tag:            *img.ImageTag,
-			RepositoryName: repoName,
-		})
 	}
 
 	return res, nil

+ 17 - 3
server/api/git_repo_handler.go

@@ -267,7 +267,8 @@ func (app *App) HandleGetProcfileContents(w http.ResponseWriter, r *http.Request
 }
 
 type HandleGetRepoZIPDownloadURLResp struct {
-	URLString string `json:"url"`
+	URLString       string `json:"url"`
+	LatestCommitSHA string `json:"latest_commit_sha"`
 }
 
 // HandleGetRepoZIPDownloadURL gets the URL for downloading a zip file from a Github
@@ -285,13 +286,25 @@ func (app *App) HandleGetRepoZIPDownloadURL(w http.ResponseWriter, r *http.Reque
 	name := chi.URLParam(r, "name")
 	branch := chi.URLParam(r, "branch")
 
+	branchResp, _, err := client.Repositories.GetBranch(
+		context.TODO(),
+		owner,
+		name,
+		branch,
+	)
+
+	if err != nil {
+		app.handleErrorInternal(err, w)
+		return
+	}
+
 	ghURL, _, err := client.Repositories.GetArchiveLink(
 		context.TODO(),
 		owner,
 		name,
 		github.Zipball,
 		&github.RepositoryContentGetOptions{
-			Ref: branch,
+			Ref: *branchResp.Commit.SHA,
 		},
 	)
 
@@ -301,7 +314,8 @@ func (app *App) HandleGetRepoZIPDownloadURL(w http.ResponseWriter, r *http.Reque
 	}
 
 	apiResp := HandleGetRepoZIPDownloadURLResp{
-		URLString: ghURL.String(),
+		URLString:       ghURL.String(),
+		LatestCommitSHA: *branchResp.Commit.SHA,
 	}
 
 	json.NewEncoder(w).Encode(apiResp)