From 7754458949341e2934f4fcd53b9fd68e612cef1f Mon Sep 17 00:00:00 2001 From: Loic PORTE Date: Wed, 15 Jan 2020 15:00:39 +0100 Subject: [PATCH] Add fail on change for terraform plan --- README.md | 21 ++------------------- go.mod | 2 ++ plugin.go | 14 ++++++++++---- 3 files changed, 14 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 01ffed7..a6a237c 100644 --- a/README.md +++ b/README.md @@ -16,27 +16,10 @@ go test go build ``` -## Docker - -Build the docker image with the following commands: - -``` -docker build --rm=true \ - -t jmccann/drone-terraform \ - --build-arg terraform_version=0.12.0 . -``` - ## Usage Execute from the working directory: +```bash +$ PLUGIN_ACTIONS=plan-fail-on-modification ./drone-terraform ``` -docker run --rm \ - -v $(pwd):$(pwd) \ - -w $(pwd) \ - jmccann/drone-terraform:latest --plan -``` - -## Drone 0.4 - -Legacy `drone-terraform` plugin exists @ `jmccann/drone-terraform:0.4` diff --git a/go.mod b/go.mod index 516bad3..fe13ed4 100644 --- a/go.mod +++ b/go.mod @@ -10,3 +10,5 @@ require ( github.com/urfave/cli v0.0.0-20161006035353-55f715e28c46 golang.org/x/sys v0.0.0-20161006025142-8d1157a43547 ) + +go 1.13 diff --git a/plugin.go b/plugin.go index 4afe66d..fa68422 100644 --- a/plugin.go +++ b/plugin.go @@ -115,15 +115,17 @@ func (p Plugin) Exec() error { case "validate": commands = append(commands, tfValidate()) case "plan": - commands = append(commands, tfPlan(p.Config, false)) + commands = append(commands, tfPlan(p.Config, false, false)) case "plan-destroy": - commands = append(commands, tfPlan(p.Config, true)) + commands = append(commands, tfPlan(p.Config, true, false)) + case "plan-fail-on-modification": + commands = append(commands, tfPlan(p.Config, false, true)) case "apply": commands = append(commands, tfApply(p.Config)) case "destroy": commands = append(commands, tfDestroy(p.Config)) default: - return fmt.Errorf("valid actions are: fmt, validate, plan, apply, plan-destroy, destroy. You provided %s", action) + return fmt.Errorf("valid actions are: fmt, validate, plan, apply, plan-destroy, plan-fail-on-modification destroy. You provided %s", action) } } @@ -293,7 +295,7 @@ func tfDestroy(config Config) *exec.Cmd { ) } -func tfPlan(config Config, destroy bool) *exec.Cmd { +func tfPlan(config Config, destroy bool, detailedExitCode bool) *exec.Cmd { args := []string{ "plan", } @@ -304,6 +306,10 @@ func tfPlan(config Config, destroy bool) *exec.Cmd { args = append(args, fmt.Sprintf("-out=%s", getTfoutPath())) } + if detailedExitCode { + args = append(args, "-detailed-exitcode") + } + for _, v := range config.Targets { args = append(args, "--target", fmt.Sprintf("%s", v)) }