php-censor/docs/en/sources/git.md
2017-11-08 12:03:00 +03:00

1.7 KiB

Automatically building commits pushed to Git

Requirements

  • A git repository on a server (bare or plain does not matter)
  • curl to send the web hook

Installation

  1. Create a new file post-receive inside the git hooks directory with the following content:
#!/bin/sh

PROJECT_ID=1

# You can use the project title instead of the id:
# PROJECT_ID=my_project_title

# If the names of the repository and the project match:
# PROJECT_ID=`basename \`pwd\` | sed 's#\.git$##'`

APP_URL="http://my.server.com/php-censor/"

trigger_hook() {
    NEWREV="$2"
    REFNAME="$3"
    
    if [ "$NEWREV" = "0000000000000000000000000000000000000000" ]; then
        # Ignore deletion
        return
    fi
    
    case "$REFNAME" in
        # Triggers only on branches and tags
        refs/heads/*|refs/tags/*) ;;
        # Bail out on other references
        *) return ;;
    esac
    
    BRANCH=$(git rev-parse --symbolic --abbrev-ref "$REFNAME")
    COMMITTER=$(git log -1 "$NEWREV" --pretty=format:%ce)
    MESSAGE=$(git log -1 "$NEWREV" --pretty=format:%s)
    
    echo "Sending webhook"
    curl \
        --data-urlencode branch="$BRANCH" \
        --data-urlencode commit="$NEWREV" \
        --data-urlencode committer="$COMMITTER" \
        --data-urlencode message="$MESSAGE" \
        "$APP_URL/webhook/git/$PROJECT_ID"
}

if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
    PAGER= trigger_hook $1 $2 $3
else
    while read oldrev newrev refname; do
        trigger_hook $oldrev $newrev $refname
    done
fi
  1. Change the file to be executable: chmod a+x post-receive
  2. Push changes to the repository