In an Azure Pipeline, the following will post a multi-line comment to a GitHub PR:
stages:
- stage: MyStage
jobs:
- job: CommentOnPR
steps:
- task: GitHubComment@0
displayName: Post comment to PR
inputs:
gitHubConnection: MyGitHubConnection
repositoryName: $(build.repository.name)
comment: |
Here is a comment
with multiple lines
The following will also post a multi-line comment:
variables:
myComment: "Here is a comment\nwithmultiplelines"
stages:
- stage: MyStage
jobs:
- job: CommentOnPR
steps:
- task: GitHubComment@0
displayName: Post comment to PR
inputs:
gitHubConnection: MyGitHubConnection
repositoryName: $(build.repository.name)
comment: $(myComment)
The multi-line comment I am wanting to post is dynamically generated in a script kind of like the following located at scripts/my-script
:
#!/bin/bash
output="# Changes
The following packages were updated:
"
for package_name in $(git diff --name-only origin/main packages/ | cut -d'/' -f2 | sort -u)
do
output+="\n- $package_name"
done
export output
The output of this script then looks something like the following:
# Changes
The following packages were updated:
- some-package
- another-package
- etc.
(Note, my actual script is different, and is doing a few other things in addition to generating a multi-line string. I'm not asking about the contents of this script specifically.)
Given that setup, I would like my pipeline to run the scripts/my-script
and use the output from it in the GitHub PR comment. However, everything I've tried either ends up with just the first line as the comment or a single line comment where all the \n
s are shown literally.
I tried this:
stages:
- stage: MyStage
jobs:
- job: CommentOnPR
steps:
- bash: |
source scripts/my-script
echo "##vso[task.setvariable variable=myComment]$output"
- task: GitHubComment@0
displayName: Post comment to PR
inputs:
gitHubConnection: MyGitHubConnection
repositoryName: $(build.repository.name)
comment: $(myComment)
But the comment in GitHub was just the first line:
# Changes
I then tried changing scripts/my-script
to only use \n
s:
#!/bin/bash
output="# Changes\n\nThe following packages were updated:\n"
for package_name in $(git diff --name-only origin/main packages/ | cut -d'/' -f2 | sort -u)
do
output+="\n- $package_name"
done
export output
The comment in GitHub was all a single line:
# Changes\n\nThe following packages were updated:\n\n- some-package\n- another-package \n- etc.
I can't find the magic combination where I can dynamically generate a multi-line string in a step and then have the subsequent GitHubComment
task display it properly. I'm fairly new to bash scripting and Pipelines. Any ideas? Thank you.