Skip to content

Instantly share code, notes, and snippets.

@GTRekter
Last active January 23, 2024 14:11
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GTRekter/51f8be3fbfb13b3696f92e117d956597 to your computer and use it in GitHub Desktop.
Save GTRekter/51f8be3fbfb13b3696f92e117d956597 to your computer and use it in GitHub Desktop.
This snippet contains the steps to generate a terraform plan and post it as a comment of a pull request in Azure DevOps
- script: |
terraform plan -out tf.tfplan
displayName: Generate Terraform plan
- script: |
terraform show -no-color tf.tfplan > $(Agent.TempDirectory)/tf.txt
displayName: Convert Terraform plan to text
- bash: |
cd $(Agent.TempDirectory)
ENCODED_URL=$(echo "$(System.CollectionUri)$(System.TeamProject)/_apis/git/repositories/${{ variables.SourceRepositoryName }}/pullRequests/$(System.PullRequest.PullRequestId)/threads?api-version=7.0" | sed 's/ /%20/g')
jq --rawfile comment tf.txt '.comments[0].content=$comment' <<< '{"comments": [{"parentCommentId": 0,"content": "","commentType": 1}],"status": 1}' |
curl --request POST "$ENCODED_URL" \
--header "Content-Type: application/json" \
--header "Accept: application/json" \
--header "Authorization: Bearer $SYSTEM_ACCESSTOKEN" \
--data @- \
--verbose
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
displayName: 'Post comment with Terraform Plan'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment