Skip to content

Commit

Permalink
feat: add ability to track git untracked files (runatlantis#3724)
Browse files Browse the repository at this point in the history
  • Loading branch information
X-Guardian authored and terakoya76 committed Dec 31, 2024
1 parent 29d0503 commit 4f06248
Show file tree
Hide file tree
Showing 13 changed files with 654 additions and 153 deletions.
5 changes: 5 additions & 0 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ const (
GitlabTokenFlag = "gitlab-token"
GitlabUserFlag = "gitlab-user"
GitlabWebhookSecretFlag = "gitlab-webhook-secret" // nolint: gosec
IncludeGitUntrackedFiles = "include-git-untracked-files"
APISecretFlag = "api-secret"
HidePrevPlanComments = "hide-prev-plan-comments"
QuietPolicyChecks = "quiet-policy-checks"
Expand Down Expand Up @@ -475,6 +476,10 @@ var boolFlags = map[string]boolFlag{
"VCS support is limited to: GitHub.",
defaultValue: false,
},
IncludeGitUntrackedFiles: {
description: "Include git untracked files in the Atlantis modified file scope.",
defaultValue: false,
},
ParallelPlanFlag: {
description: "Run plan operations in parallel.",
defaultValue: false,
Expand Down
52 changes: 31 additions & 21 deletions runatlantis.io/docs/custom-workflows.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,17 @@ workflows:
- run: terraform apply $PLANFILE
```

### cdktf
Here are the requirements to enable [cdktf](https://developer.hashicorp.com/terraform/cdktf)

- A custom image with `cdktf` installed
- The autoplan file updated to trigger off of `**/cdk.tf.json`
- The output of `cdktf synth` has to be committed to the pull request
- Optional: Use `pre_workflow_hooks` to run `cdktf synth` as a double check
### CDKTF
Here are the requirements to enable [CDKTF](https://developer.hashicorp.com/terraform/cdktf)

- A custom image with `CDKTF` installed
- Add `**/cdk.tf.json` to the list of Atlantis autoplan files.
- Set the `atlantis-include-git-untracked-files` flag so that the Terraform files dynamically generated
by CDKTF will be add to the Atlantis modified file list.
- Use `pre_workflow_hooks` to run `cdktf synth`
- Optional: There isn't a requirement to use a repo `atlantis.yaml` but one can be leveraged if needed.

#### custom image
#### Custom Image

```dockerfile
# Dockerfile
Expand All @@ -179,11 +180,12 @@ FROM ghcr.io/runatlantis/atlantis:v0.19.7
RUN apk add npm && npm i -g cdktf-cli
```

#### server config
#### Server Config

```bash
# env variables
ATLANTIS_AUTOPLAN_FILE_LIST="**/*.tf,**/*.tfvars,**/*.tfvars.json,**/cdk.tf.json"
ATLANTIS_INCLUDE_GIT_UNTRACKED_FILES=true
```

OR
Expand All @@ -192,9 +194,10 @@ OR
```yaml
# config.yaml
autoplan-file-list: "**/*.tf,**/*.tfvars,**/*.tfvars.json,**/cdk.tf.json"
include-git-untracked-files: true
```

#### server repo config
#### Server Repo Config

Use `pre_workflow_hooks`

Expand All @@ -204,32 +207,39 @@ Use `pre_workflow_hooks`
repos:
- id: /.*cdktf.*/
pre_workflow_hooks:
- run: npm i && cdktf get && cdktf synth
- run: npm i && cdktf get && cdktf synth --output ci-cdktf.out
```

#### repo structure
**Note:** don't use the default `cdktf.out` directory that CDKTF uses, as this should be in the `.gitignore` list of the
repo, so that locally generated files are not checked in.

#### Repo Structure

This is the git repo structure after running `cdktf synth`. The `cdk.tf.json` files contain the HCL that atlantis can run.
This is the git repo structure after running `cdktf synth`. The `cdk.tf.json` files contain the Terraform configuration
that atlantis can run.

```bash
$ tree --gitignore
.
├── cdktf.json
├── cdktf.out
├── ci-cdktf.out
│ ├── manifest.json
│ └── stacks
│ └── eks
│ └── cdk.tf.json
```

#### workflow
#### Workflow

1. Container orchestrator (k8s/fargate/ecs/etc) uses the custom docker image of atlantis with `cdktf` installed with the `--autoplan-file-list` to trigger on json files
1. PR branch is pushed up containing `cdktf` changes and generated hcl json
1. Atlantis checks out the branch in the repo
1. Atlantis runs the `npm i && cdktf get && cdktf synth` command in the repo root as a step in `pre_workflow_hooks` (as a double check described above)
1. Atlantis detects the change to the generated hcl json files in a number of `dir`s
1. Atlantis then runs `terraform` workflows in the respective `dir`s as usual
1. Container orchestrator (k8s/fargate/ecs/etc) uses the custom docker image of atlantis with `cdktf` installed with
the `--autoplan-file-list` to trigger on `cdk.tf.json` files and `--include-git-untracked-files` set to include the
CDKTF dynamically generated Terraform files in the Atlantis plan.
1. PR branch is pushed up containing `cdktf` code changes.
1. Atlantis checks out the branch in the repo.
1. Atlantis runs the `npm i && cdktf get && cdktf synth` command in the repo root as a step in `pre_workflow_hooks`,
generating the `cdk.tf.json` Terraform files.
1. Atlantis detects the `cdk.tf.json` untracked files in a number of directories.
1. Atlantis then runs `terraform` workflows in the respective directories as usual.

### Terragrunt
Atlantis supports running custom commands in place of the default Atlantis
Expand Down
10 changes: 10 additions & 0 deletions runatlantis.io/docs/server-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,16 @@ This is useful when you have many projects and want to keep the pull request cle
Hide previous plan comments to declutter PRs. This is only supported in
GitHub and GitLab currently. This is not enabled by default.

### `--include-git-untracked-files`
```bash
atlantis server --include-git-untracked-files
# or
ATLANTIS_INCLUDE_GIT_UNTRACKED_FILES=true
```
Include git untracked files in the Atlantis modified file list.
Used for example with CDKTF pre-workflow hooks that dynamically generate
Terraform files.

### `--locking-db-type`
```bash
atlantis server --locking-db-type="<boltdb|redis>"
Expand Down
1 change: 1 addition & 0 deletions server/controllers/events/events_controller_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1323,6 +1323,7 @@ func setupE2E(t *testing.T, repoDir string, opt setupOption) (events_controllers
"**/*.tf,**/*.tfvars,**/*.tfvars.json,**/terragrunt.hcl,**/.terraform.lock.hcl",
false,
false,
false,
statsScope,
logger,
terraformClient,
Expand Down
54 changes: 54 additions & 0 deletions server/events/mock_workingdir_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions server/events/mocks/mock_working_dir.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4f06248

Please sign in to comment.