php-censor/docs/en/sources/git.md

69 lines
1.7 KiB
Markdown
Raw Permalink Normal View History

2016-07-19 11:12:28 +02:00
Automatically building commits pushed to Git
2017-01-04 13:22:20 +01:00
============================================
2016-07-19 11:12:28 +02:00
Requirements
2017-01-04 13:22:20 +01:00
------------
2016-07-19 11:12:28 +02:00
2016-07-17 16:20:35 +02:00
- A git repository on a server (bare or plain does not matter)
- [curl](http://curl.haxx.se) to send the web hook
2016-07-19 11:12:28 +02:00
Installation
2017-01-04 13:22:20 +01:00
------------
2016-07-19 11:12:28 +02:00
2017-07-18 15:54:24 +02:00
1. Create a new file `post-receive` inside the
[git `hooks` directory](http://www.git-scm.com/book/en/Customizing-Git-Git-Hooks) with the following content:
2016-07-17 16:20:35 +02:00
```shell
#!/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/"
2016-07-17 16:20:35 +02:00
trigger_hook() {
2016-07-19 11:12:28 +02:00
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" \
2016-07-21 17:20:34 +02:00
"$APP_URL/webhook/git/$PROJECT_ID"
2016-07-17 16:20:35 +02:00
}
if [ -n "$1" -a -n "$2" -a -n "$3" ]; then
2016-07-19 11:12:28 +02:00
PAGER= trigger_hook $1 $2 $3
2016-07-17 16:20:35 +02:00
else
2016-07-19 11:12:28 +02:00
while read oldrev newrev refname; do
trigger_hook $oldrev $newrev $refname
done
2016-07-17 16:20:35 +02:00
fi
```
2. Change the file to be executable: `chmod a+x post-receive`
3. Push changes to the repository