Skip to content

Commit

Permalink
adding terraform plan to PR output
Browse files Browse the repository at this point in the history
  • Loading branch information
arnol377 committed Aug 15, 2024
2 parents 8b87e90 + d250628 commit d3bb629
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 31 deletions.
62 changes: 37 additions & 25 deletions image-pipeline.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,6 @@ locals {
]
}

moved {
from = module.linux_image_pipeline
to = module.image_pipeline_repos["linux-image-pipeline"]
}

moved {
from = module.win_image_pipeline
to = module.image_pipeline_repos["windows-image-pipeline"]
}

moved {
from = module.goss-testing
to = module.image_pipeline_repos["image-pipeline-goss-testing"]
}

moved {
from = module.image_pipeline_ansible_playbooks
to = module.image_pipeline_repos["image-pipeline-ansible-playbooks"]
}

moved {
from = module.image_pipeline
to = module.aws_image_pipeline
}

module "image_pipeline_repos" {
for_each = toset(local.pipeline_repos)
source = "git@github.e.it.census.gov:CSVD/terraform-github-repo"
Expand Down Expand Up @@ -120,3 +95,40 @@ module "aws_image_pipeline" {
}
]
}


# image-pipeline
module "terraform_aws_image_pipeline" {
source = "git@github.e.it.census.gov:CSVD/terraform-github-repo"
#github_codeowners_team = "CSVD"
github_repo_description = "Terraform Module that creates codepipeline and codebuild jobs and other resources for building and deploying images"
repo_org = "arnol377"
name = "terraform-aws-image-pipeline"
github_repo_topics = [
"terraform"
]
force_name = true
create_codeowners = false
enforce_prs = true
collaborators = local.collaborators
admin_teams = [github_team.team.name]
pull_request_bypassers = local.pull_request_bypassers
vars = [
{
name = "terraform_version"
value = "1.9.1"
}
]
extra_files = [
{
path = ".github/workflows/terraform-plan.yaml"
content = templatefile(
"${path.module}/workflows/terraform-plan.yaml.tpl",
{
repo_name = "aws-image-pipeline"
directory = "./examples"
}
)
}
]
}
Empty file added variables.tf
Empty file.
16 changes: 15 additions & 1 deletion workflows/terraform-apply.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,41 @@ jobs:

- uses: CSVD/gh-actions-setup-terraform@v2
with:
terraform_wrapper: false
terraform_version: $${{ vars.terraform_version }}

- name: Terraform Format
id: fmt
%{ if directory != null }
working-directory: ${directory}
%{ endif }
run: |
terraform fmt -check

- name: Autoformat Halt
if: env.auto_format == 'true'
%{ if directory != null }
working-directory: ${directory}
%{ endif }
run: exit 1

- name: Terraform Init
id: init
%{ if directory != null }
working-directory: ${directory}
%{ endif }
run: terraform init -upgrade

- name: Terraform Validate
id: validate
%{ if directory != null }
working-directory: ${directory}
%{ endif }
run: terraform validate

- name: Terraform Apply
id: apply
%{ if directory != null }
working-directory: ${directory}
%{ endif }
run: terraform apply -auto-approve
continue-on-error: true
76 changes: 71 additions & 5 deletions workflows/terraform-plan.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,93 @@ jobs:

- uses: CSVD/gh-actions-setup-terraform@v2
with:
terraform_wrapper: false
terraform_version: $${{ vars.terraform_version }}

- name: Terraform Format
id: fmt
continue-on-error: true
%{ if directory != null }
working-directory: ${directory}
%{ endif }
run: |
terraform fmt -check

- name: Autoformat Halt
if: env.auto_format == 'true'
run: exit 1

- name: Terraform Init
id: init
%{ if directory != null }
working-directory: ${directory}
%{ endif }
run: terraform init -upgrade

- name: Terraform Validate
id: validate
%{ if directory != null }
working-directory: ${directory}
%{ endif }
run: terraform validate

- name: Terraform Plan
id: plan
%{ if directory != null }
working-directory: ${directory}
%{ endif }
run: terraform plan
continue-on-error: true

- uses: actions/github-script@v7
if: github.event_name == 'pull_request'
env:
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}"
with:
github-token: ${{ secrets.GH_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 Format and Style')
})

// 2. Prepare format of the comment
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\`
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\`
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\`
<details><summary>Validation Output</summary>

\`\`\`\n
${{ steps.validate.outputs.stdout }}
\`\`\`

</details>

#### Terraform Plan 📖\`${{ steps.plan.outcome }}\`

<details><summary>Show Plan</summary>

\`\`\`\n
${process.env.PLAN}
\`\`\`

</details>

*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ env.tf_actions_working_dir }}\`, Workflow: \`${{ github.workflow }}\`*`;

// 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
})
}

0 comments on commit d3bb629

Please sign in to comment.