Skip to content

Commit

Permalink
feat: Added inputs for tf-init step
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanveer143s committed Jul 1, 2024
1 parent b8b6f98 commit e0c02a2
Showing 1 changed file with 42 additions and 18 deletions.
60 changes: 42 additions & 18 deletions .github/workflows/terraform_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,10 @@ jobs:
actions_subcommand: 'fmt'
path: ${{ inputs.working_directory }}

# - name: terraform init
# run: |
# cd ${{ inputs.working_directory }}
# terraform init

- name: terraform init
run: |
cd ${{ inputs.working_directory }}
terraform init \
-backend-config="bucket=${{ inputs.backend_bucket_name }}" \
-backend-config="key=${{ inputs.errored_tfstate_path }}"
terraform init
- name: 'Terraform validate'
if: ${{ inputs.destroy != true }}
Expand Down Expand Up @@ -240,16 +233,47 @@ jobs:
minimum-approvals: ${{ inputs.minimum-approvals }}
issue-title: "Terraform Plan for Infrastructure Update"

- name: terraform apply
if: ${{ inputs.destroy != true }}
run: |
if [ -n "${{ inputs.var_file }}" ]; then
cd ${{ inputs.working_directory }}
terraform apply -var-file="${{ inputs.var_file }}" -auto-approve
else
cd ${{ inputs.working_directory }}
terraform apply -auto-approve
fi
# - name: terraform apply
# if: ${{ inputs.destroy != true }}
# run: |
# if [ -n "${{ inputs.var_file }}" ]; then
# cd ${{ inputs.working_directory }}
# terraform apply -var-file="${{ inputs.var_file }}" -auto-approve
# else
# cd ${{ inputs.working_directory }}
# terraform apply -auto-approve
# fi
- name: terraform apply
if: ${{ inputs.destroy != true }}
run: |
cd ${{ inputs.working_directory }}
if [ -f "errored.tfstate" ]; then
echo "Found errored.tfstate, attempting state recovery..."
terraform state push errored.tfstate
# Optionally move or manage the errored.tfstate file
mv errored.tfstate /path/to/backup/location
fi
if [ -n "${{ inputs.var_file }}" ]; then
terraform apply -var-file="${{ inputs.var_file }}" -auto-approve || {
if [ -f "errored.tfstate" ]; then
echo "Copying errored.tfstate to S3..."
aws s3 cp errored.tfstate s3://${{ inputs.backend_s3_bucket }}/${{ inputs.backend_s3_key }}/errored.tfstate
fi
exit 1
}
else
terraform apply -auto-approve || {
if [ -f "errored.tfstate" ]; then
echo "Copying errored.tfstate to S3..."
aws s3 cp errored.tfstate s3://${{ inputs.backend_s3_bucket }}/${{ inputs.backend_s3_key }}/errored.tfstate
fi
exit 1
}
}
- name: Terraform destroy
if: ${{ inputs.destroy == true }}
Expand Down

0 comments on commit e0c02a2

Please sign in to comment.