Get original repository branch name

Travis does not provide the original repository branch name on any
environment variable. We are getting the information from the GitHub
API instead.

The misleading environment variable `TRAVIS_BRANCH` returns `master`
instead:
> TRAVIS_BRANCH: For builds not triggered by a pull request this is the
name of the branch currently being built; whereas for builds triggered
by a pull request this is the name of the branch targeted by the pull
request (in many cases this will be master).

Reference: http://docs.travis-ci.com/user/environment-variables/
This commit is contained in:
Bruno Pinto 2015-10-01 01:09:41 +01:00
parent faba765a94
commit dde679ff98
2 changed files with 17 additions and 0 deletions

View File

@ -9,6 +9,7 @@ env:
- OMF_REPO_BRANCH="$TRAVIS_BRANCH"
before_install:
- export OMF_REPO_BRANCH=`tools/branch-name.sh`
- docker build -t fish . < Dockerfile
before_script:

16
tools/branch-name.sh Executable file
View File

@ -0,0 +1,16 @@
#!/usr/bin/env sh
URL=https://api.github.com/repos/$TRAVIS_REPO_SLUG/pulls/$TRAVIS_PULL_REQUEST
BRANCH_NAME=`curl -s $URL ^/dev/null \
| grep -C 2 "head" \
| grep \"ref\" \
| cut -d':' -f2- \
| sed -e 's/["|,]//g;s/^[ \t]//g'
`
if [ -n "$BRANCH_NAME" ]; then
echo $BRANCH_NAME
else
echo $TRAVIS_BRANCH
fi