Created
October 22, 2022 08:35
-
-
Save syedadeel2/e6096481d29247bc91b64e4d611c4e02 to your computer and use it in GitHub Desktop.
Get Terraform Output to Variables
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" | |
on: | |
push: | |
branches: main | |
permissions: | |
contents: read | |
jobs: | |
terraform: | |
name: "Terraform" | |
runs-on: ubuntu-latest | |
env: | |
skip_terraform: 'false' | |
skip_terraform_plan: 'true' | |
skip_terraform_apply: 'true' | |
skip_terraform_destroy: 'true' | |
defaults: | |
run: | |
shell: bash | |
steps: | |
# Checkout the repository to the GitHub Actions runner | |
- name: Checkout | |
uses: actions/checkout@v3 | |
with: | |
path: main | |
- name: Set Environment Variables | |
run: | | |
echo "TF_VAR_ref=${ref}" >> $GITHUB_ENV | |
echo "TF_VAR_environment=${{ env.environment }}" >> $GITHUB_ENV | |
# Install the latest version of Terraform CLI and configure the Terraform CLI configuration file with a Terraform Cloud user API token | |
- name: Setup Terraform | |
uses: hashicorp/setup-terraform@v2 | |
with: | |
terraform_wrapper: true # << this is important to output to step | |
if: ${{ env.skip_terraform != 'true' }} | |
# Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc. | |
- name: Terraform Init | |
run: terraform init | |
if: ${{ env.skip_terraform != 'true' }} | |
- name: Terraform Destroy | |
run: terraform destroy -auto-approve -input=false | |
if: ${{ env.skip_terraform != 'true' && env.skip_terraform_destroy != 'true' }} | |
# Generates an execution plan for Terraform | |
- name: Terraform Plan | |
run: terraform plan -input=false | |
if: ${{ env.skip_terraform != 'true' && env.skip_terraform_plan != 'true' }} | |
- name: Terraform Apply | |
run: terraform apply -auto-approve -input=false | |
if: ${{ env.skip_terraform != 'true' && env.skip_terraform_apply != 'true' }} | |
- name: Terraform Output | |
id: terra_out | |
run: terraform output -json | |
- name: Convert Terraform Output To Envs | |
run: | | |
oJSON=$(jq -n --arg b "$outs" '${{ steps.terra_out.outputs.stdout }}') | |
echo "REPLACE_THIS_WITH_YOUR_WHATEVER_NAME=$(jq -r '.REPLACE_THIS_WITH_ACTUAL_OUTPUT_NAME.value' <<< $oJSON)" >> $GITHUB_ENV | |
- name: Perform Manipulations | |
shell: pwsh | |
run: | | |
echo "${{ env.REPLACE_THIS_WITH_YOUR_WHATEVER_NAME }}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment