Adding Terraform Outputs to Github PR

trying to improve on the github action outputs. using the below gets me 90% of the way. i just want to add the outputs of tf to it if possible? The onyl way i see to get those values is via a `qovery lifecycle env list --show-values and id then to drop all the other values or just retain those from service terraform. Is there common way to do that?

      - name: Qovery Terraform Deploy
        if: ${{ inputs.terraform-deployment }}
        id: terraform-deploy
        run: |
          qovery lifecycle deploy \
          --organization "Open Corporates" \
          --project "Open Corporates" \
          --lifecycle "terraform" \
          --environment ${{ steps.extract_branch.outputs.branch }} \
          --commit-id $GITHUB_SHA \
          --watch

          qovery_status_markdown_output=`qovery service list \
          --organization "Open Corporates" \
          --project "Open Corporates" \
          --environment ${{ steps.extract_branch.outputs.branch }} \
          --markdown`

          echo "QOVERY_STATUS_MARKDOWN_OUTPUT<<EOF" >> "$GITHUB_OUTPUT"
          echo "$qovery_status_markdown_output" >> "$GITHUB_OUTPUT"
          echo "EOF" >> "$GITHUB_OUTPUT"

        env:
          QOVERY_CLI_ACCESS_TOKEN: ${{ secrets.qovery-token }}


      - name: PR Comment with URL
        uses: mshick/add-pr-comment@v2
        with:
          message-id: Qovery environment
          message: |
            ## Terraform Deployment:

            ${{ steps.terraform-deploy.outputs.QOVERY_STATUS_MARKDOWN_OUTPUT }}

Hi @Stephen_Bennett :wave: ,

Correct me if I’m wrong, but what do you want is the Terraform execution output? If yes, you have two possibilities:

Solution 1: Lifecycle Job Output

(This is the recommended way for your use case)

Use the Lifecycle Job output to:

  1. Inject the Terraform output into an environment variable (e.g, TERRAFORM_EXEC_OUTPUT)
  2. Retrieve it from the environment variable (e.g. TERRAFORM_EXEC_OUTPUT) so you can read it and use the content with the CLI command:
qovery lifecycle env list --json -n "terraform" \
--organization "Open Corporates" \
--project "Open Corporates" \
--environment "your-env" \
| jq -r '.[] | select(.key == "TERRAFORM_EXEC_OUTPUT").interpolated_value

Solution 2: Use Lifecycle Logs

Use the qovery log command

E.g. qovery log --organization "Open Corporates" --project "Open Corporates" --environment "your-env" --lifecycle "terraform"

I didn’t try this command - but if it does not work, I can take a look

I hope it helps :slightly_smiling_face:

playing around im still not getting the desired output.

because its creating a alias of the output with the correct name and a link to the value and the entry with the value doesnt a custom value name. ie:

    {
        "alias_parent_key": null,
        "created_at": "2024-07-01T10:44:33.634Z",
        "id": "14a24bea-861c-4e8f-8a49-f6d86271cda7",
        "interpolated_value": "data-quality/bot-data-quality/data-expectation-check-dev-1855/user",
        "key": "QOVERY_OUTPUT_JOB_Z3ED32CDA_RAKE_USER_SECRETS_PATH",
        "override_parent_value": null,
        "scope": "BUILT_IN",
        "service_name": "terraform",
        "updated_at": "2024-07-01T10:52:18.046Z",
        "value": "data-quality/bot-data-quality/data-expectation-check-dev-1855/user"
    },
    {
        "alias_parent_key": "QOVERY_OUTPUT_JOB_Z3ED32CDA_RAKE_USER_SECRETS_PATH",
        "created_at": "2024-07-01T10:44:33.672Z",
        "id": "69b93e0f-59ce-46be-8ab0-871a91b90787",
        "interpolated_value": "QOVERY_OUTPUT_JOB_Z3ED32CDA_RAKE_USER_SECRETS_PATH",
        "key": "RAKE_USER_SECRETS_PATH",
        "override_parent_value": null,
        "scope": "ENVIRONMENT",
        "service_name": null,
        "updated_at": "2024-07-01T10:44:33.672Z",
        "value": "QOVERY_OUTPUT_JOB_Z3ED32CDA_RAKE_USER_SECRETS_PATH"
    },

where i would like to get:

{
  "key" : "RAKE_USER_SECRETS_PATH"
  "value: : "data-quality/bot-data-quality/data-expectation-check-dev-1855/user"
}

im using: 

jq ‘. | select(.scope == “ENVIRONMENT” and .alias_parent_key != null) | {key: .key, value: .value}’

Hello @Stephen_Bennett
It should have been fixed in the CLI version 0.94.17.
Let me know if it is not the case.

Regards

so running the above (adding the closing ’ for the jq pipe i get error

% qovery lifecycle env list --json -n "terraform" \
--organization "Open Corporates" \
--project "Open Corporates" \
--environment "dev-1855" \
| jq -r '.[]' | select(.key == "TERRAFORM_EXEC_OUTPUT").interpolated_value
zsh: no matches found: select(.key == TERRAFORM_EXEC_OUTPUT).interpolated_value
% 
[1]    done         qovery lifecycle env list --json -n "terraform" --organization  --project    | 
       broken pipe  jq -r '.[]'

and running my query i get :

% qovery lifecycle env list --json -n "terraform" \--organization "Open Corporates" \
--project "Open Corporates" \
--environment "dev-1855" \
| jq '.[] | select(.scope == "ENVIRONMENT" and .alias_parent_key != null) | {key: .key, value: .value}'
{
  "key": "S3_DATA_BUCKET",
  "value": "QOVERY_OUTPUT_JOB_Z3ED32CDA_S3_DATA_BUCKET"
}
{
  "key": "BATCH_JOB_DEFINITION",
  "value": "QOVERY_OUTPUT_JOB_Z3ED32CDA_BATCH_JOB_DEFINITION"
}
{
  "key": "RAKE_USER_NAME",
  "value": "QOVERY_OUTPUT_JOB_Z3ED32CDA_RAKE_USER_NAME"
}
{
  "key": "TF_VAR_QOVERY_ENVIRONMENT_NAME",
  "value": "QOVERY_ENVIRONMENT_NAME"
}
{
  "key": "RAKE_USER_SECRETS_PATH",
  "value": "QOVERY_OUTPUT_JOB_Z3ED32CDA_RAKE_USER_SECRETS_PATH"
}
{
  "key": "S3_REPORTS_BUCKET",
  "value": "QOVERY_OUTPUT_JOB_Z3ED32CDA_S3_REPORTS_BUCKET"
}

im on

% qovery version
Info: 0.94.17

I’m not sure it is related but the ’ for closing the jq pipe is not at the correct place. It should be at the end after .interpolated_value

qovery lifecycle env list --json -n "terraform" \
--organization "Open Corporates" \
--project "Open Corporates" \
--environment "your-env" \
| jq -r '.[] | select(.key == "TERRAFORM_EXEC_OUTPUT").interpolated_value '

interesting… i now get no output from running:

% qovery lifecycle env list --json -n "terraform" \
--organization "Open Corporates" \
--project "Open Corporates" \
--environment "dev-1855" \
| jq -r '.[] | select(.key == "TERRAFORM_EXEC_OUTPUT").interpolated_value' 
% 

to confirm just running the command to get the json output. there is no key: terraform_exec_output in any of the json.

i do a decent output from :

% qovery lifecycle env list --json -n "terraform" \
--organization "Open Corporates" \
--project "Open Corporates" \
--environment "dev-1855" \
| jq '.[] | select(.scope == "ENVIRONMENT" and .alias_parent_key != null) | {key: .key, value: .interpolated_value}' 
{
  "key": "S3_DATA_BUCKET",
  "value": "oc-idp-data-expectation-check-dev-1855-data"
}
{
  "key": "BATCH_JOB_DEFINITION",
  "value": "oc-idp-data-expectation-check-dev-1855-job"
}
{
  "key": "RAKE_USER_NAME",
  "value": "data-expectation-check-dev-1855-user"
}
{
  "key": "TF_VAR_QOVERY_ENVIRONMENT_NAME",
  "value": "dev-1855"
}
{
  "key": "RAKE_USER_SECRETS_PATH",
  "value": "data-quality/bot-data-quality/data-expectation-check-dev-1855/user"
}
{
  "key": "S3_REPORTS_BUCKET",
  "value": "oc-idp-data-expectation-check-dev-1855-reports"
}

current working version

      - name: Qovery Terraform Deploy
        if: ${{ inputs.terraform-deployment }}
        id: terraform-deploy
        run: |
          qovery lifecycle deploy \
          --organization "Open Corporates" \
          --project "Open Corporates" \
          --lifecycle "terraform" \
          --environment ${{ steps.extract_branch.outputs.branch }} \
          --commit-id $GITHUB_SHA \
          --watch

          qovery_status_markdown_output=`qovery service list \
          --organization "Open Corporates" \
          --project "Open Corporates" \
          --environment ${{ steps.extract_branch.outputs.branch }} \
          --markdown`

          echo "QOVERY_STATUS_MARKDOWN_OUTPUT<<EOF" >> "$GITHUB_OUTPUT"
          echo "$qovery_status_markdown_output" >> "$GITHUB_OUTPUT"
          echo "EOF" >> "$GITHUB_OUTPUT"

        env:
          QOVERY_CLI_ACCESS_TOKEN: ${{ secrets.qovery-token }}

      - name: Qovery Terraform outputs
        if: ${{ inputs.terraform-deployment }}
        id: terraform-outputs
        run: |
          qovery_terraform_output=`qovery lifecycle env list --json -n "terraform" \
          --organization "Open Corporates" \
          --project "Open Corporates" \
          --environment ${{ steps.extract_branch.outputs.branch }} \
          | jq -r '.[] | select(.scope == "ENVIRONMENT" and .alias_parent_key != null) | [.key, .interpolated_value] | @tsv' \
          | awk -v OFS=" | " 'BEGIN{print "| Key | Value |\n| --- | ----- |"} {print "| "$1, $2" |"}'`

          echo "QOVERY_STATUS_MARKDOWN_OUTPUT<<EOF" >> "$GITHUB_OUTPUT"
          echo "$qovery_terraform_output" >> "$GITHUB_OUTPUT"
          echo "EOF" >> "$GITHUB_OUTPUT"
        env:
          QOVERY_CLI_ACCESS_TOKEN: ${{ secrets.qovery-token }}

      - name: PR Comment with URL
        uses: mshick/add-pr-comment@v2
        with:
          message-id: Qovery environment
          message: |
            ## Terraform Deployment:

            ${{ steps.terraform-deploy.outputs.QOVERY_STATUS_MARKDOWN_OUTPUT }}

            ## Terraform Outputs:

            ${{ steps.terraform-outputs.outputs.QOVERY_STATUS_MARKDOWN_OUTPUT }}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.