Skip to content

Instantly share code, notes, and snippets.

@cecton
Last active August 17, 2019 12:32
Show Gist options
  • Save cecton/538ce1a6e0b645bd6cbbecf6ccf51366 to your computer and use it in GitHub Desktop.
Save cecton/538ce1a6e0b645bd6cbbecf6ccf51366 to your computer and use it in GitHub Desktop.
Script to comment on GitHub from a CircleCI build
#!/bin/sh
{ # Prevent execution if this script was only partially downloaded
# NOTE: This script will use the GitHub Personal token stored in GITHUB_AUTH_TOKEN
# to post a comment from CircleCI to your pull request (or commit if there
# is no pull request).
#
# The message must be provided in /tmp/comment
#
# This is a less sophisticated replacement for:
# https://github.com/themadcreator/circle-github-bot
set -e
# NOTE: replace newlines by \n and " by \"
body=$(cat /tmp/comment | sed -e ':a' -e 'N' -e '$!ba' -e 's/\n/\\n/g;s/"/\\"/g')
if [ -n "$CIRCLE_PULL_REQUEST" ]; then
location=$(echo $CIRCLE_PULL_REQUEST | grep -Po 'github.com/\K[^/]+/[^/]+')
curl -s -u ${GITHUB_AUTH_TOKEN:?}:x-oauth-basic --data '{"body":"'"$body"'"}' \
https://api.github.com/repos/$location/issues/${CIRCLE_PULL_REQUEST##*/}/comments
else
curl -s -u ${GITHUB_AUTH_TOKEN:?}:x-oauth-basic --data '{"body":"'"$body"'"}' \
https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME:?}/${CIRCLE_PROJECT_REPONAME:?}/commits/${CIRCLE_SHA1:?}/comments
fi
}
@cecton
Copy link
Author

cecton commented Aug 17, 2019

Example Usage:

- run:
    name: Run tests
    command: |
      (
        echo '### Test Results'
        echo '```none'
        <<<run_test_command_here>>>
        echo '```'
      ) > /tmp/comment
      curl -sSL https://gist.githubusercontent.com/cecton/538ce1a6e0b645bd6cbbecf6ccf51366/raw/de93dfdf7182c07eebbfa258ca5a38a0c8149e01/circleci_comment_on_github.sh | sh -

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment