Skip to content

Commit

Permalink
chore: working on the terraform tests creations
Browse files Browse the repository at this point in the history
  • Loading branch information
gambol99 committed May 10, 2024
1 parent 64934fc commit ccba870
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/terraform-module-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,29 @@ on:
required: false
type: boolean

enable-terraform-tests-credentials:
default: false
description: "Whether to run terraform test with AWS credentials"
required: false
type: boolean

terraform-dir:
default: "."
description: "The directory to validate"
required: false
type: string

terraform-tests-aws-region:
default: "eu-west-1"
description: "The AWS region to use for the terraform tests"
required: false
type: string

terraform-tests-aws-role:
description: "The AWS role to assume for the terraform tests"
required: false
type: string

terraform-version:
default: "1.7.1"
description: "The version of terraform to use"
Expand All @@ -55,6 +72,11 @@ on:
required: false
type: string

env:
AWS_ROLE: ${{ inputs.aws-role }}
AWS_WEB_IDENTITY_TOKEN_FILE: /tmp/web_identity_token_file
TF_LOG: ${{ inputs.terraform-log-level }}

permissions:
contents: read
pull-requests: write
Expand Down Expand Up @@ -145,8 +167,22 @@ jobs:
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Retrieve Web Identity Token for AWS Authentication
if: inputs.enable-terraform-tests-credentials
run: |
curl -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=sts.amazonaws.com" | jq -r '.value' > $AWS_WEB_IDENTITY_TOKEN_FILE
- name: Authenticate with AWS
if: inputs.enable-terraform-tests-credentials
id: auth
uses: aws-actions/configure-aws-credentials@v4
with:
aws-region: ${{ inputs.terraform-tests-aws-region }}
role-session-name: ${{ github.event.repository.name }}
role-to-assume: ${{ inputs.terraform-tests-aws-role }}
mask-aws-account-id: "no"
- name: Terraform Init
run: terraform -chdir=${{ inputs.terraform-dir }} init -backend=false
- name:
- name: Run Tests
id: tests
run: terraform -chdir=${{ inputs.terraform-dir }} test
Expand Down
31 changes: 0 additions & 31 deletions .github/workflows/terraform-plan-and-apply-aws.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,6 @@ on:
required: false
type: string

enable-terraform-tests:
default: true
description: "Whether to run terraform test"
required: false
type: boolean

enable-infracost:
default: false
description: "Whether to run infracost on the Terraform Plan (secrets.infracost-api-key must be set if enabled)"
Expand Down Expand Up @@ -199,28 +193,6 @@ jobs:
id: lint
run: tflint -f compact

terraform-tests:
name: "Terraform Unit Tests"
runs-on: ubuntu-latest
if: inputs.enable-terraform-tests
defaults:
run:
working-directory: ${{ inputs.working-directory }}
outputs:
result: ${{ steps.tests.outcome }}
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Setup Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.terraform-version }}
- name: Terraform Init
run: terraform -chdir=${{ inputs.terraform-dir }} init -backend=false
- name: Run Tests
id: tests
run: terraform -chdir=${{ inputs.terraform-dir }} test

terraform-security:
name: "Terraform Security"
runs-on: ubuntu-latest
Expand Down Expand Up @@ -365,7 +337,6 @@ jobs:
- terraform-format
- terraform-lint
- terraform-plan
- terraform-tests
- terraform-security
- commitlint
steps:
Expand Down Expand Up @@ -402,7 +373,6 @@ jobs:
* ๐Ÿ” <b>Terraform Linting:</b> \`${{ needs.terraform-lint.outputs.result }}\`
* ๐Ÿ”‘ <b>AWS Authentication:</b> \`${{ needs.terraform-plan.outputs.result-auth }}\`
* ๐Ÿ‘ฎ <b>Terraform Security Check:</b> \`${{ needs.terraform-security.outputs.result }}\`
* ๐Ÿ”˜ <b>Terraform Tests:</b> \`${{ needs.terraform-tests.outputs.result }}\`
* ๐Ÿ”ง <b>Terraform Initialisation:</b> \`${{ needs.terraform-plan.outputs.result-init }}\`
* ๐Ÿค– <b>Terraform Validation:</b> \`${{ needs.terraform-plan.outputs.result-validate }}\`
* ๐Ÿ“ <b>Terraform S3 Backend:</b> \`${{ needs.terraform-plan.outputs.result-s3-backend-check }}\`
Expand Down Expand Up @@ -447,7 +417,6 @@ jobs:
- terraform-format
- terraform-lint
- terraform-plan
- terraform-tests
steps:
- name: Checkout Repository
uses: actions/checkout@v4
Expand Down

0 comments on commit ccba870

Please sign in to comment.