Created
February 17, 2023 02:15
-
-
Save maucaro/1128cdae6d07ba460176f831defb2dd1 to your computer and use it in GitHub Desktop.
Google Cloud Build sample Terraform deployment with rollback on failure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
steps: | |
- name: 'hashicorp/terraform:1.0.4' | |
dir: terraform | |
args: | |
- '-c' | |
- | | |
terraform init | |
id: terraform init | |
entrypoint: sh | |
- name: 'hashicorp/terraform:1.0.4' | |
dir: terraform | |
args: | |
- '-c' | |
- |- | |
if ! terraform apply -input=false -auto-approve ; then | |
terraform destroy -input=false -auto-approve | |
return 1 | |
fi | |
id: terraform apply | |
entrypoint: sh | |
logsBucket: 'gs://arrcuspoc-cb-logs' | |
options: | |
logging: GCS_ONLY | |
timeout: 86400s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
terraform { | |
required_providers { | |
http = { | |
source = "hashicorp/http" | |
version = "3.2.1" | |
} | |
google = { | |
source = "hashicorp/google" | |
version = "4.53.1" | |
} | |
null = { | |
source = "hashicorp/null" | |
version = "3.2.1" | |
} | |
} | |
} | |
variable "project_id" { | |
type = string | |
description = "Google Cloud Project ID" | |
} | |
variable "region" { | |
type = string | |
description = "Google Cloud Region" | |
default = "us-west1" | |
} | |
provider "google" { | |
project = var.project_id | |
} | |
resource "google_compute_network" "vpc_network" { | |
name = "my-custom-mode-network" | |
auto_create_subnetworks = false | |
mtu = 1460 | |
} | |
resource "google_compute_subnetwork" "default" { | |
name = "my-custom-subnet" | |
ip_cidr_range = "10.0.1.0/24" | |
region = var.region | |
network = google_compute_network.vpc_network.id | |
depends_on = [ | |
null_resource.example | |
] | |
} | |
data "http" "example" { | |
url = "https://checkpoint-api.hashicorp.com/v1/check/terraform" | |
# Optional request headers | |
request_headers = { | |
Accept = "application/json" | |
} | |
} | |
resource "null_resource" "example" { | |
# On success, this will attempt to execute the true command in the | |
# shell environment running terraform. | |
# On failure, this will attempt to execute the false command in the | |
# shell environment running terraform. | |
provisioner "local-exec" { | |
# Expected status_code is 200; if ommitted from contains list, the command will fail | |
command = contains([201, 204], data.http.example.status_code) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment