Create GitHub PR Comment from Azure Pipeline

Viewed 751

After a quick internet search I couldn't seem to find an easy way for my Azure Pipeline to write a custom comment back to the PR that triggered it. Is this possible? Does it require a PAT? I can't use any solution that requires exposing a PAT to a external PR, as they could then easily exfiltrate it.

1 Answers

Sure, you can add comments to the PR on GitHub from Azure pipelines. You can use the GitHub Comment task in your pipeline to easily write comments to the GitHub PR.

enter image description here

With this task, you also need to create a GitHub service connection, or a GitHub Enterprise Server service connection if your repository is hosted on GitHub Enterprise Server, for use on the task.

enter image description here

When creating the GitHub (or GitHub Enterprise Server) service connection, you can choose an authorization method from the optional.

  • GitHub service connection -- Grant authorization or Personal Access Token

    enter image description here

  • GitHub Enterprise Server service connection -- Personal Access Token, Username and Password or OAuth2

    enter image description here

So, a GitHub PAT is not required if the authorization method you choose is not Personal Access Token.

[UPDATE]

If you are worrying about that the service connection would be abused by someone to attack your source code repository, you can do the following things:

  1. On GitHub, you can create a PAT, and limit the permission scopes of this PAT. More details, see "Creating a personal access token".

  2. On Azure DevOps, you can choose Personal Access Token as the authorization method on the service connection, and fill with the PAT that you created in above step. Then you can limit which users, teams and groups, even which pipelines, can use the service connection in the project. More details, you can see "Secure a service connection".

Related