eddiewebb is testing out GitHub Actions π #6
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: ${{ github.actor }} is testing out GitHub Actions π | |
| on: | |
| pull_request: | |
| types: [opened, reopened, ready_for_review, synchronize] | |
| defaults: | |
| run: | |
| working-directory: core-env/aws | |
| permissions: | |
| id-token: write | |
| pull-requests: write | |
| jobs: | |
| core_aws: | |
| outputs: | |
| tofu-plan-aws: ${{ steps.plan_output.outputs.output }} | |
| tofu-fmt-aws: ${{ 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 | |
| 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 | |
| continue-on-error: true | |
| - name: plan output | |
| id: plan_output | |
| run: echo "output=${{steps.plan.output}}" >> "$GITHUB_OUTPUT" | |
| eks_primary: | |
| needs: core_aws | |
| outputs: | |
| tofu-plan-aws: ${{ steps.plan_output.outputs.output }} | |
| tofu-fmt-aws: ${{ 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 | |
| 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 | |
| continue-on-error: true | |
| - name: plan output | |
| id: plan_output | |
| run: echo "output=${{steps.plan.output}}" >> "$GITHUB_OUTPUT" | |
| pr_comment: | |
| needs: eks_primary | |
| runs-on: ubuntu-latest | |
| steps: | |
| - 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 output = ` | |
| #### OpenTofu Format and Style: AWS Core π\`${{ needs.core_aws.outputs.fmt }}\` | |
| #### OpenTofu Format and Style: EKS Primary Cluster π\`${{ needs.eks_primary.outputs.fmt }}\` | |
| #### OpenTofu Plan: AWS Core π | |
| <details><summary>Show Plan</summary> | |
| \`\`\`\n | |
| ${{ needs.core_aws.outputs.plan }} | |
| \`\`\` | |
| </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 | |
| }) | |
| } |