workflow changes #9
This file contains 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 Workflow | |
on: | |
push: | |
branches: | |
- main | |
paths: | |
- terraform/** | |
pull_request: | |
branches: | |
- main | |
paths: | |
- terraform/** | |
jobs: | |
terraform: | |
name: "Terraform deploy" | |
runs-on: ubuntu-latest | |
defaults: | |
run: | |
shell: bash | |
# We keep Terraform files in the terraform directory. | |
working-directory: ./terraform | |
steps: | |
- name: Checkout the repository to the runner | |
uses: actions/checkout@v2 | |
- name: Setup Terraform with specified version on the runner | |
uses: hashicorp/setup-terraform@v3 | |
with: | |
terraform_version: 1.5.7 | |
terraform_wrapper: false | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v2 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ${{ secrets.AWS_REGION }} | |
aws-session-token: ${{ secrets.AWS_SESSION_TOKEN }} | |
- 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 | |
- name: Terraform plan | |
id: plan | |
if: github.event_name == 'pull_request' | |
run: terraform plan -no-color -input=false | |
continue-on-error: true | |
- uses: actions/github-script@v6 | |
if: github.event_name == 'pull_request' | |
env: | |
PLAN: "terraform\n${{ steps.plan.outputs.stdout }}" | |
with: | |
script: | | |
const output = `#### Terraform Format and Style 🖌\`${{ steps.fmt.outcome }}\` | |
#### Terraform Initialization ⚙️\`${{ steps.init.outcome }}\` | |
#### Terraform Validation 🤖\`${{ steps.validate.outcome }}\` | |
#### Terraform Plan 📖\`${{ steps.plan.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 Plan Status | |
if: steps.plan.outcome == 'failure' | |
run: exit 1 | |
- name: Terraform Apply | |
if: github.ref == 'refs/heads/main' && github.event_name == 'push' | |
run: terraform apply -auto-approve -input=false | |
- name: Capture Terraform Output | |
id: tf_output | |
run: | | |
terraform output -raw eks_connect > eks_connect.txt | |
- name: Upload Terraform Output Artifact | |
uses: actions/upload-artifact@v4 | |
with: | |
name: terraform-output | |
path: ./eks_connect.txt |