Browse Source

Export deployEnvPath as TF_VAR_*

pull/76/head
Eric Ho 6 years ago
parent
commit
7730e57b58
No known key found for this signature in database GPG Key ID: 4397AE5628A156C
  1. 16
      build-docker.sh
  2. 29
      main.go
  3. 37
      plugin.go

16
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}') 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 set -x
docker tag jmccann/drone-terraform:latest jmccann/drone-terraform:${major} docker tag moneysmartco/drone-terraform:latest moneysmartco/drone-terraform:${major}
docker tag jmccann/drone-terraform:latest jmccann/drone-terraform:${major}.${minor} docker tag moneysmartco/drone-terraform:latest moneysmartco/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}.${minor}-${tf_ver}
docker push jmccann/drone-terraform:latest docker push moneysmartco/drone-terraform:latest
docker push jmccann/drone-terraform:${major} docker push moneysmartco/drone-terraform:${major}
docker push jmccann/drone-terraform:${major}.${minor} docker push moneysmartco/drone-terraform:${major}.${minor}
docker push jmccann/drone-terraform:${major}.${minor}-${tf_ver} docker push moneysmartco/drone-terraform:${major}.${minor}-${tf_ver}
set +x set +x

29
main.go

@ -103,6 +103,12 @@ func main() {
Usage: "a list of var files to use. Each value is passed as -var-file=<value>", Usage: "a list of var files to use. Each value is passed as -var-file=<value>",
EnvVar: "PLUGIN_VAR_FILES", 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 { if err := app.Run(os.Args); err != nil {
@ -137,17 +143,18 @@ func run(c *cli.Context) error {
plugin := Plugin{ plugin := Plugin{
Config: Config{ Config: Config{
Actions: c.StringSlice("actions"), Actions: c.StringSlice("actions"),
Vars: vars, Vars: vars,
Secrets: secrets, Secrets: secrets,
InitOptions: initOptions, InitOptions: initOptions,
Cacert: c.String("ca_cert"), Cacert: c.String("ca_cert"),
Sensitive: c.Bool("sensitive"), Sensitive: c.Bool("sensitive"),
RoleARN: c.String("role_arn_to_assume"), RoleARN: c.String("role_arn_to_assume"),
RootDir: c.String("root_dir"), RootDir: c.String("root_dir"),
Parallelism: c.Int("parallelism"), Parallelism: c.Int("parallelism"),
Targets: c.StringSlice("targets"), Targets: c.StringSlice("targets"),
VarFiles: c.StringSlice("var_files"), VarFiles: c.StringSlice("var_files"),
DeployEnvPath: c.String("deploy-env-path"),
}, },
Netrc: Netrc{ Netrc: Netrc{
Login: c.String("netrc.username"), Login: c.String("netrc.username"),

37
plugin.go

@ -16,22 +16,24 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials/stscreds" "github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts" "github.com/aws/aws-sdk-go/service/sts"
"github.com/joho/godotenv"
) )
type ( type (
// Config holds input parameters for the plugin // Config holds input parameters for the plugin
Config struct { Config struct {
Actions []string Actions []string
Vars map[string]string Vars map[string]string
Secrets map[string]string Secrets map[string]string
InitOptions InitOptions InitOptions InitOptions
Cacert string Cacert string
Sensitive bool Sensitive bool
RoleARN string RoleARN string
RootDir string RootDir string
Parallelism int Parallelism int
Targets []string Targets []string
VarFiles []string VarFiles []string
DeployEnvPath string
} }
// Netrc is credentials for cloning // Netrc is credentials for cloning
@ -81,6 +83,7 @@ func (p Plugin) Exec() error {
commands = append(commands, exec.Command("terraform", "version")) commands = append(commands, exec.Command("terraform", "version"))
p.CopyDeployEnv()
CopyTfEnv() CopyTfEnv()
if p.Config.Cacert != "" { if p.Config.Cacert != "" {
@ -139,6 +142,18 @@ func (p Plugin) Exec() error {
return nil 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 // CopyTfEnv creates copies of TF_VAR_ to lowercase
func CopyTfEnv() { func CopyTfEnv() {
tfVar := regexp.MustCompile(`^TF_VAR_.*$`) tfVar := regexp.MustCompile(`^TF_VAR_.*$`)

Loading…
Cancel
Save