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

update Terraform CI validation and CI trigger #421

Merged
merged 5 commits into from
Sep 21, 2021
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
14 changes: 11 additions & 3 deletions .github/workflows/validate-terraform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
# Licensed under the MIT License.

name: validate-terraform
on: [pull_request, workflow_dispatch]
on:
pull_request:
paths:
- 'src/terraform/**'
workflow_dispatch:
jobs:
validate-terraform:
runs-on: ubuntu-latest
steps:
- uses: hashicorp/setup-terraform@v1
with:
terraform_version: 0.13.4
terraform_version: 1.0.3
- shell: bash
name: check tooling versions
run: |
Expand All @@ -18,4 +22,8 @@ jobs:
- shell: bash
name: validate and lint terraform
run: |
src/build/validate_tf.sh
src/build/validate_tf.sh src/terraform/mlz src/terraform/tier3
- shell: bash
name: check terraform formatting
run: |
src/build/check_tf_format.sh src/terraform
49 changes: 49 additions & 0 deletions src/build/check_tf_format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
#
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
# Check Terraform formatting for 1:M directories, exiting if any errors are produced

program_log () {
echo "${0}: ${1}"
}

error_log () {
echo "Error: ${1}"
}

# Check for Terraform
if ! command -v terraform &> /dev/null; then
error_log "Terraform could not be found. This script requires the Terraform CLI."
echo "See https://learn.hashicorp.com/tutorials/terraform/install-cli for installation instructions."
exit 1
fi

format_tf() {
local tf_dir=$1
cd "$tf_dir" || exit 1
program_log "checking formatting at $tf_dir..."
if terraform fmt -check -recursive >> /dev/null;
then
program_log "successful check with 'terraform fmt -check -recursive ${tf_dir}'"
else
linting_results=$(terraform fmt -check -recursive)
for j in $linting_results
do
error_log "'${j}' is not formatted correctly. Format with the command 'terraform fmt ${j}'"
done
program_log "run 'terraform fmt -recursive' to format all Terraform components in a directory"
exit 1;
fi
}

working_dir=$(pwd)

for arg in "$@"
do
cd "$working_dir" || exit 1
format_tf "$(realpath "$arg")"
done

program_log "done!"
50 changes: 16 additions & 34 deletions src/build/validate_tf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
#
# Validates and lints terraform, exiting if any errors are produced
# Validates and lints Terraform for 1:M directories, exiting if any errors are produced

program_log () {
echo "${0}: ${1}"
Expand All @@ -20,38 +20,20 @@ if ! command -v terraform &> /dev/null; then
exit 1
fi

full_path=$(realpath "${0}")
repo_path=$(dirname "$(dirname "${full_path}")")
core_path="${repo_path}/core"
validate_tf() {
local tf_dir=$1
cd "$tf_dir" || exit 1
program_log "validating at $tf_dir..."
terraform init -backend=false >> /dev/null || exit 1
terraform validate >> /dev/null || exit 1
}

if [ -d "$core_path" ];
then
# Validate all .tf and their dependencies in core_path
program_log "Validating Terraform..."
cd "${core_path}" || exit
for i in $(find . -name "*.tf" -printf "%h\n" | sort --unique)
do
cd "${i}" || exit
echo "validating ${i}..."
terraform init -backend=false >> /dev/null || exit 1
terraform validate >> /dev/null || exit 1
cd "${core_path}" || exit
done
program_log "Terraform validated successfully!"
working_dir=$(pwd)

# Check formatting in all .tf files in repo
program_log "Linting Terraform..."
cd "${repo_path}" || exit
if terraform fmt -check -recursive >> /dev/null;
then
program_log "Terraform linted successfully!"
else
linting_results=$(terraform fmt -check -recursive)
for j in $linting_results
do
error_log "please format '${j}' with the command 'terraform fmt'"
done
program_log "alternatively, you can run 'terraform fmt -recursive' to format all *.tf in a directory"
exit 1;
fi
fi
for arg in "$@"
do
cd "$working_dir" || exit 1
validate_tf "$(realpath "$arg")"
done

program_log "done!"
2 changes: 1 addition & 1 deletion src/terraform/modules/firewall/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,5 @@ variable "tags" {
variable "disable_snat_ip_range" {
description = "The address space to be used to ensure that SNAT is disabled."
default = ["0.0.0.0/0"]
type = list
type = list(any)
}