diff --git a/.github/workflows/_terraform-plan-pr-comment.yml b/.github/workflows/_terraform-plan-pr-comment.yml new file mode 100644 index 00000000..eae92f8a --- /dev/null +++ b/.github/workflows/_terraform-plan-pr-comment.yml @@ -0,0 +1,103 @@ +name: Add Terraform CDK plan as comment on pull request + +on: + workflow_call: + inputs: + deploy-env: + required: true + type: string + +env: + DEPLOY_ENV: ${{ inputs.deploy-env }} + +jobs: + terraform: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Read node version from `.nvmrc` file + id: nvmrc + shell: bash + run: echo ::set-output name=NODE_VERSION::$(cat .nvmrc) + + - name: Install required node.js version + uses: actions/setup-node@v3 + with: + node-version: ${{ steps.nvmrc.outputs.NODE_VERSION }} + + - name: Install pnpm + uses: pnpm/action-setup@v2 + id: pnpm-install + with: + version: 8 + run_install: false + + - name: Get pnpm store directory + id: pnpm-cache + shell: bash + run: | + echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT + + - uses: actions/cache@v3 + name: Setup pnpm cache + with: + path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} + key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} + restore-keys: | + ${{ runner.os }}-pnpm-store- + + - name: Install dependencies + run: pnpm install + + - name: Initialize Terraform CDK configuration + shell: bash + run: | + cd infra + pnpm cdktf get + pnpm build:tsc + + - name: Install CloudFoundry CLI + run: | + curl -L "https://packages.cloudfoundry.org/stable?release=linux64-binary&version=v8&source=github" | sudo tar -zx --directory=/usr/local/bin + cf --version + + - name: Configure CloudFoundry API endpoint + run: | + cf api https://api.fr.cloud.gov + + - name: Show Terraform plan + id: show_plan + env: + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + shell: bash + run: | + output=$(DEPLOY_ENV=${DEPLOY_ENV} pnpm cdktf diff --no-color --app "npx ts-node src/index.ts") + echo "${output}" + echo "::set-output name=stdout::$output" + working-directory: infra + + - name: Find Comment + uses: peter-evans/find-comment@v3 + id: find_comment + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body-includes: '' + + - name: Create or update PR comment with Terraform plan + uses: peter-evans/create-or-update-comment@v4 + with: + comment-id: ${{ steps.find_comment.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + token: ${{ secrets.GITHUB_TOKEN }} + repository: ${{ github.repository }} + body: | + + **Terraform Plan** + ```terraform + ${{ steps.show_plan.outputs.stdout }} + ``` + edit-mode: replace diff --git a/.github/workflows/add-terraform-plan-to-pr.yml b/.github/workflows/add-terraform-plan-to-pr.yml new file mode 100644 index 00000000..e0549bc9 --- /dev/null +++ b/.github/workflows/add-terraform-plan-to-pr.yml @@ -0,0 +1,18 @@ +name: Add Terraform CDK plan as comment on pull request + +on: + pull_request: + branches: + - staging + - main + types: + - opened + - synchronize + - reopened + +jobs: + add-terraform-plan-to-staging-pr: + uses: ./.github/workflows/_terraform-plan-pr-comment.yml + secrets: inherit + with: + deploy-env: staging