Skip to content

Instantly share code, notes, and snippets.

@c9s

c9s/-

Created July 19, 2015 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save c9s/492cee84c7f34c0cc283 to your computer and use it in GitHub Desktop.
Save c9s/492cee84c7f34c0cc283 to your computer and use it in GitHub Desktop.
#!/bin/bash
source /etc/profile
ANSI_RED="\033[31;1m"
ANSI_GREEN="\033[32;1m"
ANSI_RESET="\033[0m"
ANSI_CLEAR="\033[0K"
TRAVIS_TEST_RESULT=
TRAVIS_CMD=
function travis_cmd() {
local assert output display retry timing cmd result
cmd=$1
TRAVIS_CMD=$cmd
shift
while true; do
case "$1" in
--assert) assert=true; shift ;;
--echo) output=true; shift ;;
--display) display=$2; shift 2;;
--retry) retry=true; shift ;;
--timing) timing=true; shift ;;
*) break ;;
esac
done
if [[ -n "$timing" ]]; then
travis_time_start
fi
if [[ -n "$output" ]]; then
echo "\$ ${display:-$cmd}"
fi
if [[ -n "$retry" ]]; then
travis_retry eval "$cmd"
else
eval "$cmd"
fi
result=$?
if [[ -n "$timing" ]]; then
travis_time_finish
fi
if [[ -n "$assert" ]]; then
travis_assert $result
fi
return $result
}
travis_time_start() {
travis_timer_id=$(printf %08x $(( RANDOM * RANDOM )))
travis_start_time=$(travis_nanoseconds)
echo -en "travis_time:start:$travis_timer_id\r${ANSI_CLEAR}"
}
travis_time_finish() {
local result=$?
travis_end_time=$(travis_nanoseconds)
local duration=$(($travis_end_time-$travis_start_time))
echo -en "travis_time:end:$travis_timer_id:start=$travis_start_time,finish=$travis_end_time,duration=$duration\r${ANSI_CLEAR}"
return $result
}
function travis_nanoseconds() {
local cmd="date"
local format="+%s%N"
local os=$(uname)
if hash gdate > /dev/null 2>&1; then
cmd="gdate" # use gdate if available
elif [[ "$os" = Darwin ]]; then
format="+%s000000000" # fallback to second precision on darwin (does not support %N)
fi
$cmd -u $format
}
travis_assert() {
local result=${1:-$?}
if [ $result -ne 0 ]; then
echo -e "\n${ANSI_RED}The command \"$TRAVIS_CMD\" failed and exited with $result during $TRAVIS_STAGE.${ANSI_RESET}\n\nYour build has been stopped."
travis_terminate 2
fi
}
travis_result() {
local result=$1
export TRAVIS_TEST_RESULT=$(( ${TRAVIS_TEST_RESULT:-0} | $(($result != 0)) ))
if [ $result -eq 0 ]; then
echo -e "\n${ANSI_GREEN}The command \"$TRAVIS_CMD\" exited with $result.${ANSI_RESET}"
else
echo -e "\n${ANSI_RED}The command \"$TRAVIS_CMD\" exited with $result.${ANSI_RESET}"
fi
}
travis_terminate() {
pkill -9 -P $$ &> /dev/null || true
exit $1
}
travis_wait() {
local timeout=$1
if [[ $timeout =~ ^[0-9]+$ ]]; then
# looks like an integer, so we assume it's a timeout
shift
else
# default value
timeout=20
fi
local cmd="$@"
local log_file=travis_wait_$$.log
$cmd &>$log_file &
local cmd_pid=$!
travis_jigger $! $timeout $cmd &
local jigger_pid=$!
local result
{
wait $cmd_pid 2>/dev/null
result=$?
ps -p$jigger_pid &>/dev/null && kill $jigger_pid
} || return 1
if [ $result -eq 0 ]; then
echo -e "\n${ANSI_GREEN}The command \"$TRAVIS_CMD\" exited with $result.${ANSI_RESET}"
else
echo -e "\n${ANSI_RED}The command \"$TRAVIS_CMD\" exited with $result.${ANSI_RESET}"
fi
echo -e "\n${ANSI_GREEN}Log:${ANSI_RESET}\n"
cat $log_file
return $result
}
travis_jigger() {
# helper method for travis_wait()
local cmd_pid=$1
shift
local timeout=$1 # in minutes
shift
local count=0
# clear the line
echo -e "\n"
while [ $count -lt $timeout ]; do
count=$(($count + 1))
echo -ne "Still running ($count of $timeout): $@\r"
sleep 60
done
echo -e "\n${ANSI_RED}Timeout (${timeout} minutes) reached. Terminating \"$@\"${ANSI_RESET}\n"
kill -9 $cmd_pid
}
travis_retry() {
local result=0
local count=1
while [ $count -le 3 ]; do
[ $result -ne 0 ] && {
echo -e "\n${ANSI_RED}The command \"$@\" failed. Retrying, $count of 3.${ANSI_RESET}\n" >&2
}
"$@"
result=$?
[ $result -eq 0 ] && break
count=$(($count + 1))
sleep 1
done
[ $count -gt 3 ] && {
echo -e "\n${ANSI_RED}The command \"$@\" failed 3 times.${ANSI_RESET}\n" >&2
}
return $result
}
travis_fold() {
local action=$1
local name=$2
echo -en "travis_fold:${action}:${name}\r${ANSI_CLEAR}"
}
decrypt() {
echo $1 | base64 -d | openssl rsautl -decrypt -inkey ~/.ssh/id_rsa.repo
}
# XXX Forcefully removing rabbitmq source until next build env update
# See http://www.traviscistatus.com/incidents/6xtkpm1zglg3
if [[ -f /etc/apt/sources.list.d/rabbitmq-source.list ]] ; then
sudo rm -f /etc/apt/sources.list.d/rabbitmq-source.list
fi
mkdir -p $HOME/build
cd $HOME/build
travis_fold start system_info
echo -e "\033[33;1mBuild system information\033[0m"
if [[ -f /usr/share/travis/system_info ]]; then
cat /usr/share/travis/system_info
fi
travis_fold end system_info
echo
echo "options rotate
options timeout:1
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 208.67.222.222
nameserver 208.67.220.220
" | sudo tee /etc/resolv.conf &> /dev/null
sudo sed -e 's/^\(127\.0\.0\.1.*\)$/\1 '`hostname`'/' -i'.bak' /etc/hosts
test -f /etc/mavenrc && sudo sed -e 's/M2_HOME=\(.\+\)$/M2_HOME=${M2_HOME:-\1}/' -i'.bak' /etc/mavenrc
sudo sed -e 's/^127\.0\.0\.1\(.*\) localhost \(.*\)$/127.0.0.1 localhost \1 \2/' -i'.bak' /etc/hosts 2>/dev/null
# apply :home_paths
for path_entry in $HOME/.local/bin $HOME/bin ; do
if [[ ${PATH%%:*} != $path_entry ]] ; then
export PATH="$path_entry:$PATH"
fi
done
export GIT_ASKPASS=echo
travis_fold start git.checkout
if [[ ! -d travis-ci/travis-support/.git ]]; then
travis_cmd git\ clone\ --depth\=50\ --branch\=master\ http://github.com/travis-ci/travis-support.git\ travis-ci/travis-support --assert --echo --retry --timing
else
travis_cmd git\ -C\ travis-ci/travis-support\ fetch\ origin --assert --echo --retry --timing
travis_cmd git\ -C\ travis-ci/travis-support\ reset\ --hard --assert --echo
fi
travis_cmd cd\ travis-ci/travis-support --echo
travis_cmd git\ checkout\ -qf\ a214c21 --assert --echo
travis_fold end git.checkout
if [[ -f .gitmodules ]]; then
travis_fold start git.submodule
echo Host\ github.com'
'\ StrictHostKeyChecking\ no'
' >> ~/.ssh/config
travis_cmd git\ submodule\ init --assert --echo --timing
travis_cmd git\ submodule\ update --assert --echo --retry --timing
travis_fold end git.submodule
fi
rm -f ~/.ssh/source_rsa
export PS4=+
export TRAVIS=true
export CI=true
export CONTINUOUS_INTEGRATION=true
export HAS_JOSH_K_SEAL_OF_APPROVAL=true
export TRAVIS_PULL_REQUEST=false
export TRAVIS_SECURE_ENV_VARS=false
export TRAVIS_BUILD_ID=1
export TRAVIS_BUILD_NUMBER=1
export TRAVIS_BUILD_DIR=$HOME/build/travis-ci/travis-support
export TRAVIS_JOB_ID=1
export TRAVIS_JOB_NUMBER=1.1
export TRAVIS_BRANCH=master
export TRAVIS_COMMIT=a214c21
export TRAVIS_COMMIT_RANGE=abcdefg..a214c21
export TRAVIS_REPO_SLUG=travis-ci/travis-support
export TRAVIS_OS_NAME=''
export TRAVIS_LANGUAGE=''
export TRAVIS_TAG=''
export TRAVIS_RUBY_VERSION=ruby-head
if [[ -f build.gradle ]]; then
travis_cmd export\ TERM\=dumb --echo
fi
echo rvm_remote_server_url3\=https://s3.amazonaws.com/travis-rubies/binaries'
'rvm_remote_server_type3\=rubies'
'rvm_remote_server_verify_downloads3\=1 > $rvm_path/user/db
travis_fold start rvm
echo -e "\033[33;1mSetting up latest ruby-head\033[0m"
travis_cmd export\ ruby_alias\=\`rvm\ alias\ show\ ruby-head\ 2\>/dev/null\` --echo
travis_cmd rvm\ alias\ delete\ ruby-head --assert --echo --timing
travis_cmd rvm\ remove\ \$\{ruby_alias:-ruby-head\}\ --gems --assert --echo --timing
travis_cmd rvm\ remove\ ruby-head\ --gems\ --fuzzy --assert --echo --timing
travis_cmd rvm\ install\ ruby-head\ --binary --assert --echo --timing
travis_cmd rvm\ use\ ruby-head --assert --echo --timing
travis_fold end rvm
if [[ -f Gemfile ]]; then
travis_cmd export\ BUNDLE_GEMFILE\=\$PWD/Gemfile --echo
fi
travis_cmd ruby\ --version --echo
travis_cmd rvm\ --version --echo
travis_cmd bundle\ --version --echo
travis_cmd gem\ --version --echo
if [[ -f Gemfile ]]; then
if [[ -f Gemfile.lock ]]; then
travis_fold start install.bundler
travis_cmd bundle\ install\ --jobs\=3\ --retry\=3\ --deployment --assert --echo --retry --timing
travis_fold end install.bundler
else
travis_fold start install.bundler
travis_cmd bundle\ install\ --jobs\=3\ --retry\=3 --assert --echo --retry --timing
travis_fold end install.bundler
fi
fi
if [[ -f Gemfile ]]; then
travis_cmd bundle\ exec\ rake --echo --timing
else
travis_cmd rake --echo --timing
fi
travis_result $?
echo -e "\nDone. Your build exited with $TRAVIS_TEST_RESULT."
travis_terminate $TRAVIS_TEST_RESULT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment