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

fix: auto plan when .terraform.lock.hcl changed #3473

Merged

Conversation

shouichi
Copy link
Contributor

@shouichi shouichi commented Jun 2, 2023

what

When projects are specified and when_modified is not specified via
the config file, changing .terraform.lock.hcl file won't trigger auto
plan. This is because the default raw.WhenModified does not include
.terraform.lock.hcl.

Note that when projects are auto detected, changing
.terraform.lock.hcl triggers auto plan. This is because the
cmd.DefaultAutoplanFileList includes .terraform.lock.hcl.

why

It is desirable to run terraform plan/apply even if only .terraform.lock.hcl are updated.

tests

I added a unit test and also tested this behavior by running atlantis with this change.

references

See also #3466.

@shouichi shouichi marked this pull request as ready for review June 2, 2023 10:14
@shouichi shouichi requested a review from a team as a code owner June 2, 2023 10:14
@github-actions github-actions bot added the go Pull requests that update Go code label Jun 2, 2023
@jamengual
Copy link
Contributor

This can be done in the default or custom workflow by changing the autoplan triggered files or https://www.runatlantis.io/docs/server-configuration.html#autoplan-file-list, why to change it in atlantis code?

@shouichi
Copy link
Contributor Author

shouichi commented Jun 2, 2023

Thank you, I didn't know that feature. But I think it's a good default. I was surprised that Atlantis didn't run auto plan when the lock file was changed.

@jamengual
Copy link
Contributor

This default does not work for a lot of people that do not commit the lock file, that is the problem.

@nitrocode
Copy link
Member

Tests are failing and no new tests have been added in this pr for coverage.

What cases would someone only update the lock file and not the hcl files themselves?

@shouichi
Copy link
Contributor Author

shouichi commented Jun 5, 2023

I usually set relatively relaxed version constraints on HCL and exact versions are set by .terraform.lock.hcl.

# version.tf
terraform {
  required_providers {
    random = {
      source  = "hashicorp/random"
      version = "~> 3.0"
    }
  }
}

# .terraform.lock.hcl
provider "registry.terraform.io/hashicorp/random" {
  version     = "3.5.1"
  constraints = "~> 3.0"
  hashes = [...]
}

When I update provider versions by terraform init -upgrade only .terraform.lock.hcl is updated. In this scenario, I want to run terraform apply to minimize the risk of the provider version update. I think it's a good practice and it would be great if atlantis runs the auto-plan without configuring it.

If this makes sense, I'd like to fix/add tests to cover the new scenario where only .terraform.lock.hcl is modified. If it doesn't, feel free to close this. Thank you.

@shouichi shouichi force-pushed the add-lock-file-to-default-when-modified branch 2 times, most recently from 227b1ef to 7354064 Compare June 5, 2023 02:20
@nitrocode
Copy link
Member

To me, your change makes sense. Thank you for contributing. My only issue is the lack of tests.

cc: @jamengual @GenPage thoughts?

@jamengual
Copy link
Contributor

jamengual commented Jun 5, 2023

it is fine with me as long as there are tests.

@nitrocode nitrocode added the needs tests Change requires tests label Jun 5, 2023
@shouichi
Copy link
Contributor Author

shouichi commented Jun 6, 2023

Could you help me to write tests? I found

{
Description: "Modified .terraform.lock.hcl triggers autoplan ",
RepoDir: "simple-with-lockfile",
ModifiedFiles: []string{".terraform.lock.hcl"},
ExpAutoplan: true,
Comments: []string{
"atlantis plan",
},
ExpReplies: [][]string{
{"exp-output-autoplan.txt"},
{"exp-output-plan.txt"},
},
LockFileTracked: true,
},
and it seems like this already tests if modifying .terraform.lock.hcl runs the auto-plan. But atlantis v0.24 won't run auto-plan when only .terraform.lock.hcl is modified. Am I missing something?

@shouichi shouichi force-pushed the add-lock-file-to-default-when-modified branch from 7354064 to 92fc89b Compare July 5, 2023 09:13
@shouichi shouichi changed the title feat: auto plan when .terraform.lock.hcl changed fix: auto plan when .terraform.lock.hcl changed Jul 5, 2023
@shouichi shouichi force-pushed the add-lock-file-to-default-when-modified branch 2 times, most recently from e6a51d5 to 7c72247 Compare July 5, 2023 09:43
@github-actions github-actions bot added github-actions and removed go Pull requests that update Go code labels Jul 5, 2023
@shouichi shouichi force-pushed the add-lock-file-to-default-when-modified branch from 46fa8fe to 7c72247 Compare July 5, 2023 09:59
@github-actions github-actions bot added go Pull requests that update Go code and removed github-actions labels Jul 5, 2023
@shouichi shouichi force-pushed the add-lock-file-to-default-when-modified branch from 5036dd3 to c151c7d Compare July 5, 2023 10:09
@github-actions github-actions bot added the docs Documentation label Jul 5, 2023
@shouichi shouichi force-pushed the add-lock-file-to-default-when-modified branch from c151c7d to 7b7289f Compare July 5, 2023 10:11
@shouichi
Copy link
Contributor Author

shouichi commented Jul 5, 2023

@nitrocode I added a unit test and updated the PR description. Can you take a look?

Problem

When `projects` are specified and `when_modified` is not specified via
the config file, changing `.terraform.lock.hcl` file won't trigger auto
plan. This is because the default `raw.WhenModified` does not include
`.terraform.lock.hcl`.

Note that when projects are auto detected, changing
`.terraform.lock.hcl` triggers auto plan. This is because the
`cmd.DefaultAutoplanFileList` includes `.terraform.lock.hcl`.

Solution

Include `.terraform.lock.hcl` to the default `raw.WhenModified`.
@GenPage GenPage force-pushed the add-lock-file-to-default-when-modified branch from 7b7289f to b1e3183 Compare July 31, 2023 13:22
@GenPage GenPage merged commit 2bd778b into runatlantis:main Jul 31, 2023
14 checks passed
@GenPage
Copy link
Member

GenPage commented Jul 31, 2023

Thank you for the contribution @shouichi

ijames-gc pushed a commit to gocardless/atlantis that referenced this pull request Feb 13, 2024
Problem

When `projects` are specified and `when_modified` is not specified via
the config file, changing `.terraform.lock.hcl` file won't trigger auto
plan. This is because the default `raw.WhenModified` does not include
`.terraform.lock.hcl`.

Note that when projects are auto detected, changing
`.terraform.lock.hcl` triggers auto plan. This is because the
`cmd.DefaultAutoplanFileList` includes `.terraform.lock.hcl`.

Solution

Include `.terraform.lock.hcl` to the default `raw.WhenModified`.
ijames-gc pushed a commit to gocardless/atlantis that referenced this pull request Feb 13, 2024
Problem

When `projects` are specified and `when_modified` is not specified via
the config file, changing `.terraform.lock.hcl` file won't trigger auto
plan. This is because the default `raw.WhenModified` does not include
`.terraform.lock.hcl`.

Note that when projects are auto detected, changing
`.terraform.lock.hcl` triggers auto plan. This is because the
`cmd.DefaultAutoplanFileList` includes `.terraform.lock.hcl`.

Solution

Include `.terraform.lock.hcl` to the default `raw.WhenModified`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation go Pull requests that update Go code needs tests Change requires tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants