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

Terraform: install modules when updating lockfile #3968

Merged
merged 1 commit into from
Jun 22, 2021
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
10 changes: 9 additions & 1 deletion terraform/lib/dependabot/terraform/file_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def update_registry_declaration(new_req, old_req, updated_content)
end
end

def update_lockfile_declaration
def update_lockfile_declaration # rubocop:disable Metrics/AbcSize
return if lock_file.nil?

new_req = dependency.requirements.first
Expand All @@ -115,6 +115,14 @@ def update_lockfile_declaration
content.scan(declaration_regex).first.scan(/^\s*version\s*=.*/)
content.sub!(declaration_regex, updated_dependency)
end
rescue SharedHelpers::HelperSubprocessFailed => e
raise if @retrying_lock || !e.message.include?("terraform init")

# NOTE: Modules need to be installed before terraform can update the
# lockfile
@retrying_lock = true
SharedHelpers.run_shell_command("terraform init")
retry
end

content
Expand Down
46 changes: 46 additions & 0 deletions terraform/spec/dependabot/terraform/file_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -838,5 +838,51 @@ module "github_terraform" {
)
end
end

describe "with a lockfile and modules that need to be installed" do
let(:files) { project_dependency_files("lockfile_with_modules") }
let(:dependencies) do
[
Dependabot::Dependency.new(
name: "integrations/github",
version: "4.12.0",
previous_version: "4.4.0",
requirements: [{
requirement: "4.12.0",
groups: [],
file: "main.tf",
source: {
type: "registry",
registry_hostname: "registry.terraform.io",
module_identifier: "integrations/github"
}
}],
previous_requirements: [{
requirement: "4.4.0",
groups: [],
file: "main.tf",
source: {
type: "registry",
registry_hostname: "registry.terraform.io",
module_identifier: "integrations/github"
}
}],
package_manager: "terraform"
)
]
end

it "updates the version in the lockfile" do
lockfile = subject.find { |file| file.name == ".terraform.lock.hcl" }

expect(lockfile.content).to include(
<<~DEP
provider "registry.terraform.io/integrations/github" {
version = "4.12.0"
constraints = "~> 4.4"
DEP
)
end
end
end
end

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module "caf" {
source = "aztfmod/caf/azurerm"
version = "5.1.0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_providers {
github = {
source = "integrations/github"
version = "~> 4.4"
}
}
required_version = ">= 0.14"
}