From 95ddff563afa10308f6afa3ceb91a683d79de401 Mon Sep 17 00:00:00 2001 From: David Cheung Date: Thu, 24 Sep 2020 12:46:30 -0400 Subject: [PATCH 1/4] add linting and validation to terraform code --- .github/workflows/terraform.yml | 54 ++++++++++++++++++++ tests/fixtures/test-project/zero-project.yml | 26 ++++++++++ 2 files changed, 80 insertions(+) create mode 100644 .github/workflows/terraform.yml create mode 100644 tests/fixtures/test-project/zero-project.yml diff --git a/.github/workflows/terraform.yml b/.github/workflows/terraform.yml new file mode 100644 index 0000000..6ee3998 --- /dev/null +++ b/.github/workflows/terraform.yml @@ -0,0 +1,54 @@ +name: 'Validate Terraform' + +on: + push: + branches: + - main + pull_request: + +jobs: + validate: + name: template terraform + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: hashicorp/setup-terraform@v1 + + - name: Install Zero + id: install_zero + run: | + mkdir ${GITHUB_WORKSPACE}/bin + cd ${GITHUB_WORKSPACE}/bin + LATEST_ZERO_DOWNLOAD_URL=$(curl --silent "https://api.github.com/repos/commitdev/zero/releases/latest" | jq -r ".assets | .[] | select(.name | contains(\"Linux_x86_64\")) | .browser_download_url") + ZERO_BINARY_FILE_NAME=$(curl --silent "https://api.github.com/repos/commitdev/zero/releases/latest" | jq -r ".assets | .[] | select(.name | contains(\"Linux_x86_64\")) | .name") + curl -OJL $LATEST_ZERO_DOWNLOAD_URL + tar -xvf "./${ZERO_BINARY_FILE_NAME}" + ./zero version + echo "::add-path::${GITHUB_WORKSPACE}/bin" + - name: Copy test project structure + id: copy_test_dir + run: | + cp -r ${GITHUB_WORKSPACE}/tests/fixtures/test-project/ ${GITHUB_WORKSPACE}/temp-project + - name: zero create + id: zero_create + run: | + cd ${GITHUB_WORKSPACE}/temp-project + zero create + - name: Terraform Init and Validate + id: init_and_validate + run: | + INFRA_DIR=${GITHUB_WORKSPACE}/temp-project/infrastructure + ## Defining test targets per line, last line ends with double quote + TERRAFORM_TEST_TARGETS="${INFRA_DIR}/terraform/bootstrap/remote-state + ${INFRA_DIR}/terraform/bootstrap/secrets + ${INFRA_DIR}/terraform/environments/stage + ${INFRA_DIR}/terraform/environments/prod + ${INFRA_DIR}/kubernetes/terraform/environments/stage + ${INFRA_DIR}/kubernetes/terraform/environments/prod" + + for dir in $TERRAFORM_TEST_TARGETS; do + echo "Linting ${dir}" + cd $dir + terraform init -backend=false + terraform validate -no-color + done diff --git a/tests/fixtures/test-project/zero-project.yml b/tests/fixtures/test-project/zero-project.yml new file mode 100644 index 0000000..0f28ec1 --- /dev/null +++ b/tests/fixtures/test-project/zero-project.yml @@ -0,0 +1,26 @@ +# Templated zero-project.yml file +name: test-project + +shouldPushRepositories: false + +modules: + zero-aws-eks-stack: + parameters: + accountId: "123456781234" + database: mysql + eksWorkerAMI: ami-06d3900b00136f924 + fileUploads: "no" + loggingType: kibana + productionBackendSubdomain: api. + productionFrontendSubdomain: app. + productionHostRoot: zero-test.online + randomSeed: 08178BF6 + region: us-east-2 + sendgridApiKey: SOME_API_KEY + stagingBackendSubdomain: api-stage. + stagingFrontendSubdomain: app-stage. + stagingHostRoot: zero-test-stage.online + files: + dir: infrastructure + repo: github.com/commitdev-test/infrastructure + source: ../ From 63222e100cb3a58a332f31c2f68a46e6504fa56d Mon Sep 17 00:00:00 2001 From: David Cheung Date: Fri, 25 Sep 2020 16:23:09 -0400 Subject: [PATCH 2/4] fixup! add linting and validation to terraform code --- .github/workflows/terraform.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/terraform.yml b/.github/workflows/terraform.yml index 6ee3998..8acb3a9 100644 --- a/.github/workflows/terraform.yml +++ b/.github/workflows/terraform.yml @@ -8,7 +8,7 @@ on: jobs: validate: - name: template terraform + name: Validate Terraform runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 From f674d664f0058e212f382873296b8b8c799d98f4 Mon Sep 17 00:00:00 2001 From: David Cheung Date: Fri, 25 Sep 2020 18:25:46 -0400 Subject: [PATCH 3/4] fixup! fixup! add linting and validation to terraform code --- README.md | 6 ++++++ tests/fixtures/test-project/zero-project.yml | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b3f353..b370810 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,11 @@ the [Zero][zero] repository for more information. The tool will parse through configuration and fill in any template variables which need to be encoded into the terraform configuration. +### Testing and linting +The codebase uses terraform validate as a basic sanity check, it uses a +[fixture][ci-fixture] of reasonable values to fill-in the templates, and then runs +`terraform init` and `terraform validate` against the environment and modules. + ### How much does this stack cost? The expected total monthly cost: $ 0.202 USD / hr or ~$150USD / month. The most expensive component will be the EKS cluster as well as the instances that it @@ -72,6 +77,7 @@ Project board: [zenhub][zenhub-board] [zero]: https://github.com/commitdev/zero [arch-diagram]: ./docs/architecture-overview.svg [resource-list]: ./docs/resources.md +[ci-fixture]: tests/fixtures/test-project/zero-project.yml [aws-cli]: https://docs.aws.amazon.com/polly/latest/dg/setup-aws-cli.html [aws-route53]: https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/domain-register.html diff --git a/tests/fixtures/test-project/zero-project.yml b/tests/fixtures/test-project/zero-project.yml index 0f28ec1..d224576 100644 --- a/tests/fixtures/test-project/zero-project.yml +++ b/tests/fixtures/test-project/zero-project.yml @@ -1,4 +1,5 @@ -# Templated zero-project.yml file +# CI Test fixture +# data used by github actions to template out terraform code, then running terraform validate name: test-project shouldPushRepositories: false @@ -23,4 +24,6 @@ modules: files: dir: infrastructure repo: github.com/commitdev-test/infrastructure + ## This source is dependant on where the zero-project.yml is copied to + ## needs to resolve to the root of zero-aws-eks-stack equivalent to locate the /templates folder source: ../ From 8a62fda138dfcef1592677697df85261a008722c Mon Sep 17 00:00:00 2001 From: David Cheung Date: Fri, 25 Sep 2020 18:29:07 -0400 Subject: [PATCH 4/4] fixup! fixup! fixup! add linting and validation to terraform code --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b370810..eaeae27 100644 --- a/README.md +++ b/README.md @@ -53,9 +53,9 @@ information. The tool will parse through configuration and fill in any template variables which need to be encoded into the terraform configuration. ### Testing and linting -The codebase uses terraform validate as a basic sanity check, it uses a -[fixture][ci-fixture] of reasonable values to fill-in the templates, and then runs -`terraform init` and `terraform validate` against the environment and modules. +The codebase uses terraform validate as a basic sanity check, it uses +[an example zero-project.yml fixture][ci-fixture] to fill-in the templates, then runs +`terraform init` and `terraform validate` against the templated out environment and modules. ### How much does this stack cost? The expected total monthly cost: $ 0.202 USD / hr or ~$150USD / month. The most