Merge pull request #7 from kunduso/terraform-import #21
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: terraform-import-aws-resource | |
| on: | |
| workflow_dispatch: | |
| push: | |
| branches: [ '*' ] | |
| paths: | |
| - 'terraform-import-aws-resource/**' | |
| - '.github/workflows/terraform-import.yml' | |
| pull_request: | |
| branches: ["main"] | |
| paths: | |
| - 'terraform-import-aws-resource/**' | |
| - '.github/workflows/terraform-import.yml' | |
| env: | |
| AWS_REGION: us-west-2 # set this to your preferred AWS region | |
| permissions: read-all | |
| jobs: | |
| terraform: | |
| name: 'terraform-import' | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: read | |
| id-token: write | |
| pull-requests: write | |
| defaults: | |
| run: | |
| shell: bash | |
| working-directory: ./terraform-import-aws-resource | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - name: Configure AWS Credentials Action For GitHub Actions | |
| uses: aws-actions/configure-aws-credentials@v5.1.0 | |
| with: | |
| role-to-assume: ${{ secrets.IAM_ROLE }} | |
| role-session-name: AWSSession | |
| aws-region: ${{ env.AWS_REGION }} | |
| - name: Setup Terraform | |
| uses: hashicorp/setup-terraform@v3 | |
| - name: Terraform Init | |
| id: init | |
| run: terraform init | |
| - name: Terraform Format | |
| id: fmt | |
| run: terraform fmt -check | |
| - name: Terraform Validate | |
| id: validate | |
| run: terraform validate -no-color | |
| - name: Terraform Plan | |
| id: plan | |
| if: github.ref != 'refs/heads/main' || github.event_name == 'pull_request' | |
| run: terraform plan -no-color -input=false | |
| continue-on-error: true | |
| - name: Post Terraform Plan Output | |
| uses: actions/github-script@v6 | |
| if: github.event_name == 'pull_request' | |
| env: | |
| PLAN: "terraform\n${{ steps.plan.outputs.stdout }}" | |
| with: | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| script: | | |
| const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\` | |
| #### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\` | |
| #### Terraform Plan 📖\`${{ steps.plan.outcome }}\` | |
| #### Terraform Validation 🤖\`${{ steps.validate.outcome }}\` | |
| <details><summary>Show Plan</summary> | |
| \`\`\`\n | |
| ${process.env.PLAN} | |
| \`\`\` | |
| </details> | |
| *Pushed by: @${{ github.actor }}, Action: \`${{ github.event_name }}\`*`; | |
| github.rest.issues.createComment({ | |
| issue_number: context.issue.number, | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| body: output | |
| }) | |
| - name: Terraform Apply | |
| if: github.ref == 'refs/heads/main' | |
| run: terraform apply -auto-approve -input=false -no-color |