Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add execution order group #2178

Merged
merged 2 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ lint: ## Run linter locally
golangci-lint run

check-lint: ## Run linter in CI/CD. If running locally use 'lint'
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b ./bin v1.39.0
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b ./bin v1.39.0
./bin/golangci-lint run -j 4 --timeout 5m

check-fmt: ## Fail if not formatted
Expand Down
21 changes: 17 additions & 4 deletions runatlantis.io/docs/repo-level-atlantis-yaml.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ projects:
to be allowed to set this key. See [Server-Side Repo Config Use Cases](server-side-repo-config.html#repos-can-set-their-own-apply-requirements).
:::

### Order of planning/applying
```yaml
version: 3
projects:
- dir: project1
execution_order_group: 2
- dir: project2
execution_order_group: 1
```
With this config above, Atlantis runs planning/applying for project2 first, then for project1.
Several projects can have same `execution_order_group`. Any order in one group isn't guaranteed.

### Custom Backend Config
See [Custom Workflow Use Cases: Custom Backend Config](custom-workflows.html#custom-backend-config)
Expand Down Expand Up @@ -259,8 +270,10 @@ Atlantis supports this but requires the `name` key to be specified. See [Custom
```yaml
enabled: true
when_modified: ["*.tf", "terragrunt.hcl"]
execution_order_group: 0
```
| Key | Type | Default | Required | Description |
|---------------|---------------|----------------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| enabled | boolean | `true` | no | Whether autoplanning is enabled for this project. |
| when_modified | array[string] | `["**/*.tf*"]` | no | Uses [.dockerignore](https://docs.docker.com/engine/reference/builder/#dockerignore-file) syntax. If any modified file in the pull request matches, this project will be planned. See [Autoplanning](autoplanning.html). Paths are relative to the project's dir. |
| Key | Type | Default | Required | Description |
|-----------------------|---------------|----------------|----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| enabled | boolean | `true` | no | Whether autoplanning is enabled for this project. |
| when_modified | array[string] | `["**/*.tf*"]` | no | Uses [.dockerignore](https://docs.docker.com/engine/reference/builder/#dockerignore-file) syntax. If any modified file in the pull request matches, this project will be planned. See [Autoplanning](autoplanning.html). Paths are relative to the project's dir. |
| execution_order_group | int | `0` | no | Index of execution order group. Projects will be sort by this field before planning/applying |
5 changes: 5 additions & 0 deletions server/core/config/raw/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Project struct {
Autoplan *Autoplan `yaml:"autoplan,omitempty"`
ApplyRequirements []string `yaml:"apply_requirements,omitempty"`
DeleteSourceBranchOnMerge *bool `yaml:"delete_source_branch_on_merge,omitempty"`
ExecutionOrderGroup *int `yaml:"execution_order_group,omitempty"`
}

func (p Project) Validate() error {
Expand Down Expand Up @@ -92,6 +93,10 @@ func (p Project) ToValid() valid.Project {
v.DeleteSourceBranchOnMerge = p.DeleteSourceBranchOnMerge
}

if p.ExecutionOrderGroup != nil {
v.ExecutionOrderGroup = *p.ExecutionOrderGroup
}

return v
}

Expand Down
16 changes: 10 additions & 6 deletions server/core/config/raw/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ autoplan:
when_modified: []
enabled: false
apply_requirements:
- mergeable`,
- mergeable
execution_order_group: 10`,
exp: raw.Project{
Name: String("myname"),
Dir: String("mydir"),
Expand All @@ -53,7 +54,8 @@ apply_requirements:
WhenModified: []string{},
Enabled: Bool(false),
},
ApplyRequirements: []string{"mergeable"},
ApplyRequirements: []string{"mergeable"},
ExecutionOrderGroup: Int(10),
},
},
}
Expand Down Expand Up @@ -281,8 +283,9 @@ func TestProject_ToValid(t *testing.T) {
WhenModified: []string{"hi"},
Enabled: Bool(false),
},
ApplyRequirements: []string{"approved"},
Name: String("myname"),
ApplyRequirements: []string{"approved"},
Name: String("myname"),
ExecutionOrderGroup: Int(10),
},
exp: valid.Project{
Dir: ".",
Expand All @@ -293,8 +296,9 @@ func TestProject_ToValid(t *testing.T) {
WhenModified: []string{"hi"},
Enabled: false,
},
ApplyRequirements: []string{"approved"},
Name: String("myname"),
ApplyRequirements: []string{"approved"},
Name: String("myname"),
ExecutionOrderGroup: 10,
},
},
{
Expand Down
2 changes: 2 additions & 0 deletions server/core/config/valid/global_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type MergedProjectCfg struct {
RepoCfgVersion int
PolicySets PolicySets
DeleteSourceBranchOnMerge bool
ExecutionOrderGroup int
}

// WorkflowHook is a map of custom run commands to run before or after workflows.
Expand Down Expand Up @@ -303,6 +304,7 @@ func (g GlobalCfg) MergeProjectCfg(log logging.SimpleLogging, repoID string, pro
RepoCfgVersion: rCfg.Version,
PolicySets: g.PolicySets,
DeleteSourceBranchOnMerge: deleteSourceBranchOnMerge,
ExecutionOrderGroup: proj.ExecutionOrderGroup,
}
}

