Skip to content

Commit

Permalink
Refactor Terraform workflow to use GitHub Actions setup and cache
Browse files Browse the repository at this point in the history
This commit refactors the Terraform workflow to use the GitHub Actions setup and cache. It removes the unnecessary steps for checking out the repository and blowing up the .terraform directory. It also adds the setup for AWS and GitHub credentials. The Terraform init and plan steps are now using the CSVD/terraform-init and CSVD/terraform-plan actions, respectively, with specific versions and workspace configurations.
  • Loading branch information
gomez385 committed Oct 7, 2024
1 parent 1050676 commit c6ea55a
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions .github/workflows/terraform_plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ jobs:

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3

- uses: CSVD/gh-actions-setup-node@v3
with:
node-version: 16

- name: blow up .terraform
run: rm -rf ${{ github.workspace }}/.terraform || echo "nope"

- name: Setup AWS Credentials
id: aws_credentials
run: |
Expand All @@ -48,25 +38,27 @@ jobs:
echo AWS_SECRET_ACCESS_KEY=`jq -r '.SecretAccessKey' aws_credentials.json` >> $GITHUB_ENV
aws configure set aws_session_token `jq -r '.Token' aws_credentials.json`
echo AWS_SESSION_TOKEN=`jq -r '.Token' aws_credentials.json` >> $GITHUB_ENV
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it

- name: Setup GITHUB Credentials
id: github_credentials
run: |
echo GITHUB_TOKEN=$(python encode_jwt.py "$GITHUB_APP_PEM_FILE" "$GITHUB_APP_INSTALLATION_ID" "$GITHUB_BASE_URL") >> $GITHUB_ENV
- name: Terraform Init
id: init
run: /opt/tfenv/bin/terraform init -upgrade

- name: Terraform Validate
id: validate
run: /opt/tfenv/bin/terraform validate
uses: CSVD/terraform-init@main
id: terraform_init
with:
terraform_version: "1.9.1"
workspace: ${{ vars.terraform_workspace }}
setup_terraform: true
terraform_init: true

- name: Terraform Plan
id: plan
run: /opt/tfenv/bin/terraform plan -var-file=varfiles/${{ vars.terraform_workspace }}.tfvars

# - name: Terraform Apply
# id: plan
# run: /opt/tfenv/bin/terraform apply -auto-approve -var-file=varfiles/${{ vars.terraform_workspace }}.tfvars
# continue-on-error: true
uses: CSVD/terraform-plan@main
with:
terraform_version: "1.9.1"
workspace: ${{ vars.terraform_workspace }}
commit_sha: ${{ steps.terraform_init.outputs.commit_sha }}
varfile: varfiles/${{ vars.terraform_workspace }}.tfvars
download_cache: true

0 comments on commit c6ea55a

Please sign in to comment.