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

fix: green checkmark on tf plan changes #13

Merged
merged 8 commits into from
Jan 11, 2024
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
29 changes: 13 additions & 16 deletions .github/workflows/_test-workflows.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@ on:

jobs:
# Enable once we have access to dai sandbox account
# test_terraform_deploy_basic:
# uses: ./.github/workflows/tf-deploy-basic.yaml
# with:
# github_artifact_path: path/to/artifacts
# aws_account_id: 123456789012
# aws_region: us-east-1
# aws_role_name: cicd-iac
# environment: sandbox
# tf_deploy_override: true
# tf_dir: tests/terraform
# tf_feedback: true
# tf_state_bucket: my-tf-state-bucket
# tf_state_key: my/tf/state
# tf_var_file: true
# tf_vars: var1=value1,var2=value2
# tf_version: 1.5.6
test_terraform_deploy_basic:
uses: ./.github/workflows/tf-deploy-basic.yaml
with:
github_artifact_path: path/to/artifacts
aws_account_id: ${{ vars.aws_account_id }}
aws_region: ${{ vars.aws_region }}
aws_role_name: ${{ vars.aws_role_name }}
environment: sandbox
github_feedback: true
tf_deploy_override: true
tf_dir: tests/terraform
tf_vars: var1=value1,var2=value2
tf_version: 1.6.6

test_docker_build:
uses: ./.github/workflows/docker-build.yaml
Expand Down
28 changes: 15 additions & 13 deletions .github/workflows/tf-deploy-basic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ on:
default: 1.5.6

jobs:
terraform_deployment:
name: Deployment
terraform:
permissions:
contents: read
id-token: write
Expand All @@ -84,15 +83,16 @@ jobs:
role-to-assume: arn:aws:iam::${{ inputs.aws_account_id }}:role/${{ inputs.aws_role_name }}
aws-region: ${{ inputs.aws_region }}

- uses: actions/download-artifact@v3
if: ${{ inputs.github_artifact_path }} != ""
- uses: actions/download-artifact@v4
if: inputs.github_artifact_path != ''
with:
path: ${{ inputs.github_artifact_path }}

- name: Terraform setup
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ inputs.tf_version }}
terraform_wrapper: false

- name: Terraform Init
id: init
Expand Down Expand Up @@ -139,23 +139,26 @@ jobs:
tf_args+=" -var-file=$var_file"
done

tf_plan_exitcode=0
terraform plan \
-detailed-exitcode \
-no-color \
-input=false \
-lock=false \
-out="tfplan" \
${tf_args}
continue-on-error: true
${tf_args} || tf_plan_exitcode=$?

echo "exitcode=$tf_plan_exitcode" >> $GITHUB_OUTPUT
if [ $tf_plan_exitcode -eq 1 ]; then
echo Terraform Plan Failed!
exit 1
fi

- name: Create String Output
id: tf-plan-string
run: |
TERRAFORM_PLAN=$(terraform show -no-color tfplan)
TERRAFORM_SUMMARY_HEADER=$(grep -E -i 'No changes|Plan: ' <<< "$TERRAFORM_PLAN")

echo "Plan exit code: ${{ steps.plan.outputs.exitcode }}"

delimiter="$(openssl rand -hex 8)"
echo "summary<<${delimiter}" >> $GITHUB_OUTPUT
echo "$TERRAFORM_SUMMARY_HEADER" >> $GITHUB_OUTPUT
Expand All @@ -180,7 +183,7 @@ jobs:
- uses: actions/[email protected]
if: github.event_name == 'pull_request' && inputs.github_feedback == true
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
PLAN: "${{ steps.tf-plan-string.outputs.summary }}"
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
Expand All @@ -190,9 +193,8 @@ jobs:
* Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
* Terraform Validation 🤖${{ steps.validate.outcome }}
* Terraform Plan 📖\`${{ steps.plan.outcome }}\`
<details><summary>Show Plan</summary>
\`\`\`${process.env.PLAN}\`\`\`
</details>

${process.env.PLAN}
*Pusher: Action: \`${{ github.event_name }}\`, Workflow: \`${{ github.workflow }}\`*`;
const comments = await github.issues.listComments({
issue_number: context.issue.number,
Expand Down
8 changes: 8 additions & 0 deletions tests/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ output "random_pet" {
output "random_id" {
value = random_id.this.id
}

variable "var1" {
type = string
}

variable "var2" {
type = string
}
Loading