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

Add an example for updating .terraform.lock.hcl #113

Merged
merged 7 commits into from
Dec 4, 2020
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
31 changes: 31 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ commands:
provider_name:
description: A name of provider
type: string
terraform_version:
description: A version of terraform command for updating a lock file
type: string
default: latest
steps:
- run:
name: Update terraform-provider-<< parameters.provider_name >> to latest
Expand All @@ -87,6 +91,32 @@ commands:
else
git checkout -b update-terraform-provider-<< parameters.provider_name >>-to-v${VERSION} origin/<< parameters.base_branch >>
tfupdate provider << parameters.provider_name >> -v ${VERSION} << parameters.args >>

# install terraform
TERRAFORM_VERSION=<< parameters.terraform_version >>
if [ "${TERRAFORM_VERSION}" = "latest" ]; then
TERRAFORM_VERSION=$(tfupdate release latest hashicorp/terraform)
fi
wget -qO- https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip | unzip -d /bin - && chmod +x /bin/terraform
terraform version

# create a local filesystem mirror to avoid duplicate downloads
FS_MIRROR="/tmp/terraform.d/plugins"
terraform providers mirror -platform=linux_amd64 -platform=darwin_amd64 "${FS_MIRROR}"

# update the lock file
ALL_DIRS=$(find . -type f -name '*.tf' | xargs -I {} dirname {} | grep -v 'modules/')
for dir in ${ALL_DIRS}
do
pushd "$dir"
# always create a new lock to avoid duplicate downloads by terraoform init -upgrade
rm -f .terraform.lock.hcl
# generate h1 hashes for all platforms you need
# recording zh hashes requires to download from origin, so we intentionally ignore them.
terraform providers lock -fs-mirror="${FS_MIRROR}" -platform=linux_amd64 -platform=darwin_amd64
popd
done

if git add . && git diff --cached --exit-code --quiet; then
echo "No changes"
else
Expand All @@ -108,6 +138,7 @@ jobs:
- tfupdate_terraform
- tfupdate_provider:
provider_name: 'aws'
terraform_version: '0.14.0'

workflows:
version: 2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.terraform/
20 changes: 20 additions & 0 deletions .terraform.lock.hcl

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

10 changes: 10 additions & 0 deletions dev/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = "0.14.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = "3.15.0"
}
}
}
2 changes: 1 addition & 1 deletion main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = "0.14.0-beta2"
required_version = "0.14.0"

required_providers {
aws = {
Expand Down
10 changes: 10 additions & 0 deletions prod/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
terraform {
required_version = "0.14.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = "3.15.0"
}
}
}