Expand Down
30 changes: 30 additions & 0 deletions server/core/config/valid/global_cfg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,36 @@ repos:
PolicySets: emptyPolicySets,
},
},
"execution order group is set": {
gCfg: "",
repoID: "github.com/owner/repo",
proj: valid.Project{
Dir: "mydir",
Workspace: "myworkspace",
Name: String("myname"),
Autoplan: valid.Autoplan{
WhenModified: []string{".tf"},
Enabled: true,
},
ExecutionOrderGroup: 10,
},
repoWorkflows: nil,
exp: valid.MergedProjectCfg{
ApplyRequirements: []string{},
Workflow: valid.Workflow{
Name: "default",
Apply: valid.DefaultApplyStage,
PolicyCheck: valid.DefaultPolicyCheckStage,
Plan: valid.DefaultPlanStage,
},
RepoRelDir: "mydir",
Workspace: "myworkspace",
Name: "myname",
AutoplanEnabled: true,
PolicySets: emptyPolicySets,
ExecutionOrderGroup: 10,
},
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions server/core/config/valid/repo_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ type Project struct {
Autoplan Autoplan
ApplyRequirements []string
DeleteSourceBranchOnMerge *bool
ExecutionOrderGroup int
}

// GetName returns the name of the project or an empty string if there is no
Expand Down
2 changes: 2 additions & 0 deletions server/events/command/project_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ type ProjectContext struct {
DeleteSourceBranchOnMerge bool
// UUID for atlantis logs
JobID string
// The index of order group. Before planning/applying it will use to sort projects. Default is 0.
ExecutionOrderGroup int
}

// SetScope sets the scope of the stats object field. Note: we deliberately set this on the value
Expand Down
10 changes: 10 additions & 0 deletions server/events/project_command_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package events
import (
"fmt"
"os"
"sort"

"github.com/runatlantis/atlantis/server/core/config/valid"
"github.com/runatlantis/atlantis/server/logging"
Expand Down Expand Up @@ -329,6 +330,10 @@ func (p *DefaultProjectCommandBuilder) buildPlanAllCommands(ctx *command.Context
}
}

sort.Slice(projCtxs, func(i, j int) bool {
return projCtxs[i].ExecutionOrderGroup < projCtxs[j].ExecutionOrderGroup
})

return projCtxs, nil
}

Expand Down Expand Up @@ -466,6 +471,11 @@ func (p *DefaultProjectCommandBuilder) buildAllProjectCommands(ctx *command.Cont
}
cmds = append(cmds, commentCmds...)
}

sort.Slice(cmds, func(i, j int) bool {
return cmds[i].ExecutionOrderGroup < cmds[j].ExecutionOrderGroup
})

return cmds, nil
}

Expand Down
1 change: 1 addition & 0 deletions server/events/project_command_context_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func newProjectCommandContext(ctx *command.Context,
PolicySets: policySets,
PullReqStatus: pullStatus,
JobID: uuid.New().String(),
ExecutionOrderGroup: projCfg.ExecutionOrderGroup,
}
}

Expand Down