From 2e7a17b2f2ec8db2acf3baa663be67222ab975c3 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 18:15:38 +0100 Subject: [PATCH 01/27] modify workflow --- terraform/bkp.terraform.tfvars.k | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/terraform/bkp.terraform.tfvars.k b/terraform/bkp.terraform.tfvars.k index f9fc7a7..de4f8b5 100644 --- a/terraform/bkp.terraform.tfvars.k +++ b/terraform/bkp.terraform.tfvars.k @@ -10,6 +10,4 @@ db_domain = "db.cv1.drintech.online" traefik_domain = "traefik.cv1.drintech.online" cert_email = "admin@example.com" # replace with a valid email -Still testing.... - - +Still hello.... From 78de87b2ea5c5c990386351c5c8798abb3a3f4b8 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 18:38:25 +0100 Subject: [PATCH 02/27] modify yml path --- .github/workflows/terraform-validate.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/terraform-validate.yml b/.github/workflows/terraform-validate.yml index d07f5e6..3055d58 100644 --- a/.github/workflows/terraform-validate.yml +++ b/.github/workflows/terraform-validate.yml @@ -6,8 +6,8 @@ on: push: branches: - 'infra_features' - path: - - '../terraform/**' + # path: + # - '../terraform/**' env: AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }} From c2b5c4792a346d90d47e7f11dfefd004425aa711 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 18:40:07 +0100 Subject: [PATCH 03/27] modify yml --- .github/workflows/terraform-plan.yml | 4 ++-- .github/workflows/terraform-validate.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index cbac0f4..bc02005 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -7,8 +7,8 @@ on: types: [opened, synchronize, reopened] branches: - 'infra_main' - paths: - - './terraform/**' + # paths: + # - './terraform/**' env: AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }} diff --git a/.github/workflows/terraform-validate.yml b/.github/workflows/terraform-validate.yml index 3055d58..d07f5e6 100644 --- a/.github/workflows/terraform-validate.yml +++ b/.github/workflows/terraform-validate.yml @@ -6,8 +6,8 @@ on: push: branches: - 'infra_features' - # path: - # - '../terraform/**' + path: + - '../terraform/**' env: AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }} From 80fecf417840a2e9f60ffe9773a02da16ea3bd34 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 18:45:55 +0100 Subject: [PATCH 04/27] test yml --- terraform/bkp.terraform.tfvars.k | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terraform/bkp.terraform.tfvars.k b/terraform/bkp.terraform.tfvars.k index de4f8b5..cd79c31 100644 --- a/terraform/bkp.terraform.tfvars.k +++ b/terraform/bkp.terraform.tfvars.k @@ -10,4 +10,4 @@ db_domain = "db.cv1.drintech.online" traefik_domain = "traefik.cv1.drintech.online" cert_email = "admin@example.com" # replace with a valid email -Still hello.... +Let's move on From 672d4efcbea59d9ff8a02516957ba56774a78342 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 18:57:30 +0100 Subject: [PATCH 05/27] modify.yml --- .github/workflows/terraform-plan.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index bc02005..801484f 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -9,6 +9,8 @@ on: - 'infra_main' # paths: # - './terraform/**' +permissions: + pull-requests: write env: AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }} From 82d74c7dcd11c25032425fe74f4a1c0a96d26113 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 20:02:49 +0100 Subject: [PATCH 06/27] modify yml --- .github/workflows/terraform-plan.yml | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index 801484f..36e44a0 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -60,15 +60,29 @@ jobs: run: terraform show -json tfplan.out > tfplan.json working-directory: ./terraform + # Checkout the branch you want Infracost to compare costs against, most commonly the target branch. + - name: Checkout base branch + uses: actions/checkout@v4 + with: + ref: '${{ github.event.pull_request.base.ref }}' - name: Setup Infracost uses: infracost/actions/setup@v3 with: api-key: ${{ secrets.INFRACOST_API_KEY }} - - name: Run Infracost run: | - infracost breakdown --path=./terraform --format=json --out-file=/tmp/infracost.json + infracost breakdown --path=./terraform --format=json --out-file=/tmp/infracost-base.json + # Checkout the PR branch with your infrastructure changes. + - uses: actions/checkout@v4 + - name: Run Infracost + shell: bash + run: | + infracost breakdown --path=./terraform \ + --format=json \ + --compare-to=/tmp/infracost-base.json \ + --out-file=/tmp/infracost.json + - name: Post Infracost Comment run: | infracost comment github --path=/tmp/infracost.json \ From 000bbee2e4c9f17302fb7e02093c0ca14fb5d04d Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 20:09:55 +0100 Subject: [PATCH 07/27] modify yml --- .github/workflows/terraform-plan.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index 36e44a0..b656e02 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -78,7 +78,7 @@ jobs: - name: Run Infracost shell: bash run: | - infracost breakdown --path=./terraform \ + infracost diff --path=./terraform \ --format=json \ --compare-to=/tmp/infracost-base.json \ --out-file=/tmp/infracost.json From c8ccb20948c4233755d16360e0453c4450aa46b5 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 20:30:24 +0100 Subject: [PATCH 08/27] modify yml --- .github/workflows/terraform-plan.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index b656e02..bebd94f 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -82,7 +82,12 @@ jobs: --format=json \ --compare-to=/tmp/infracost-base.json \ --out-file=/tmp/infracost.json - + + # generate the html report based on the JSON output from last step + - name: Generate Infracost Report + run: | + infracost output --path=/tmp/infracost.json --show-skipped --format html --out-file report.html + - name: Post Infracost Comment run: | infracost comment github --path=/tmp/infracost.json \ From 0efbfbeabfad2a269eb1627e2aface81bba4b9e4 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:08:51 +0100 Subject: [PATCH 09/27] modify workflow --- .github/workflows/terraform-plan.yml | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index bebd94f..31eeb31 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -71,23 +71,18 @@ jobs: api-key: ${{ secrets.INFRACOST_API_KEY }} - name: Run Infracost run: | - infracost breakdown --path=./terraform --format=json --out-file=/tmp/infracost-base.json + infracost breakdown --path=./terraform/tfplan.json --format=json --out-file=/tmp/infracost-base.json # Checkout the PR branch with your infrastructure changes. - uses: actions/checkout@v4 - name: Run Infracost shell: bash run: | - infracost diff --path=./terraform \ + infracost diff --path=./terraform/tfplan.json \ --format=json \ --compare-to=/tmp/infracost-base.json \ --out-file=/tmp/infracost.json - # generate the html report based on the JSON output from last step - - name: Generate Infracost Report - run: | - infracost output --path=/tmp/infracost.json --show-skipped --format html --out-file report.html - - name: Post Infracost Comment run: | infracost comment github --path=/tmp/infracost.json \ From 01c6ed6eed990aaaaf411652a7d1a4c05181ea93 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:17:50 +0100 Subject: [PATCH 10/27] modify workflow --- .github/workflows/terraform-plan.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index 31eeb31..015623c 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -57,7 +57,7 @@ jobs: - name: Save Plan JSON id: save-plan - run: terraform show -json tfplan.out > tfplan.json + run: terraform show -json tfplan.out > ../tfplan.json working-directory: ./terraform # Checkout the branch you want Infracost to compare costs against, most commonly the target branch. @@ -71,14 +71,14 @@ jobs: api-key: ${{ secrets.INFRACOST_API_KEY }} - name: Run Infracost run: | - infracost breakdown --path=./terraform/tfplan.json --format=json --out-file=/tmp/infracost-base.json + infracost breakdown --path=./tfplan.json --format=json --out-file=/tmp/infracost-base.json # Checkout the PR branch with your infrastructure changes. - uses: actions/checkout@v4 - name: Run Infracost shell: bash run: | - infracost diff --path=./terraform/tfplan.json \ + infracost diff --path=./tfplan.json \ --format=json \ --compare-to=/tmp/infracost-base.json \ --out-file=/tmp/infracost.json From c244cc14911d52624d51179ad6253703963a29d0 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:30:02 +0100 Subject: [PATCH 11/27] modify workflow --- .github/workflows/terraform-plan.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index 015623c..11db44b 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -57,7 +57,7 @@ jobs: - name: Save Plan JSON id: save-plan - run: terraform show -json tfplan.out > ../tfplan.json + run: terraform show -json tfplan.out > ../../tfplan.json && pwd working-directory: ./terraform # Checkout the branch you want Infracost to compare costs against, most commonly the target branch. @@ -71,14 +71,14 @@ jobs: api-key: ${{ secrets.INFRACOST_API_KEY }} - name: Run Infracost run: | - infracost breakdown --path=./tfplan.json --format=json --out-file=/tmp/infracost-base.json + infracost breakdown --path=../tfplan.json --format=json --out-file=/tmp/infracost-base.json && pwd # Checkout the PR branch with your infrastructure changes. - uses: actions/checkout@v4 - name: Run Infracost shell: bash run: | - infracost diff --path=./tfplan.json \ + infracost diff --path=../tfplan.json \ --format=json \ --compare-to=/tmp/infracost-base.json \ --out-file=/tmp/infracost.json From 532c2b8e16fae203b801166beef12bc02e2e1c12 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:42:56 +0100 Subject: [PATCH 12/27] add extra resource --- terraform/ec2.tf | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/terraform/ec2.tf b/terraform/ec2.tf index b305cfc..83f614b 100644 --- a/terraform/ec2.tf +++ b/terraform/ec2.tf @@ -13,4 +13,17 @@ resource "aws_instance" "ec2" { } } +resource "aws_instance" "ec2-1" { + ami = var.ami_id + instance_type = var.instance_type + key_name = var.key_pair_name + subnet_id = aws_subnet.public_subnet.id + vpc_security_group_ids = [aws_security_group.sg.id] + tags = { + Name = var.ec2_name + } + provisioner "local-exec" { + command = "echo 'Instance provisioned: ${self.public_ip}'" + } +} \ No newline at end of file From 78c94a814aa9fa7d3ceebfcbe53f85377d04dc20 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:59:14 +0100 Subject: [PATCH 13/27] modify workflow --- .github/workflows/terraform-plan.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index 11db44b..3896fcf 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -71,17 +71,18 @@ jobs: api-key: ${{ secrets.INFRACOST_API_KEY }} - name: Run Infracost run: | - infracost breakdown --path=../tfplan.json --format=json --out-file=/tmp/infracost-base.json && pwd + infracost breakdown --path=. --format=json --out-file=/tmp/infracost-base.json && pwd && cat /tmp/infracost-base.json - # Checkout the PR branch with your infrastructure changes. - - uses: actions/checkout@v4 + # Checkout the current PR branch so we can create a diff. + - name: Checkout PR branch + uses: actions/checkout@v4 - name: Run Infracost shell: bash run: | infracost diff --path=../tfplan.json \ --format=json \ --compare-to=/tmp/infracost-base.json \ - --out-file=/tmp/infracost.json + --out-file=/tmp/infracost.json && cat /tmp/infracost.json - name: Post Infracost Comment run: | From 582953631d1e2af3a9ec2c6118dfedbb898f053d Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 23:23:16 +0100 Subject: [PATCH 14/27] modify workflow --- .github/workflows/terraform-plan.yml | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index 3896fcf..d929b4b 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -60,25 +60,24 @@ jobs: run: terraform show -json tfplan.out > ../../tfplan.json && pwd working-directory: ./terraform - # Checkout the branch you want Infracost to compare costs against, most commonly the target branch. - - name: Checkout base branch - uses: actions/checkout@v4 - with: - ref: '${{ github.event.pull_request.base.ref }}' - name: Setup Infracost uses: infracost/actions/setup@v3 with: api-key: ${{ secrets.INFRACOST_API_KEY }} - name: Run Infracost run: | - infracost breakdown --path=. --format=json --out-file=/tmp/infracost-base.json && pwd && cat /tmp/infracost-base.json + infracost breakdown --path=../tfplan.json --format=json --out-file=/tmp/infracost.json && pwd && cat /tmp/infracost.json - # Checkout the current PR branch so we can create a diff. - - name: Checkout PR branch + # Checkout the branch you want Infracost to compare costs against, most commonly the target branch. + - name: Checkout base branch uses: actions/checkout@v4 + with: + ref: '${{ github.event.pull_request.base.ref }}' + - name: Run Infracost shell: bash run: | + infracost breakdown --path=./terraform --format=json --out-file=/tmp/infracost-base.json && pwd && cat /tmp/infracost-base.json infracost diff --path=../tfplan.json \ --format=json \ --compare-to=/tmp/infracost-base.json \ From 8a5d181161a4e8d118595835f15c543b46074ca2 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Tue, 10 Dec 2024 23:43:41 +0100 Subject: [PATCH 15/27] modify --- .github/workflows/terraform-plan.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index d929b4b..f5d9bbb 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -66,7 +66,7 @@ jobs: api-key: ${{ secrets.INFRACOST_API_KEY }} - name: Run Infracost run: | - infracost breakdown --path=../tfplan.json --format=json --out-file=/tmp/infracost.json && pwd && cat /tmp/infracost.json + infracost breakdown --path=./terraform --format=json --out-file=/tmp/infracost.json && pwd && cat /tmp/infracost.json # Checkout the branch you want Infracost to compare costs against, most commonly the target branch. - name: Checkout base branch From 4d737b0ea9ebe37d6a5ba40a60bdc1deb7276407 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Wed, 11 Dec 2024 00:22:30 +0100 Subject: [PATCH 16/27] modify --- .github/workflows/terraform-plan.yml | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index f5d9bbb..9451988 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -15,17 +15,8 @@ permissions: env: AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - TF_VAR_aws_region: ${{ vars.TF_AWS_REGION }} - TF_VAR_ami_id: ${{ vars.TF_AMI_ID }} - TF_VAR_instance_type: ${{ vars.TF_INSTANCE_TYPE }} - TF_VAR_key_pair_name: ${{ vars.TF_KEY_PAIR_NAME }} TF_VAR_private_key: ${{ secrets.PRIVATE_KEY }} - TF_VAR_domain_name: ${{ vars.TF_DOMAIN_NAME }} - TF_VAR_frontend_domain: ${{ vars.TF_FRONTEND_DOMAIN }} - TF_VAR_db_domain: ${{ vars.TF_DB_DOMAIN }} - TF_VAR_traefik_domain: ${{ vars.TF_TRAEFIK_DOMAIN }} TF_VAR_cert_email: ${{ secrets.TF_CERT_EMAIL }} - TF_VAR_private_key_path: ./${{ vars.TF_KEY_PAIR_NAME }}.pem jobs: @@ -39,8 +30,8 @@ jobs: - name: Write Private Key to File run: | - echo "${{ secrets.PRIVATE_KEY }}" > ${{ vars.TF_KEY_PAIR_NAME }}.pem - chmod 600 ${{ vars.TF_KEY_PAIR_NAME }}.pem + echo "${{ secrets.PRIVATE_KEY }}" > private_key.pem + chmod 600 private_key.pem - name: Setup Terraform uses: hashicorp/setup-terraform@v2 @@ -52,7 +43,18 @@ jobs: - name: Terraform Plan id: plan - run: terraform plan -out=tfplan.out + run: | + terraform plan \ + -out=tfplan.out \ + -var="aws_region=us-east-1" \ + -var="ami_id=ami-005fc0f236362e99f" \ + -var="instance_type=t3.medium" \ + -var="key_pair_name=private_key" \ + -var="private_key_path=../private_key.pem" \ + -var="domain_name=drintech.online" \ + -var="frontend_domain=cv1.drintech.online" \ + -var="db_domain=db.cv1.drintech.online" \ + -var="traefik_domain=traefik.cv1.drintech.online" \ working-directory: ./terraform - name: Save Plan JSON From f715dbde4b844bcd0b66ddcd41810c626f7d1a46 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Wed, 11 Dec 2024 00:26:35 +0100 Subject: [PATCH 17/27] revert --- .github/workflows/terraform-plan.yml | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index 9451988..f5d9bbb 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -15,8 +15,17 @@ permissions: env: AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }} AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + TF_VAR_aws_region: ${{ vars.TF_AWS_REGION }} + TF_VAR_ami_id: ${{ vars.TF_AMI_ID }} + TF_VAR_instance_type: ${{ vars.TF_INSTANCE_TYPE }} + TF_VAR_key_pair_name: ${{ vars.TF_KEY_PAIR_NAME }} TF_VAR_private_key: ${{ secrets.PRIVATE_KEY }} + TF_VAR_domain_name: ${{ vars.TF_DOMAIN_NAME }} + TF_VAR_frontend_domain: ${{ vars.TF_FRONTEND_DOMAIN }} + TF_VAR_db_domain: ${{ vars.TF_DB_DOMAIN }} + TF_VAR_traefik_domain: ${{ vars.TF_TRAEFIK_DOMAIN }} TF_VAR_cert_email: ${{ secrets.TF_CERT_EMAIL }} + TF_VAR_private_key_path: ./${{ vars.TF_KEY_PAIR_NAME }}.pem jobs: @@ -30,8 +39,8 @@ jobs: - name: Write Private Key to File run: | - echo "${{ secrets.PRIVATE_KEY }}" > private_key.pem - chmod 600 private_key.pem + echo "${{ secrets.PRIVATE_KEY }}" > ${{ vars.TF_KEY_PAIR_NAME }}.pem + chmod 600 ${{ vars.TF_KEY_PAIR_NAME }}.pem - name: Setup Terraform uses: hashicorp/setup-terraform@v2 @@ -43,18 +52,7 @@ jobs: - name: Terraform Plan id: plan - run: | - terraform plan \ - -out=tfplan.out \ - -var="aws_region=us-east-1" \ - -var="ami_id=ami-005fc0f236362e99f" \ - -var="instance_type=t3.medium" \ - -var="key_pair_name=private_key" \ - -var="private_key_path=../private_key.pem" \ - -var="domain_name=drintech.online" \ - -var="frontend_domain=cv1.drintech.online" \ - -var="db_domain=db.cv1.drintech.online" \ - -var="traefik_domain=traefik.cv1.drintech.online" \ + run: terraform plan -out=tfplan.out working-directory: ./terraform - name: Save Plan JSON From 084bd6665a50587976ddfb9bd29f2c5f8147008c Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Wed, 11 Dec 2024 00:34:21 +0100 Subject: [PATCH 18/27] modify --- .github/workflows/terraform-plan.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index f5d9bbb..8720b36 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -66,7 +66,7 @@ jobs: api-key: ${{ secrets.INFRACOST_API_KEY }} - name: Run Infracost run: | - infracost breakdown --path=./terraform --format=json --out-file=/tmp/infracost.json && pwd && cat /tmp/infracost.json + infracost breakdown --path=./terraform --format=json --out-file=/tmp/infracost-new.json && pwd && cat /tmp/infracost-new.json # Checkout the branch you want Infracost to compare costs against, most commonly the target branch. - name: Checkout base branch @@ -77,10 +77,9 @@ jobs: - name: Run Infracost shell: bash run: | - infracost breakdown --path=./terraform --format=json --out-file=/tmp/infracost-base.json && pwd && cat /tmp/infracost-base.json - infracost diff --path=../tfplan.json \ + infracost diff --path=./terraform \ --format=json \ - --compare-to=/tmp/infracost-base.json \ + --compare-to=/tmp/infracost-new.json \ --out-file=/tmp/infracost.json && cat /tmp/infracost.json - name: Post Infracost Comment @@ -89,4 +88,4 @@ jobs: --repo=$GITHUB_REPOSITORY \ --github-token=${{ github.token }} \ --pull-request=${{ github.event.pull_request.number }} \ - --behavior=update + --behavior=update \ No newline at end of file From a9b2b0343223371f520403417e2eed597baaa059 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Wed, 11 Dec 2024 00:56:21 +0100 Subject: [PATCH 19/27] modify --- .github/workflows/terraform-plan.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index 8720b36..40361e0 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -77,10 +77,11 @@ jobs: - name: Run Infracost shell: bash run: | - infracost diff --path=./terraform \ + infracost breakdown --path=/tmp/infracost-new.json --format=json --out-file=/tmp/infracost-base.json && pwd + infracost diff --path=../tfplan.json \ --format=json \ - --compare-to=/tmp/infracost-new.json \ - --out-file=/tmp/infracost.json && cat /tmp/infracost.json + --compare-to=/tmp/infracost-base.json \ + --out-file=/tmp/infracost.json - name: Post Infracost Comment run: | From 8c2916cdf58eca243babf3af38459adb34a52549 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Wed, 11 Dec 2024 01:03:32 +0100 Subject: [PATCH 20/27] modify --- .github/workflows/terraform-plan.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index 40361e0..c448307 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -77,7 +77,7 @@ jobs: - name: Run Infracost shell: bash run: | - infracost breakdown --path=/tmp/infracost-new.json --format=json --out-file=/tmp/infracost-base.json && pwd + infracost breakdown --path=./terraform --format=json --out-file=/tmp/infracost-base.json && pwd infracost diff --path=../tfplan.json \ --format=json \ --compare-to=/tmp/infracost-base.json \ From a53a62f8d6728d241ec938f8e5292d4aef2ce8a6 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Wed, 11 Dec 2024 01:17:49 +0100 Subject: [PATCH 21/27] update yml --- .github/workflows/terraform-plan.yml | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index c448307..4ea59cc 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -64,9 +64,6 @@ jobs: uses: infracost/actions/setup@v3 with: api-key: ${{ secrets.INFRACOST_API_KEY }} - - name: Run Infracost - run: | - infracost breakdown --path=./terraform --format=json --out-file=/tmp/infracost-new.json && pwd && cat /tmp/infracost-new.json # Checkout the branch you want Infracost to compare costs against, most commonly the target branch. - name: Checkout base branch @@ -78,10 +75,16 @@ jobs: shell: bash run: | infracost breakdown --path=./terraform --format=json --out-file=/tmp/infracost-base.json && pwd - infracost diff --path=../tfplan.json \ - --format=json \ - --compare-to=/tmp/infracost-base.json \ - --out-file=/tmp/infracost.json + + # Checkout the current PR branch so we can create a diff. + - name: Checkout PR branch + uses: actions/checkout@v4 + - name: Generate Infracost diff + run: | + infracost diff --path=./terraform \ + --format=json \ + --compare-to=/tmp/infracost-base.json \ + --out-file=/tmp/infracost.json - name: Post Infracost Comment run: | From 125e3654e69d2892e8ac871356b2cdd969dc6865 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Wed, 11 Dec 2024 02:09:31 +0100 Subject: [PATCH 22/27] modify --- .github/workflows/terraform-plan.yml | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index 4ea59cc..e276cef 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -57,7 +57,7 @@ jobs: - name: Save Plan JSON id: save-plan - run: terraform show -json tfplan.out > ../../tfplan.json && pwd + run: terraform show -json tfplan.out > /tmp/tfplan.txt working-directory: ./terraform - name: Setup Infracost @@ -92,4 +92,9 @@ jobs: --repo=$GITHUB_REPOSITORY \ --github-token=${{ github.token }} \ --pull-request=${{ github.event.pull_request.number }} \ - --behavior=update \ No newline at end of file + --behavior=update + - name: Comment PR + uses: thollander/actions-comment-pull-request@v3 + with: + file-path: /tmp/tfplan.txt + pr-number: ${{ github.event.pull_request.number }} \ No newline at end of file From 31a9258ea4acfbcddd4fb3dcc3edcf662d61e505 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Wed, 11 Dec 2024 02:23:37 +0100 Subject: [PATCH 23/27] adjust --- .github/workflows/terraform-plan.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index e276cef..3f57efa 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -57,7 +57,7 @@ jobs: - name: Save Plan JSON id: save-plan - run: terraform show -json tfplan.out > /tmp/tfplan.txt + run: terraform show -no-color tfplan.out > /tmp/tfplan.txt working-directory: ./terraform - name: Setup Infracost @@ -72,15 +72,15 @@ jobs: ref: '${{ github.event.pull_request.base.ref }}' - name: Run Infracost - shell: bash run: | - infracost breakdown --path=./terraform --format=json --out-file=/tmp/infracost-base.json && pwd + infracost breakdown --path=./terraform --format=json --out-file=/tmp/infracost-base.json # Checkout the current PR branch so we can create a diff. - name: Checkout PR branch uses: actions/checkout@v4 - name: Generate Infracost diff run: | + infracost breakdown --path=./terraform --format=table --out-file=/tmp/infracost-new.txt infracost diff --path=./terraform \ --format=json \ --compare-to=/tmp/infracost-base.json \ @@ -97,4 +97,5 @@ jobs: uses: thollander/actions-comment-pull-request@v3 with: file-path: /tmp/tfplan.txt + file-path: /tmp/infracost-new.txt pr-number: ${{ github.event.pull_request.number }} \ No newline at end of file From 43071d3ec60715062c240eb75b7e995bc79f9115 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Wed, 11 Dec 2024 02:32:05 +0100 Subject: [PATCH 24/27] add comment-pr to workflow --- .github/workflows/terraform-plan.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index 3f57efa..c765091 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -97,5 +97,11 @@ jobs: uses: thollander/actions-comment-pull-request@v3 with: file-path: /tmp/tfplan.txt + pr-number: ${{ github.event.pull_request.number }} + comment-tag: comment-output + - name: Comment PR + uses: thollander/actions-comment-pull-request@v3 + with: file-path: /tmp/infracost-new.txt - pr-number: ${{ github.event.pull_request.number }} \ No newline at end of file + pr-number: ${{ github.event.pull_request.number }} + comment-tag: comment-output \ No newline at end of file From edbe1946712d62a9600598b2d2bdf8ef7bd8540b Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Wed, 11 Dec 2024 02:51:24 +0100 Subject: [PATCH 25/27] add pr comment update --- .github/workflows/terraform-plan.yml | 48 +++++++++++++++++++++------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index c765091..fb68492 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -93,15 +93,41 @@ jobs: --github-token=${{ github.token }} \ --pull-request=${{ github.event.pull_request.number }} \ --behavior=update - - name: Comment PR - uses: thollander/actions-comment-pull-request@v3 + - name: Update PR Comment + uses: actions/github-script@v6 + if: github.event_name == 'pull_request' + env: + PLAN: ${{ steps.plan.outcome }} with: - file-path: /tmp/tfplan.txt - pr-number: ${{ github.event.pull_request.number }} - comment-tag: comment-output - - name: Comment PR - uses: thollander/actions-comment-pull-request@v3 - with: - file-path: /tmp/infracost-new.txt - pr-number: ${{ github.event.pull_request.number }} - comment-tag: comment-output \ No newline at end of file + github-token: ${{ secrets.GITHUB_TOKEN }} + script: | + const fs = require('fs'); + const plan = fs.readFileSync('/tmp/tfplan.txt', 'utf8'); + const infracost = fs.readFileSync('/tmp/infracost-new.txt', 'utf8'); + + const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\` + #### Terraform Plan 📖\`${{ steps.plan.outcome }}\` + +
Show Plan + + \`\`\`hcl + ${plan} + \`\`\` + +
+ + #### New Infracost Breakdown 💰 +
Show Breakdown + \`\`\` + ${infracost} + \`\`\` +
+ + *Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`; + + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: output + }) \ No newline at end of file From d2e75e276789787190418a5bf2b5f1e02f27eba7 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Wed, 11 Dec 2024 02:57:45 +0100 Subject: [PATCH 26/27] modify pr comment --- .github/workflows/terraform-plan.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index fb68492..36d91ca 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -118,7 +118,7 @@ jobs: #### New Infracost Breakdown 💰
Show Breakdown - \`\`\` + \`\`\`sh ${infracost} \`\`\`
From 44f11c0a28e4a5a75030039f5f01aebea5fc9543 Mon Sep 17 00:00:00 2001 From: Okesanya Odunayo <94924061+DrInTech22@users.noreply.github.com> Date: Wed, 11 Dec 2024 03:02:42 +0100 Subject: [PATCH 27/27] update --- .github/workflows/terraform-plan.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/terraform-plan.yml b/.github/workflows/terraform-plan.yml index 36d91ca..25bc579 100644 --- a/.github/workflows/terraform-plan.yml +++ b/.github/workflows/terraform-plan.yml @@ -118,9 +118,11 @@ jobs: #### New Infracost Breakdown 💰
Show Breakdown + \`\`\`sh ${infracost} \`\`\` +
*Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`;