| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- package cmd
- import (
- "fmt"
- "os"
- "github.com/fatih/color"
- "github.com/porter-dev/porter/cli/cmd/api"
- "github.com/porter-dev/porter/cli/cmd/deploy"
- "github.com/spf13/cobra"
- )
- // deployCmd represents the "porter deploy" base command when called
- // without any subcommands
- var deployCmd = &cobra.Command{
- Use: "deploy",
- Short: "Builds and deploys a specified application given by the --app flag.",
- Run: func(cmd *cobra.Command, args []string) {
- err := checkLoginAndRun(args, deployFull)
- if err != nil {
- os.Exit(1)
- }
- },
- }
- var deployGetEnvCmd = &cobra.Command{
- Use: "get-env",
- Short: "Gets environment variables for a deployment for a specified application given by the --app flag.",
- Long: fmt.Sprintf(`Gets environment variables for a deployment for a specified application given by the --app flag.
- By default, env variables are printed via stdout for use in downstream commands:
- %s
- Output can also be written to a dotenv file via the --file flag, which should specify the destination
- path for a .env file. For example:
- %s
- `,
- color.New(color.FgGreen).Sprintf("porter deploy get-env --app <app>"),
- color.New(color.FgGreen).Sprintf("porter deploy get-env --app <app> --file .env"),
- ),
- Run: func(cmd *cobra.Command, args []string) {
- err := checkLoginAndRun(args, deployGetEnv)
- if err != nil {
- os.Exit(1)
- }
- },
- }
- var deployBuildCmd = &cobra.Command{
- Use: "build",
- Short: "Builds a new version of the application specified by the --app flag.",
- Run: func(cmd *cobra.Command, args []string) {
- err := checkLoginAndRun(args, deployBuild)
- if err != nil {
- os.Exit(1)
- }
- },
- }
- var app string
- var getEnvFileDest string
- var local bool
- var localPath string
- var tag string
- func init() {
- rootCmd.AddCommand(deployCmd)
- deployCmd.PersistentFlags().StringVar(
- &app,
- "app",
- "",
- "Application in the Porter dashboard",
- )
- deployCmd.PersistentFlags().StringVar(
- &namespace,
- "namespace",
- "default",
- "Namespace of the application",
- )
- deployCmd.PersistentFlags().BoolVar(
- &local,
- "local",
- false,
- "Whether local context should be used for build",
- )
- deployCmd.PersistentFlags().StringVarP(
- &localPath,
- "path",
- "p",
- ".",
- "If local build, the path to the build directory",
- )
- deployCmd.PersistentFlags().StringVarP(
- &tag,
- "tag",
- "t",
- "",
- "If local build, the specified tag to use, if not \"latest\"",
- )
- deployCmd.AddCommand(deployGetEnvCmd)
- deployGetEnvCmd.PersistentFlags().StringVar(
- &getEnvFileDest,
- "file",
- "",
- "file destination for .env files",
- )
- deployCmd.AddCommand(deployBuildCmd)
- }
- func deployFull(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
- color.New(color.FgGreen).Println("Deploying app:", app)
- // initialize the deploy agent
- deployAgent, err := deploy.NewDeployAgent(client, app, &deploy.DeployOpts{
- ProjectID: config.Project,
- ClusterID: config.Cluster,
- Namespace: namespace,
- Local: local,
- LocalPath: localPath,
- OverrideTag: tag,
- })
- if err != nil {
- return err
- }
- buildEnv, err := deployAgent.GetBuildEnv()
- if err != nil {
- return err
- }
- // set the environment variables in the process
- err = deployAgent.SetBuildEnv(buildEnv)
- if err != nil {
- return err
- }
- // build the deployment
- color.New(color.FgGreen).Println("Building docker image for", app)
- err = deployAgent.Build()
- if err != nil {
- return err
- }
- // push the deployment
- color.New(color.FgGreen).Println("Deploying new application for", app)
- err = deployAgent.Deploy()
- if err != nil {
- return err
- }
- color.New(color.FgGreen).Println("Successfully deployed", app)
- return nil
- }
- func deployGetEnv(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
- // initialize the deploy agent
- deployAgent, err := deploy.NewDeployAgent(client, app, &deploy.DeployOpts{
- ProjectID: config.Project,
- ClusterID: config.Cluster,
- Namespace: namespace,
- })
- if err != nil {
- return err
- }
- buildEnv, err := deployAgent.GetBuildEnv()
- if err != nil {
- return err
- }
- // set the environment variables in the process
- err = deployAgent.SetBuildEnv(buildEnv)
- if err != nil {
- return err
- }
- // write the environment variables to either a file or stdout (stdout by default)
- return deployAgent.WriteBuildEnv(getEnvFileDest)
- }
- func deployBuild(resp *api.AuthCheckResponse, client *api.Client, args []string) error {
- color.New(color.FgGreen).Println("Building app:", app)
- // initialize the deploy agent
- deployAgent, err := deploy.NewDeployAgent(client, app, &deploy.DeployOpts{
- ProjectID: config.Project,
- ClusterID: config.Cluster,
- Namespace: namespace,
- })
- if err != nil {
- return err
- }
- buildEnv, err := deployAgent.GetBuildEnv()
- if err != nil {
- return err
- }
- // set the environment variables in the process
- err = deployAgent.SetBuildEnv(buildEnv)
- if err != nil {
- return err
- }
- // build the deployment
- color.New(color.FgGreen).Println("Building docker image for", app)
- return deployAgent.Build()
- }
|