-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update example to include dev and prod environments (GoogleCloudPlatf…
…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
1 parent
66e442a
commit 8d77037
Showing
18 changed files
with
178 additions
and
94 deletions.
There are no files selected for viewing
This file contains 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
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''' | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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
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" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../example-create/main.tf |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../example-create/outputs.tf |
6 changes: 2 additions & 4 deletions
6
...pipelines/example-create/terraform.tfvars → ...pelines/environments/dev/terraform.tfvars
This file contains 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
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" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../example-create/variables.tf |
This file contains 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
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" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../example-create/main.tf |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../example-create/outputs.tf |
This file contains 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
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" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../example-create/variables.tf |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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