فهرست منبع

add support for manually injecting env to build/deploy cli commands

Alexander Belanger 4 سال پیش
والد
کامیت
f5d0bb7046
5فایلهای تغییر یافته به همراه59 افزوده شده و 1 حذف شده
  1. 19 0
      cli/cmd/create.go
  2. 22 0
      cli/cmd/deploy.go
  3. 5 0
      cli/cmd/deploy/create.go
  4. 12 1
      cli/cmd/deploy/deploy.go
  5. 1 0
      cli/cmd/deploy/shared.go

+ 19 - 0
cli/cmd/create.go

@@ -5,6 +5,7 @@ import (
 	"io/ioutil"
 	"os"
 	"path/filepath"
+	"strings"
 
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
@@ -119,6 +120,14 @@ func init() {
 		"the path to the dockerfile",
 	)
 
+	createCmd.PersistentFlags().StringArrayVarP(
+		&buildFlagsEnv,
+		"env",
+		"e",
+		[]string{},
+		"Build-time environment variable, in the form 'VAR=VALUE'. These are not available at image runtime.",
+	)
+
 	createCmd.PersistentFlags().StringVar(
 		&method,
 		"method",
@@ -179,6 +188,15 @@ func createFull(_ *types.GetAuthenticatedUserResponse, client *api.Client, args
 		buildMethod = deploy.DeployBuildTypeDocker
 	}
 
+	// add additional env, if they exist
+	additionalEnv := make(map[string]string)
+
+	for _, buildEnv := range buildFlagsEnv {
+		if strSplArr := strings.SplitN(buildEnv, "=", 2); len(strSplArr) >= 2 {
+			additionalEnv[strSplArr[0]] = strSplArr[1]
+		}
+	}
+
 	createAgent := &deploy.CreateAgent{
 		Client: client,
 		CreateOpts: &deploy.CreateOpts{
@@ -189,6 +207,7 @@ func createFull(_ *types.GetAuthenticatedUserResponse, client *api.Client, args
 				LocalPath:       fullPath,
 				LocalDockerfile: dockerfile,
 				Method:          buildMethod,
+				AdditionalEnv:   additionalEnv,
 			},
 			Kind:        args[0],
 			ReleaseName: name,

+ 22 - 0
cli/cmd/deploy.go

@@ -3,6 +3,7 @@ package cmd
 import (
 	"fmt"
 	"os"
+	"strings"
 
 	"github.com/fatih/color"
 	api "github.com/porter-dev/porter/api/client"
@@ -204,8 +205,11 @@ var tag string
 var dockerfile string
 var method string
 var stream bool
+var buildFlagsEnv []string
 
 func init() {
+	buildFlagsEnv = []string{}
+
 	rootCmd.AddCommand(updateCmd)
 
 	updateCmd.PersistentFlags().StringVar(
@@ -262,6 +266,14 @@ func init() {
 		"the path to the dockerfile",
 	)
 
+	updateCmd.PersistentFlags().StringArrayVarP(
+		&buildFlagsEnv,
+		"env",
+		"e",
+		[]string{},
+		"Build-time environment variable, in the form 'VAR=VALUE'. These are not available at image runtime.",
+	)
+
 	updateCmd.PersistentFlags().StringVar(
 		&method,
 		"method",
@@ -382,6 +394,15 @@ func updateGetAgent(client *api.Client) (*deploy.DeployAgent, error) {
 		buildMethod = deploy.DeployBuildType(method)
 	}
 
+	// add additional env, if they exist
+	additionalEnv := make(map[string]string)
+
+	for _, buildEnv := range buildFlagsEnv {
+		if strSplArr := strings.SplitN(buildEnv, "=", 2); len(strSplArr) >= 2 {
+			additionalEnv[strSplArr[0]] = strSplArr[1]
+		}
+	}
+
 	// initialize the update agent
 	return deploy.NewDeployAgent(client, app, &deploy.DeployOpts{
 		SharedOpts: &deploy.SharedOpts{
@@ -392,6 +413,7 @@ func updateGetAgent(client *api.Client) (*deploy.DeployAgent, error) {
 			LocalDockerfile: dockerfile,
 			OverrideTag:     tag,
 			Method:          buildMethod,
+			AdditionalEnv:   additionalEnv,
 		},
 		Local: source != "github",
 	})

+ 5 - 0
cli/cmd/deploy/create.go

@@ -272,6 +272,11 @@ func (c *CreateAgent) CreateFromDocker(
 		env = map[string]string{}
 	}
 
+	// add additional env based on options
+	for key, val := range opts.SharedOpts.AdditionalEnv {
+		env[key] = val
+	}
+
 	buildAgent := &BuildAgent{
 		SharedOpts:  opts.SharedOpts,
 		client:      c.Client,

+ 12 - 1
cli/cmd/deploy/deploy.go

@@ -142,7 +142,18 @@ func NewDeployAgent(client *client.Client, app string, opts *DeployOpts) (*Deplo
 
 // GetBuildEnv retrieves the build env from the release config and returns it
 func (d *DeployAgent) GetBuildEnv() (map[string]string, error) {
-	return GetEnvFromConfig(d.release.Config)
+	env, err := GetEnvFromConfig(d.release.Config)
+
+	if err != nil {
+		return nil, err
+	}
+
+	// add additional env based on options
+	for key, val := range d.opts.SharedOpts.AdditionalEnv {
+		env[key] = val
+	}
+
+	return env, nil
 }
 
 // SetBuildEnv sets the build env vars in the process so that other commands can

+ 1 - 0
cli/cmd/deploy/shared.go

@@ -9,4 +9,5 @@ type SharedOpts struct {
 	LocalDockerfile string
 	OverrideTag     string
 	Method          DeployBuildType
+	AdditionalEnv   map[string]string
 }