Skip to content

Commit

Permalink
feat: Add ability to customize terraform version & upgrade to 0.12.24 (
Browse files Browse the repository at this point in the history
  • Loading branch information
rjerrems authored and morgante committed Apr 15, 2020
1 parent 3fb6d5d commit 1f82423
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 8 deletions.
2 changes: 2 additions & 0 deletions modules/cloudbuild/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Functional examples and sample Cloud Build definitions are included in the [exam
| terraform\_sa\_email | Email for terraform service account. | string | n/a | yes |
| terraform\_sa\_name | Fully-qualified name of the terraform service account. | string | n/a | yes |
| terraform\_state\_bucket | Default state bucket, used in Cloud Build substitutions. | string | n/a | yes |
| terraform\_version | Default terraform version. | string | `"0.12.24"` | no |
| terraform\_version\_sha256sum | sha256sum for default terraform version. | string | `"602d2529aafdaa0f605c06adb7c72cfb585d8aa19b3f4d8d189b42589e27bf11"` | no |

## Outputs

Expand Down
11 changes: 7 additions & 4 deletions modules/cloudbuild/cloudbuild_builder/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@

FROM gcr.io/cloud-builders/gcloud-slim

ENV TERRAFORM_VERSION=0.12.13
ENV TERRAFORM_VERSION_SHA256SUM=63f765a3f83987b67b046a9c31acff1ec9ee618990d0eab4db34eca6c0d861ec
ARG TERRAFORM_VERSION=0.12.24
ARG TERRAFORM_VERSION_SHA256SUM=602d2529aafdaa0f605c06adb7c72cfb585d8aa19b3f4d8d189b42589e27bf11

ENV ENV_TERRAFORM_VERSION=$TERRAFORM_VERSION
ENV ENV_TERRAFORM_VERSION_SHA256SUM=$TERRAFORM_VERSION_SHA256SUM

RUN apt-get update && \
/builder/google-cloud-sdk/bin/gcloud -q components install alpha beta && \
apt-get -y install curl jq unzip ca-certificates && \
curl https://releases.hashicorp.com/terraform/${TERRAFORM_VERSION}/terraform_${TERRAFORM_VERSION}_linux_amd64.zip \
curl https://releases.hashicorp.com/terraform/${ENV_TERRAFORM_VERSION}/terraform_${ENV_TERRAFORM_VERSION}_linux_amd64.zip \
> terraform_linux_amd64.zip && \
echo "${TERRAFORM_VERSION_SHA256SUM} terraform_linux_amd64.zip" > terraform_SHA256SUMS && \
echo "${ENV_TERRAFORM_VERSION_SHA256SUM} terraform_linux_amd64.zip" > terraform_SHA256SUMS && \
sha256sum -c terraform_SHA256SUMS --status && \
unzip terraform_linux_amd64.zip -d /builder/terraform && \
rm -f terraform_linux_amd64.zip && \
Expand Down
11 changes: 10 additions & 1 deletion modules/cloudbuild/cloudbuild_builder/cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@
# $ gcloud builds submit . --config=cloudbuild.yaml
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '--tag=gcr.io/${PROJECT_ID}/terraform', '.']
args: [
'build',
'--tag=gcr.io/${PROJECT_ID}/terraform',
'--build-arg=TERRAFORM_VERSION=${_TERRAFORM_VERSION}',
'--build-arg=TERRAFORM_VERSION_SHA256SUM=${_TERRAFORM_VERSION_SHA256SUM}',
'.'
]
- name: 'gcr.io/${PROJECT_ID}/terraform'
args: ['version']
substitutions:
_TERRAFORM_VERSION: '0.12.24' # default value
_TERRAFORM_VERSION_SHA256SUM: '602d2529aafdaa0f605c06adb7c72cfb585d8aa19b3f4d8d189b42589e27bf11' # default value
images: ['gcr.io/${PROJECT_ID}/terraform']
11 changes: 9 additions & 2 deletions modules/cloudbuild/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,18 @@ resource "google_cloudbuild_trigger" "non_master_trigger" {

resource "null_resource" "cloudbuild_terraform_builder" {
triggers = {
project_id_seed_project = module.cloudbuild_project.project_id
project_id_cloudbuild_project = module.cloudbuild_project.project_id
terraform_version_sha256sum = var.terraform_version_sha256sum
terraform_version = var.terraform_version
}

provisioner "local-exec" {
command = "gcloud builds submit ${path.module}/cloudbuild_builder/ --project ${module.cloudbuild_project.project_id} --config=${path.module}/cloudbuild_builder/cloudbuild.yaml"
command = <<EOT
gcloud builds submit ${path.module}/cloudbuild_builder/ \
--project ${module.cloudbuild_project.project_id} \
--config=${path.module}/cloudbuild_builder/cloudbuild.yaml \
--substitutions=_TERRAFORM_VERSION=${var.terraform_version},_TERRAFORM_VERSION_SHA256SUM=${var.terraform_version_sha256sum}
EOT
}
depends_on = [
google_project_service.cloudbuild_apis,
Expand Down
12 changes: 12 additions & 0 deletions modules/cloudbuild/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,15 @@ variable "folder_id" {
type = string
default = ""
}

variable "terraform_version" {
description = "Default terraform version."
type = string
default = "0.12.24"
}

variable "terraform_version_sha256sum" {
description = "sha256sum for default terraform version."
type = string
default = "602d2529aafdaa0f605c06adb7c72cfb585d8aa19b3f4d8d189b42589e27bf11"
}
2 changes: 1 addition & 1 deletion terraform.example.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ group_org_admins = "org-admins@example.com"

group_billing_admins = "billing-admins@example.com"

default_region = "australia-southeast1"
default_region = "australia-southeast1"

0 comments on commit 1f82423

Please sign in to comment.