-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: Add alpha support for triggering deploys from cli
- Loading branch information
Showing
3 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/cockroachdb/errors" | ||
"github.com/logrusorgru/aurora/v3" | ||
"github.com/spf13/cobra" | ||
|
||
"encr.dev/cli/cmd/encore/cmdutil" | ||
"encr.dev/cli/internal/platform" | ||
"encr.dev/pkg/appfile" | ||
) | ||
|
||
var ( | ||
appSlug string | ||
envName string | ||
commit string | ||
branch string | ||
) | ||
|
||
var deployAppCmd = &cobra.Command{ | ||
Use: "deploy --commit COMMIT_SHA_OR_BRANCH [flags]", | ||
Short: "Deploy an Encore app to a cloud environment", | ||
DisableFlagsInUseLine: true, | ||
Hidden: true, | ||
Run: func(c *cobra.Command, args []string) { | ||
if commit != "" { | ||
hb, err := hex.DecodeString(commit) | ||
if err != nil || len(hb) != 20 { | ||
cmdutil.Fatalf("invalid commit: %s", commit) | ||
} | ||
} | ||
if appSlug == "" { | ||
appRoot, _, err := cmdutil.MaybeAppRoot() | ||
if err != nil { | ||
cmdutil.Fatalf("no app found. Run deploy inside an encore app directory or specify the app with --app") | ||
} | ||
appSlug, err = appfile.Slug(appRoot) | ||
if err != nil { | ||
cmdutil.Fatalf("no app found. Run deploy inside an encore app directory or specify the app with --app") | ||
} | ||
} | ||
if envName == "" { | ||
envName = "@primary" | ||
} | ||
rollout, err := platform.Deploy(c.Context(), appSlug, envName, commit, branch) | ||
var pErr platform.Error | ||
if ok := errors.As(err, &pErr); ok { | ||
switch pErr.Code { | ||
case "app_not_found": | ||
cmdutil.Fatalf("app not found: %s", appSlug) | ||
case "validation": | ||
var details platform.ValidationDetails | ||
err := json.Unmarshal(pErr.Detail, &details) | ||
if err != nil { | ||
cmdutil.Fatalf("failed to deploy: %v", err) | ||
} | ||
switch details.Field { | ||
case "commit": | ||
cmdutil.Fatalf("could not find commit: %s. Is it pushed to the remote repository?", commit) | ||
case "branch": | ||
cmdutil.Fatalf("could not find branch: %s. Is it pushed to the remote repository?", branch) | ||
case "env": | ||
cmdutil.Fatalf("could not find environment: %s/%s", appSlug, envName) | ||
} | ||
} | ||
} | ||
if err != nil { | ||
cmdutil.Fatalf("failed to deploy: %v", err) | ||
} | ||
url := fmt.Sprintf("https://app.encore.dev/%s/deploys/%s/%s", appSlug, rollout.EnvName, strings.TrimPrefix(rollout.ID, "roll_")) | ||
fmt.Println(aurora.Sprintf("\n%s %s\n", aurora.Bold("Started Deploy:"), url)) | ||
}, | ||
} | ||
|
||
func init() { | ||
alphaCmd.AddCommand(deployAppCmd) | ||
deployAppCmd.Flags().StringVar(&appSlug, "app", "", "app slug to deploy to. Defaults to the app defined in encore.app.") | ||
deployAppCmd.Flags().StringVarP(&envName, "env", "e", "", "the environment to deploy to. Defaults to primary environment.") | ||
deployAppCmd.Flags().StringVarP(&commit, "commit", "c", "", "commit to deploy.") | ||
deployAppCmd.Flags().StringVarP(&branch, "branch", "b", "", "branch to deploy.") | ||
deployAppCmd.MarkFlagsMutuallyExclusive("commit", "branch") | ||
deployAppCmd.MarkFlagsOneRequired("commit", "branch") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters