From d250628c35fbdb46eb325821537f9516bbdae10d Mon Sep 17 00:00:00 2001 From: Dave Arnold Date: Thu, 15 Aug 2024 08:46:55 -0700 Subject: [PATCH] chore: Refactor image pipeline module names and update workflows --- image-pipeline.tf | 37 +++++++++++++++ variables.tf | 0 workflows/terraform-apply.yaml.tpl | 16 ++++++- workflows/terraform-plan.yaml.tpl | 76 ++++++++++++++++++++++++++++-- 4 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 variables.tf diff --git a/image-pipeline.tf b/image-pipeline.tf index 7b15719..9a981c5 100644 --- a/image-pipeline.tf +++ b/image-pipeline.tf @@ -122,4 +122,41 @@ 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" + } + ) + } + ] } \ No newline at end of file diff --git a/variables.tf b/variables.tf new file mode 100644 index 0000000..e69de29 diff --git a/workflows/terraform-apply.yaml.tpl b/workflows/terraform-apply.yaml.tpl index dbdf241..c112c78 100644 --- a/workflows/terraform-apply.yaml.tpl +++ b/workflows/terraform-apply.yaml.tpl @@ -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 diff --git a/workflows/terraform-plan.yaml.tpl b/workflows/terraform-plan.yaml.tpl index bbf8248..c0854e1 100644 --- a/workflows/terraform-plan.yaml.tpl +++ b/workflows/terraform-plan.yaml.tpl @@ -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 }}\` +
Validation Output + + \`\`\`\n + ${{ steps.validate.outputs.stdout }} + \`\`\` + +
+ + #### Terraform Plan 📖\`${{ steps.plan.outcome }}\` + +
Show Plan + + \`\`\`\n + ${process.env.PLAN} + \`\`\` + +
+ + *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 + }) + } \ No newline at end of file