-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reuse workflow, improve tf cloud usage (#11)
- Loading branch information
Showing
13 changed files
with
86 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: Terraform Apply | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
workflow_dispatch: | ||
|
||
jobs: | ||
terraform_apply: | ||
name: Terraform Apply | ||
uses: soat-tech-challenge/github-workflows/.github/workflows/terraform-apply.yml@main | ||
secrets: inherit | ||
with: | ||
cloud_workspace: database-staging |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,128 +9,22 @@ on: | |
jobs: | ||
tflint: | ||
name: TFLint | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup TFLint | ||
uses: terraform-linters/setup-tflint@v3 | ||
|
||
- name: Init TFLint | ||
run: tflint --init | ||
env: | ||
# https://github.com/terraform-linters/tflint/blob/master/docs/user-guide/plugins.md#avoiding-rate-limiting | ||
GITHUB_TOKEN: ${{ github.token }} | ||
|
||
- name: Run TFLint | ||
run: tflint -f compact | ||
uses: soat-tech-challenge/github-workflows/.github/workflows/tflint.yml@main | ||
|
||
tfsec: | ||
name: tfsec | ||
runs-on: ubuntu-latest | ||
uses: soat-tech-challenge/github-workflows/.github/workflows/tfsec.yml@main | ||
|
||
permissions: | ||
contents: read | ||
pull-requests: write | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: tfsec | ||
uses: aquasecurity/[email protected] | ||
with: | ||
tfsec_args: --soft-fail | ||
github_token: ${{ github.token }} | ||
|
||
terraform-cloud-speculative-run: | ||
name: Terraform Cloud Speculative Run | ||
runs-on: ubuntu-latest | ||
terraform-plan: | ||
name: Terraform Plan | ||
uses: soat-tech-challenge/github-workflows/.github/workflows/terraform-plan.yml@main | ||
secrets: inherit | ||
with: | ||
cloud_workspace: database-staging | ||
|
||
permissions: | ||
contents: read | ||
pull-requests: write | ||
|
||
env: | ||
TF_CLOUD_ORGANIZATION: ${{ vars.TF_CLOUD_ORGANIZATION }} | ||
TF_API_TOKEN: ${{ secrets.TF_API_TOKEN }} | ||
TF_WORKSPACE: ${{ vars.TF_WORKSPACE }} | ||
CONFIG_DIRECTORY: ${{ vars.CONFIG_DIRECTORY }} | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Upload Configuration | ||
uses: hashicorp/tfc-workflows-github/actions/[email protected] | ||
id: upload | ||
with: | ||
workspace: ${{ env.TF_WORKSPACE }} | ||
directory: ${{ env.CONFIG_DIRECTORY }} | ||
speculative: true | ||
|
||
- name: Create Plan Run | ||
uses: hashicorp/tfc-workflows-github/actions/[email protected] | ||
id: run | ||
## run may fail, if so continue to output PR comment | ||
## step.terraform-cloud-check-run-status will fail job after pr comment is created/updated. | ||
continue-on-error: true | ||
with: | ||
workspace: ${{ env.TF_WORKSPACE }} | ||
configuration_version: ${{ steps.upload.outputs.configuration_version_id }} | ||
plan_only: true | ||
|
||
- name: Get Plan Output | ||
uses: hashicorp/tfc-workflows-github/actions/[email protected] | ||
id: plan-output | ||
with: | ||
plan: ${{ steps.run.outputs.plan_id }} | ||
|
||
- name: Update PR with Plan comment | ||
uses: actions/github-script@v6 | ||
if: github.event_name == 'pull_request' | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
// 1. Retrieve existing bot comments for the PR | ||
const { data: comments } = await github.rest.issues.listComments({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
issue_number: context.issue.number, | ||
}) | ||
const botComment = comments.find(comment => { | ||
return comment.user.type === 'Bot' && comment.body.includes('Terraform Cloud Plan Output') | ||
}) | ||
const output = `#### Terraform Cloud Plan Output | ||
\`\`\` | ||
Plan: ${{ steps.plan-output.outputs.add }} to add, ${{ steps.plan-output.outputs.change }} to change, ${{ steps.plan-output.outputs.destroy }} to destroy. | ||
\`\`\` | ||
[Terraform Cloud Plan](${{ steps.run.outputs.run_link }}) | ||
` | ||
// 3. If we have a comment, update it, otherwise create a new one | ||
if (botComment) { | ||
github.rest.issues.updateComment({ | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
comment_id: botComment.id, | ||
body: output | ||
}) | ||
} else { | ||
github.rest.issues.createComment({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: output | ||
}) | ||
} | ||
## Check Run Status, if not planned_and_finished fail the job | ||
- id: terraform-cloud-check-run-status | ||
if: ${{ steps.run.outputs.run_status != 'planned_and_finished'}} | ||
run: | | ||
echo "Terraform Cloud Run Failed or Requires Further Attention" | ||
echo "Run Status: '${{ steps.run.outputs.run_status }}'" | ||
echo "${{ steps.run.outputs.run_link }}" | ||
exit 1 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
resource "aws_security_group" "this" { | ||
name = "soat-tc-rds-sg" | ||
vpc_id = data.aws_vpc.vpc.id | ||
|
||
ingress { | ||
from_port = var.db_port | ||
to_port = var.db_port | ||
protocol = "tcp" | ||
cidr_blocks = ["0.0.0.0/0"] | ||
} | ||
} | ||
|
||
resource "aws_db_subnet_group" "this" { | ||
name = "soat-tc-rds-subnet-group" | ||
subnet_ids = data.aws_subnets.private_subnets.ids | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
output "rds_instance_id" { | ||
description = "The RDS instance identifier" | ||
value = aws_db_instance.soat_tc_rds_db.identifier | ||
value = aws_db_instance.this.name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
AWS_ACCESS_KEY = "" | ||
AWS_SECRET_KEY = "" | ||
aws_secret_key = "" | ||
aws_access_key = "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters