gitgithubazure-devopsazure-pipelinessalesforce

Adding comment to a commit when Azure release pipeline fails


I have an Azure release pipeline that triggers a Salesforce Sandbox deployment when a developer pushes a commit to a dev branch(connected to the pipeline). Sometimes, this deployment fails, which also causes the release pipeline to fail. Currently, the developers do not receive a notification when this happens.

Is there a way to push a comment to the commit to notify the developer when their commits are causing issues on the dev sandbox?


Solution

  • You can add a new job in your release pipeline to add comments to the commit using GitHub REST API Create a commit comment when the deployment job is failed.

    Steps:
    1. Add a new agent job after your deployment job. enter image description here

      • Agent Specification: Agent Specification
      • Run this job: Only when a previous job has failed
    2. Add a Command line task to run Github REST API to add comments to your commit.

      enter image description here

      • SourceAlias: The alias of your github artifact. enter image description here
      • GitHubOwner: The account owner of the repository.
      • GitHubRepoName: The name of the repository.
      • GitHub.Token: The GitHub PAT. Create it in GitHub with repo scope and store it in Azure DevOps pipeline as a secret variable.
    The scripts:
    COMMIT_ID=$(Release.Artifacts.{SourceAlias}.SourceVersion) 
    echo $COMMIT_ID
    curl -L \
        -X POST \
        "https://api.github.com/repos/{GitHubOwner}/{GitHubRepoName}/commits/$COMMIT_ID/comments" \
        -H "X-GitHub-Api-Version: 2022-11-28" \
        -H "Accept: application/vnd.github+json" \
        -H "Authorization: token $(GitHub.Token)" \
        -d '{"body":"The deployment is failed."}'
    
    Result:

    When the deployment is successful, the "Check Deploy" job will be skipped. When the deployment is failed, the "Check Deploy" job will add a comment to the commit that triggered the Azure release pipeline.

    enter image description here

    enter image description here