Skip to content

Commit

Permalink
update example to include dev and prod environments (GoogleCloudPlatf…
Browse files Browse the repository at this point in the history
…orm#12)

* reverting to original example with added environment folders

* small fix to jenkinsfile

* making tfvars file envionment specific

* changed values in tfvars to avoid conflicts

* fix to jenkinsfile for tf apply stage and removed unused vars from tfvars

* changes to jenkinsfile to cover all merge/PR scenarios, fixed path to jenkinsfile in values.yaml

* added a variable for environment to be used in naming of resources

* removed CHANGE_TARGET and tf out file from tf apply stage

* removed extra -

* updated CIDR to valid RFC 1918 space

* updated CIDR to valid RFC 1918 space for dev

* removing approval stage

Co-authored-by: Rahul Gupta <[email protected]>
  • Loading branch information
2 people authored and bharathkkb committed Mar 30, 2020
1 parent 66e442a commit 8d77037
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 94 deletions.
105 changes: 105 additions & 0 deletions example-pipelines/environments/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

pipeline {
agent {
label "terraform-exec"
}
stages {
stage('TF init & validate') {
when { anyOf {branch "prod";branch "dev";changeRequest() } }
steps {
container('terraform') {
sh '''
if [[ $CHANGE_TARGET ]]; then
TARGET_ENV=$CHANGE_TARGET
else
TARGET_ENV=$BRANCH_NAME
fi
if [ -d "example-pipelines/environments/${TARGET_ENV}/" ]; then
cd example-pipelines/environments/${TARGET_ENV}
terraform init
terraform validate
else
for dir in example-pipelines/environments/*/
do
cd ${dir}
env=${dir%*/}
env=${env#*/}
echo ""
echo "*************** TERRAFOM INIT and VALIDATE ******************"
echo "******* At environment: ${env} ********"
echo "*************************************************"
terraform init || exit 1
terraform validate || exit 1
cd ../../../
done
fi'''
}
}
}
stage('TF plan') {
when { anyOf {branch "prod";branch "dev";changeRequest() } }
steps {
container('terraform') {
sh '''
if [[ $CHANGE_TARGET ]]; then
TARGET_ENV=$CHANGE_TARGET
else
TARGET_ENV=$BRANCH_NAME
fi
if [ -d "example-pipelines/environments/${TARGET_ENV}/" ]; then
cd example-pipelines/environments/${TARGET_ENV}
terraform plan
else
for dir in example-pipelines/environments/*/
do
cd ${dir}
env=${dir%*/}
env=${env#*/}
echo ""
echo "*************** TERRAFOM PLAN ******************"
echo "******* At environment: ${env} ********"
echo "*************************************************"
terraform plan || exit 1
cd ../../../
done
fi'''
}
}
}
stage('TF Apply') {
when { anyOf {branch "prod";branch "dev" } }
steps {
container('terraform') {
sh '''
TARGET_ENV=$BRANCH_NAME
if [ -d "example-pipelines/environments/${TARGET_ENV}/" ]; then
cd example-pipelines/environments/${TARGET_ENV}
terraform apply -input=false -auto-approve
else
echo "*************** SKIPPING APPLY ******************"
echo "Branch '$TARGET_ENV' does not represent an official environment."
echo "*************************************************"
fi'''
}
}
}
}
}
21 changes: 21 additions & 0 deletions example-pipelines/environments/dev/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


terraform {
backend "gcs" {
bucket = "PROJECT_ID-tfstate"
prefix = "env/dev"
}
}
1 change: 1 addition & 0 deletions example-pipelines/environments/dev/main.tf
1 change: 1 addition & 0 deletions example-pipelines/environments/dev/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@


project_id = "PROJECT_ID"
vpc_network_name = "example-vpc"
environment = "dev"
subnet1_region = "us-central1"
subnet1_zone = "us-central1-a"
subnet1_cidr = "10.0.0.0/17"
subnet1_cidr = "10.10.0.0/16"
1 change: 1 addition & 0 deletions example-pipelines/environments/dev/variables.tf
21 changes: 21 additions & 0 deletions example-pipelines/environments/prod/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


terraform {
backend "gcs" {
bucket = "PROJECT_ID-tfstate"
prefix = "env/prod"
}
}
1 change: 1 addition & 0 deletions example-pipelines/environments/prod/main.tf
1 change: 1 addition & 0 deletions example-pipelines/environments/prod/outputs.tf
5 changes: 5 additions & 0 deletions example-pipelines/environments/prod/terraform.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
project_id = "PROJECT_ID"
environment = "prod"
subnet1_region = "us-central1"
subnet1_zone = "us-central1-a"
subnet1_cidr = "10.20.0.0/16"
1 change: 1 addition & 0 deletions example-pipelines/environments/prod/variables.tf
59 changes: 0 additions & 59 deletions example-pipelines/example-create/Jenkinsfile

This file was deleted.

22 changes: 0 additions & 22 deletions example-pipelines/example-create/backend.tf

This file was deleted.

14 changes: 11 additions & 3 deletions example-pipelines/example-create/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
* limitations under the License.
*/

/*****************************************
Locals
*****************************************/
locals {
vpc_network_name = "example-vpc-${var.environment}"
vm_name = "example-vm-${var.environment}-001"
}

/*****************************************
Google Provider Configuration
*****************************************/
Expand All @@ -28,11 +36,11 @@ module "gcp-network" {
source = "terraform-google-modules/network/google"
version = "~> 1.4.0"
project_id = var.project_id
network_name = var.vpc_network_name
network_name = local.vpc_network_name

subnets = [
{
subnet_name = "${var.vpc_network_name}-${var.subnet1_region}"
subnet_name = "${local.vpc_network_name}-${var.subnet1_region}"
subnet_ip = var.subnet1_cidr
subnet_region = var.subnet1_region
},
Expand All @@ -45,7 +53,7 @@ module "gcp-network" {
resource "google_compute_instance" "vm_0001" {
project = var.project_id
zone = var.subnet1_zone
name = "${var.project_id}-vm-0001"
name = local.vm_name
machine_type = "f1-micro"
network_interface {
network = module.gcp-network.network_name
Expand Down
3 changes: 3 additions & 0 deletions example-pipelines/example-create/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* limitations under the License.
*/

output "example_vpc_name" {
value = module.gcp-network.network_name
}

output "example_vm_name" {
value = google_compute_instance.vm_0001.name
Expand Down
6 changes: 3 additions & 3 deletions example-pipelines/example-create/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ variable "project_id" {
default = ""
}

variable "vpc_network_name" {
variable "environment" {
type = string
description = "Name of VPC Network to be created"
default = "example-vpc"
description = "Name of the environment (dev or prod)"
default = "dev"
}

variable "subnet1_region" {
Expand Down
2 changes: 1 addition & 1 deletion jenkins-gke/jenkins-helm/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ master:
multibranchPipelineJob('terraform-jenkins-create-demo') {
factory {
workflowBranchProjectFactory {
scriptPath('example-pipelines/example-create/Jenkinsfile')
scriptPath('example-pipelines/environments/Jenkinsfile')
}
}
branchSources {
Expand Down
2 changes: 0 additions & 2 deletions jenkins-gke/tf-gke/terraform.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

project_id = "PROJECT_ID"
tfstate_gcs_backend = "PROJECT_ID-tfstate"
restart_policy = "Always"
region = "us-east4"
zones = ["us-east4-a"]
ip_range_pods_name = "ip-range-pods"
ip_range_services_name = "ip-range-scv"
network_name = "jenkins-network"
subnet_ip = "10.10.10.0/24"
subnet_name = "jenkins-subnet"
jenkins_project_name = "jenkins-gke"
jenkins_k8s_config = "jenkins-k8s-config"

0 comments on commit 8d77037

Please sign in to comment.