Last active
May 29, 2017 21:29
-
-
Save SISheogorath/f0d732778cb2e493b6bc26d4e3bd4d66 to your computer and use it in GitHub Desktop.
Use Travis CLI everywhere by running docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
set -e | |
OPWD=$(pwd) | |
TRAVIS_DIR="$HOME/.travis-cli" | |
TRAVIS_RCFILE="$HOME/.travis" | |
TRAVIS_IMAGETAG="travis:local" | |
# Check that all we need exists | |
command_exists() { command -v "$1" >/dev/null 2>&1 || { echo >&2 "I require $1 but it's not installed. Aborting."; exit 1; }; } | |
docker_installed() { command -v docker >/dev/null 2>&1 || { wget -O- http://get.docker.com | sh - ; }; command -v docker >/dev/null 2>&1 || { echo >&2 "I require docker but it's not installed and I can't install it myself. Aborting."; exit 1; }; } | |
docker_installed | |
# Simple create the tivial Travis CLI Dockerfile | |
if [ ! -e "$TRAVIS_DIR" ]; then | |
mkdir "$TRAVIS_DIR" | |
cat >"$TRAVIS_DIR/Dockerfile" <<EOF | |
FROM ruby:2.3 | |
RUN gem install travis \ | |
&& mkdir /app | |
WORKDIR /app | |
ENTRYPOINT ["travis"] | |
EOF | |
fi | |
# Use sudo if docker socket is not writeable | |
[ -w /var/run/docker.sock ] || SUDO=sudo | |
[ -e /etc/fedora-release ] && MOUNTOPTIONS=:z | |
cd "$TRAVIS_DIR" | |
# Check that there is a image. If not build a new one | |
if [ "$($SUDO docker images --format "{{.Repository}}:{{.Tag}}" | grep -c "$TRAVIS_IMAGETAG")" -eq 0 ]; then | |
$SUDO docker build -t "$TRAVIS_IMAGETAG" . | |
fi | |
# Switch back to original directory | |
cd "$OPWD" | |
# Run t in container | |
exec $SUDO docker run --rm -it -u "$(id -u)" -v "$(pwd):/app$MOUNTOPTIONS" -v "$TRAVIS_RCFILE:/.travis$MOUNTOPTIONS" --read-only $TRAVIS_IMAGETAG "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment