From db0e0636af8bf59a1031f296de344a1fa39f2e31 Mon Sep 17 00:00:00 2001 From: David Mattia Date: Sat, 26 Feb 2022 12:19:19 -0600 Subject: [PATCH] Support incremental builds (#207) --- Makefile | 2 +- README.md | 5 +-- cmd/generate.go | 3 ++ cmd/generate_test.go | 48 ++++++++++++++++++++++++++++ cmd/golden/oldProjectsPreserved.yaml | 18 +++++++++++ main.go | 2 +- 6 files changed, 74 insertions(+), 4 deletions(-) create mode 100644 cmd/golden/oldProjectsPreserved.yaml diff --git a/Makefile b/Makefile index 898018df..80f3dbf9 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -VERSION=1.12.5 +VERSION=1.13.0 PATH_BUILD=build/ FILE_COMMAND=terragrunt-atlantis-config FILE_ARCH=darwin_amd64 diff --git a/README.md b/README.md index 55055b57..ffb392f7 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Then, make sure `terragrunt-atlantis-config` is present on your Atlantis server. ```hcl variable "terragrunt_atlantis_config_version" { - default = "1.12.5" + default = "1.13.0" } build { @@ -122,6 +122,7 @@ One way to customize the behavior of this module is through CLI flag values pass | `--create-workspace` | Use different auto-generated workspace for each project. Default is use default workspace for everything | false | | `--create-project-name` | Add different auto-generated name for each project | false | | `--preserve-workflows` | Preserves workflows from old output files. Useful if you want to define your workflow definitions on the client side | true | +| `--preserve-projects` | Preserves projects from old output files. Useful for incremental builds using `--filter` | false | | `--workflow` | Name of the workflow to be customized in the atlantis server. If empty, will be left out of output | "" | | `--apply-requirements` | Requirements that must be satisfied before `atlantis apply` can be run. Currently the only supported requirements are `approved` and `mergeable`. Can be overridden by locals | [] | | `--output` | Path of the file where configuration will be generated. Typically, you want a file named "atlantis.yaml". Default is to write to `stdout`. | "" | @@ -189,7 +190,7 @@ You can install this tool locally to checkout what kinds of config it will gener Recommended: Install any version via go get: ```bash -cd && GO111MODULE=on go get github.com/transcend-io/terragrunt-atlantis-config@v1.12.5 && cd - +cd && GO111MODULE=on go get github.com/transcend-io/terragrunt-atlantis-config@v1.13.0 && cd - ``` This module officially supports golang versions v1.13, v1.14, v1.15, and v1.16, tested on CircleCI with each build diff --git a/cmd/generate.go b/cmd/generate.go index ecf5744f..d04b82f2 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -669,6 +669,9 @@ func main(cmd *cobra.Command, args []string) error { if oldConfig != nil && preserveWorkflows { config.Workflows = oldConfig.Workflows } + if oldConfig != nil && preserveProjects { + config.Projects = oldConfig.Projects + } lock := sync.Mutex{} ctx := context.Background() diff --git a/cmd/generate_test.go b/cmd/generate_test.go index 266fceed..708a3b48 100644 --- a/cmd/generate_test.go +++ b/cmd/generate_test.go @@ -34,6 +34,7 @@ func resetForRun() error { createWorkspace = false createProjectName = false preserveWorkflows = true + preserveProjects = true defaultWorkflow = "" filterPath = "" outputPath = "" @@ -355,6 +356,53 @@ func TestPreservingOldWorkflows(t *testing.T) { } } +func TestPreservingOldProjects(t *testing.T) { + err := resetForRun() + if err != nil { + t.Error("Failed to reset default flags") + return + } + + randomInt := rand.Int() + filename := filepath.Join("test_artifacts", fmt.Sprintf("%d.yaml", randomInt)) + defer os.Remove(filename) + + // Create an existing file to simulate an existing atlantis.yaml file + contents := []byte(`projects: +- autoplan: + enabled: false + when_modified: + - '*.hcl' + - '*.tf*' + dir: someDir + name: projectFromPreviousRun +`) + ioutil.WriteFile(filename, contents, 0644) + + content, err := RunWithFlags(filename, []string{ + "generate", + "--preserve-projects", + "--output", + filename, + "--root", + filepath.Join("..", "test_examples", "basic_module"), + }) + if err != nil { + t.Error("Failed to read file") + return + } + + goldenContents, err := ioutil.ReadFile(filepath.Join("golden", "oldProjectsPreserved.yaml")) + if err != nil { + t.Error("Failed to read golden file") + return + } + + if string(content) != string(goldenContents) { + t.Errorf("Content did not match golden file.\n\nExpected Content: %s\n\nContent: %s", string(goldenContents), string(content)) + } +} + func TestEnablingAutomerge(t *testing.T) { runTest(t, filepath.Join("golden", "withAutomerge.yaml"), []string{ "--root", diff --git a/cmd/golden/oldProjectsPreserved.yaml b/cmd/golden/oldProjectsPreserved.yaml new file mode 100644 index 00000000..7ddf7be9 --- /dev/null +++ b/cmd/golden/oldProjectsPreserved.yaml @@ -0,0 +1,18 @@ +automerge: false +parallel_apply: true +parallel_plan: true +projects: +- autoplan: + enabled: false + when_modified: + - '*.hcl' + - '*.tf*' + dir: . +- autoplan: + enabled: false + when_modified: + - '*.hcl' + - '*.tf*' + dir: someDir + name: projectFromPreviousRun +version: 3 diff --git a/main.go b/main.go index 8e152cb7..218c4afc 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,7 @@ import "github.com/transcend-io/terragrunt-atlantis-config/cmd" // This variable is set at build time using -ldflags parameters. // But we still set a default here for those using plain `go get` downloads // For more info, see: http://stackoverflow.com/a/11355611/483528 -var VERSION string = "1.12.5" +var VERSION string = "1.13.0" func main() { cmd.Execute(VERSION)