From 7730e57b5874a61e918cd962880660611f067e2d Mon Sep 17 00:00:00 2001 From: Eric Ho Date: Thu, 6 Sep 2018 15:41:36 +0800 Subject: [PATCH] Export deployEnvPath as TF_VAR_* --- build-docker.sh | 16 ++++++++-------- main.go | 29 ++++++++++++++++++----------- plugin.go | 37 ++++++++++++++++++++++++++----------- 3 files changed, 52 insertions(+), 30 deletions(-) diff --git a/build-docker.sh b/build-docker.sh index cf1c4d7..c2af9c6 100755 --- a/build-docker.sh +++ b/build-docker.sh @@ -13,15 +13,15 @@ minor=$(echo $tag | awk -F. '{print $2}') tf_ver=$(grep TERRAFORM_VERSION Dockerfile | head -n 1 | awk '{print $3}') -docker build -t jmccann/drone-terraform:latest . +docker build -t moneysmartco/drone-terraform:latest . set -x -docker tag jmccann/drone-terraform:latest jmccann/drone-terraform:${major} -docker tag jmccann/drone-terraform:latest jmccann/drone-terraform:${major}.${minor} -docker tag jmccann/drone-terraform:latest jmccann/drone-terraform:${major}.${minor}-${tf_ver} +docker tag moneysmartco/drone-terraform:latest moneysmartco/drone-terraform:${major} +docker tag moneysmartco/drone-terraform:latest moneysmartco/drone-terraform:${major}.${minor} +docker tag moneysmartco/drone-terraform:latest moneysmartco/drone-terraform:${major}.${minor}-${tf_ver} -docker push jmccann/drone-terraform:latest -docker push jmccann/drone-terraform:${major} -docker push jmccann/drone-terraform:${major}.${minor} -docker push jmccann/drone-terraform:${major}.${minor}-${tf_ver} +docker push moneysmartco/drone-terraform:latest +docker push moneysmartco/drone-terraform:${major} +docker push moneysmartco/drone-terraform:${major}.${minor} +docker push moneysmartco/drone-terraform:${major}.${minor}-${tf_ver} set +x \ No newline at end of file diff --git a/main.go b/main.go index ce00fba..e6deab1 100644 --- a/main.go +++ b/main.go @@ -103,6 +103,12 @@ func main() { Usage: "a list of var files to use. Each value is passed as -var-file=", EnvVar: "PLUGIN_VAR_FILES", }, + cli.StringFlag{ + Name: "deploy-env-path", + Usage: "Path to save the dotenv file", + EnvVar: "PLUGIN_DEPLOY_ENV_PATH", + Value: ".deploy.env", + }, } if err := app.Run(os.Args); err != nil { @@ -137,17 +143,18 @@ func run(c *cli.Context) error { plugin := Plugin{ Config: Config{ - Actions: c.StringSlice("actions"), - Vars: vars, - Secrets: secrets, - InitOptions: initOptions, - Cacert: c.String("ca_cert"), - Sensitive: c.Bool("sensitive"), - RoleARN: c.String("role_arn_to_assume"), - RootDir: c.String("root_dir"), - Parallelism: c.Int("parallelism"), - Targets: c.StringSlice("targets"), - VarFiles: c.StringSlice("var_files"), + Actions: c.StringSlice("actions"), + Vars: vars, + Secrets: secrets, + InitOptions: initOptions, + Cacert: c.String("ca_cert"), + Sensitive: c.Bool("sensitive"), + RoleARN: c.String("role_arn_to_assume"), + RootDir: c.String("root_dir"), + Parallelism: c.Int("parallelism"), + Targets: c.StringSlice("targets"), + VarFiles: c.StringSlice("var_files"), + DeployEnvPath: c.String("deploy-env-path"), }, Netrc: Netrc{ Login: c.String("netrc.username"), diff --git a/plugin.go b/plugin.go index 7887143..6020e94 100644 --- a/plugin.go +++ b/plugin.go @@ -16,22 +16,24 @@ import ( "github.com/aws/aws-sdk-go/aws/credentials/stscreds" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/sts" + "github.com/joho/godotenv" ) type ( // Config holds input parameters for the plugin Config struct { - Actions []string - Vars map[string]string - Secrets map[string]string - InitOptions InitOptions - Cacert string - Sensitive bool - RoleARN string - RootDir string - Parallelism int - Targets []string - VarFiles []string + Actions []string + Vars map[string]string + Secrets map[string]string + InitOptions InitOptions + Cacert string + Sensitive bool + RoleARN string + RootDir string + Parallelism int + Targets []string + VarFiles []string + DeployEnvPath string } // Netrc is credentials for cloning @@ -81,6 +83,7 @@ func (p Plugin) Exec() error { commands = append(commands, exec.Command("terraform", "version")) + p.CopyDeployEnv() CopyTfEnv() if p.Config.Cacert != "" { @@ -139,6 +142,18 @@ func (p Plugin) Exec() error { return nil } +// CopyDeployEnv export env var from DeployEnvPath file +func (p Plugin) CopyDeployEnv() { + envMap, err := godotenv.Read(p.Config.DeployEnvPath) + if err != nil { + return + } + + for k, v := range envMap { + os.Setenv(k, v) + } +} + // CopyTfEnv creates copies of TF_VAR_ to lowercase func CopyTfEnv() { tfVar := regexp.MustCompile(`^TF_VAR_.*$`)