FMT and PLAN IaC on feature branch #20
This file contains hidden or 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
| name: AWS IAC | |
| run-name: FMT and PLAN IaC on feature branch | |
| on: | |
| pull_request: | |
| types: [opened, reopened, ready_for_review, synchronize] | |
| paths: | |
| - 'core-env/**' | |
| - '.github/workflows/aws*' | |
| permissions: | |
| id-token: write | |
| pull-requests: write | |
| jobs: | |
| core_aws: | |
| outputs: | |
| plan: ${{ steps.plan_outcome.outputs.output }} | |
| fmt: ${{ steps.fmt_outcome.outputs.output }} | |
| defaults: | |
| run: | |
| working-directory: core-env/aws | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v3 | |
| - name: Configure AWS Credentials | |
| uses: aws-actions/configure-aws-credentials@main # Or a specific version | |
| with: | |
| role-to-assume: arn:aws:iam::218691292270:role/sedemo-iac-pipeline-role | |
| aws-region: us-west-2 | |
| - name: Verify AWS identity | |
| run: | | |
| # Your commands that require AWS credentials | |
| aws sts get-caller-identity | |
| - name: Verify Tofu | |
| uses: opentofu/setup-opentofu@v1 | |
| - name: OpenTofu fmt | |
| id: fmt | |
| run: tofu fmt -check > /tmp/fmt.out | |
| continue-on-error: true | |
| - name: fmt output | |
| id: fmt_outcome | |
| run: echo "output=${{steps.fmt.outcome}}" >> "$GITHUB_OUTPUT" | |
| - run: tofu init | |
| - id: plan | |
| run: tofu plan -no-color > /tmp/plan.out | |
| continue-on-error: true | |
| - name: plan output | |
| id: plan_outcome | |
| run: echo "output=${{steps.plan.outcome}}" >> "$GITHUB_OUTPUT" | |
| - name: Upload plan | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: output-aws | |
| path: /tmp/*.out | |
| eks_primary: | |
| needs: core_aws | |
| outputs: | |
| plan: ${{ steps.plan_outcome.outputs.output }} | |
| fmt: ${{ steps.fmt_outcome.outputs.output }} | |
| defaults: | |
| run: | |
| working-directory: core-env/eks-clusters | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v3 | |
| - name: Configure AWS Credentials | |
| uses: aws-actions/configure-aws-credentials@main # Or a specific version | |
| with: | |
| role-to-assume: arn:aws:iam::218691292270:role/sedemo-iac-pipeline-role | |
| aws-region: us-west-2 | |
| - name: Verify AWS identity | |
| run: | | |
| # Your commands that require AWS credentials | |
| aws sts get-caller-identity | |
| - name: Verify Tofu | |
| uses: opentofu/setup-opentofu@v1 | |
| - name: OpenTofu fmt | |
| id: fmt | |
| run: tofu fmt -check > /tmp/fmt.out | |
| continue-on-error: true | |
| - name: fmt output | |
| id: fmt_outcome | |
| run: echo "output=${{steps.fmt.outcome}}" >> "$GITHUB_OUTPUT" | |
| - run: tofu init | |
| - id: plan | |
| run: tofu plan -no-color > /tmp/plan.out | |
| continue-on-error: true | |
| - name: plan output | |
| id: plan_outcome | |
| run: echo "output=${{steps.plan.outcome}}" >> "$GITHUB_OUTPUT" | |
| - name: Upload plan | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: output-eks | |
| path: /tmp/*.out | |
| pr_comment: | |
| needs: | |
| - eks_primary | |
| - core_aws | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Download artifact | |
| uses: actions/download-artifact@v4 | |
| # - name: Display structure of downloaded files | |
| # run: | | |
| # mkdir output-aws | |
| # mkdir output-eks | |
| # echo "this is file content" >> output-aws/plan.out | |
| # echo "this is file content" >> output-eks/plan.out | |
| # ls -R . | |
| - uses: actions/github-script@v6 | |
| 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('OpenTofu Format and Style') | |
| }) | |
| // 2. Prepare format of the comment | |
| const fs = require('fs'); | |
| const awsCorePlan = fs.readFileSync('output-aws/plan.out', 'utf8'); | |
| const eksPrimaryPlan = fs.readFileSync('output-eks/plan.out', 'utf8'); | |
| const output = ` | |
| ### AWS Core | |
| #### OpenTofu Format and Style π\`${{ needs.core_aws.outputs.fmt }}\` | |
| #### OpenTofu Plan π \`${{ needs.core_aws.outputs.plan }}\` | |
| <details><summary>Show Plan</summary> | |
| \`\`\`\n | |
| ${awsCorePlan} | |
| \`\`\` | |
| </details> | |
| ### EKS Primary Cluster | |
| #### OpenTofu Format and Style π\`${{ needs.eks_primary.outputs.fmt }}\` | |
| #### OpenTofu Plan π \`${{ needs.eks_primary.outputs.plan }}\` | |
| <details><summary>Show Plan</summary> | |
| \`\`\`\n | |
| ${eksPrimaryPlan} | |
| \`\`\` | |
| </details> | |
| *Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, 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 | |
| }) | |
| } |