Skip to content

Commit

Permalink
[FEAT] add if statement for running terraform
Browse files Browse the repository at this point in the history
  • Loading branch information
ohksj77 committed Mar 16, 2024
1 parent 8b649a0 commit e4b3ec2
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions .github/workflows/backend-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ jobs:
echo "aws_access_key_id=${{ secrets.AWS_ACCESS_KEY_ID }}" >> ~/.aws/credentials
echo "aws_secret_access_key=${{ secrets.AWS_SECRET_ACCESS_KEY }}" >> ~/.aws/credentials
- name: Terraform Plan
id: plan
run: |
cd ./terraform
terraform init
plan_output=$(terraform plan -no-color)
echo "::set-output name=has_changes::$(echo $plan_output | grep -E 'No changes. Infrastructure is up-to-date.' > /dev/null && echo 'false' || echo 'true')"
echo "$plan_output"
- name: Generate provider.tf
run: |
echo 'provider "aws" {' > ./terraform/provider.tf
Expand All @@ -51,16 +42,20 @@ jobs:
terraform_version: 1.0.0

- name: Generate environment-var.tf
if: steps.plan.outputs.has_changes == 'true'
run: |
echo "${{ secrets.TF_ENV_FILE }}" > ./terraform/environment-var.tf
- name: Run Terraform commands
if: steps.plan.outputs.has_changes == 'true'
- name: Terraform Plan and Apply
id: terraform
run: |
cd ./terraform
terraform init
terraform apply -auto-approve
terraform plan -out=tfplan
terraform show -json tfplan > plan.json
if jq -e '.resource_changes[] | select(.change.actions | index("no-op") | not)' plan.json; then
terraform apply -auto-approve tfplan
fi
env:
TF_VAR_ami: ${{ secrets.TF_VAR_ami }}
TF_VAR_db_password: ${{ secrets.TF_VAR_db_password }}
Expand All @@ -69,6 +64,7 @@ jobs:
TF_VAR_elb_port: ${{ secrets.TF_VAR_elb_port }}
TF_VAR_rabbitmq: ${{ secrets.TF_VAR_rabbitmq }}


- name: Get EC2 instance IP
id: get_ec2_ip
run: |
Expand Down

0 comments on commit e4b3ec2

Please sign in to comment.