Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

Commit

Permalink
Adding ruby to travis strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
joelwurtz committed Jan 19, 2014
1 parent 9773a5f commit 7d85d78
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 22 deletions.
6 changes: 5 additions & 1 deletion docs/strategies/TravisCiStrategy.md
Expand Up @@ -10,4 +10,8 @@ For the moment only the following language / version list is supported, goal is
* 5.3
* 5.4
* 5.5
* ruby
* 1.9.2
* 1.9.3
* 2.0.0
* 2.1.0
27 changes: 26 additions & 1 deletion resources/travisci/Dockerfile
@@ -1,6 +1,30 @@
FROM ubuntu
MAINTAINER Joel Wurtz <jwurtz@jolicode.com>

# Default ENV for TravisCi
ENV CI true
ENV TRAVIS false
ENV DEBIAN_FRONTEND noninteractive
ENV USER root
ENV HOME /root
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV RAILS_ENV test
ENV RACK_ENV test
ENV MERB_ENV test
ENV JRUBY_OPTS "--server -Dcext.enabled=false -Xcompile.invokedynamic=false"
ENV TRAVIS_BRANCH local
ENV TRAVIS_BUILD_DIR $HOME/project
ENV TRAVIS_BUILD_ID 0
ENV TRAVIS_BUILD_NUMBER 0
ENV TRAVIS_COMMIT ""
ENV TRAVIS_COMMIT_RANGE ""
ENV TRAVIS_JOB_ID 0
ENV TRAVIS_JOB_NUMBER 0
ENV TRAVIS_PULL_REQUEST false
ENV TRAVIS_SECURE_ENV_VARS false
ENV TRAVIS_REPO_SLUG ""

# Add apt repository needed
RUN echo 'deb http://archive.ubuntu.com/ubuntu precise main universe' > /etc/apt/sources.list
RUN echo 'deb http://archive.ubuntu.com/ubuntu precise-security main universe' >> /etc/apt/sources.list
Expand All @@ -9,4 +33,5 @@ RUN echo 'deb http://archive.ubuntu.com/ubuntu precise-backports main restricted
RUN apt-get update && apt-get install -y python-software-properties && add-apt-repository ppa:pdoes/ppa && apt-get update

# Common for all
RUN apt-get install -y subversion mercurial build-essential openssl rsync git sudo curl wget golang-go
RUN apt-get install -y git sudo curl wget
RUN mkdir -p $HOME
1 change: 1 addition & 0 deletions resources/travisci/php/5.3/Dockerfile
@@ -0,0 +1 @@
ENV TRAVIS_PHP_VERSION "5.3"
1 change: 1 addition & 0 deletions resources/travisci/php/5.4/Dockerfile
@@ -1 +1,2 @@
ENV TRAVIS_PHP_VERSION "5.4"
RUN add-apt-repository ppa:ondrej/php5-oldstable && apt-get update
1 change: 1 addition & 0 deletions resources/travisci/php/5.5/Dockerfile
@@ -1 +1,2 @@
ENV TRAVIS_PHP_VERSION "5.5"
RUN add-apt-repository ppa:ondrej/php5 && apt-get update
4 changes: 2 additions & 2 deletions resources/travisci/php/Dockerfile.post
Expand Up @@ -9,5 +9,5 @@ RUN ln -s /etc/php5 ~/.phpenv/versions/php/etc
RUN ls -l ~/.phpenv/versions/php/etc/conf.d

# Add project at the end
ADD . /project
WORKDIR /project
ADD . /home/project
WORKDIR /home/project
1 change: 1 addition & 0 deletions resources/travisci/ruby/1.9.2/Dockerfile
@@ -0,0 +1 @@
RUN rvm install 1.9.2 && rvm alias create default 1.9.2
1 change: 1 addition & 0 deletions resources/travisci/ruby/1.9.3/Dockerfile
@@ -0,0 +1 @@
RUN rvm install 1.9.3 && rvm alias create default 1.9.3
1 change: 1 addition & 0 deletions resources/travisci/ruby/2.0.0/Dockerfile
@@ -0,0 +1 @@
RUN rvm install 2.0.0 && rvm alias create default 2.0.0
1 change: 1 addition & 0 deletions resources/travisci/ruby/2.1.0/Dockerfile
@@ -0,0 +1 @@
RUN rvm install 2.1.0 && rvm alias create default 2.1.0
3 changes: 3 additions & 0 deletions resources/travisci/ruby/Dockerfile.post
@@ -0,0 +1,3 @@
# Add project at the end
ADD . /home/project
WORKDIR /home/project
5 changes: 5 additions & 0 deletions resources/travisci/ruby/Dockerfile.pre
@@ -0,0 +1,5 @@
RUN apt-get -y install expect-dev gawk libyaml-dev libsqlite3-dev sqlite3 autoconf libgdbm-dev libncurses5-dev automake libtool bison pkg-config libffi-dev
RUN curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer -o /tmp/rvm-installer
RUN bash /tmp/rvm-installer latest-1.25 && rm /tmp/rvm-installer

ENV PATH /usr/local/rvm/bin/:$PATH
15 changes: 15 additions & 0 deletions src/Joli/JoliCi/Build.php
Expand Up @@ -37,16 +37,31 @@ public function __construct($name, $directory)
$this->dockername = sprintf("%s-%s", uniqid(), $name);
}

/**
* Get name of this build
*
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* Return directory of build
*
* @return string
*/
public function getDirectory()
{
return $this->directory;
}

/**
* Return the docker name use for image name
*
* @return string
*/
public function getDockerName()
{
return $this->dockername;
Expand Down
2 changes: 2 additions & 0 deletions src/Joli/JoliCi/BuildStrategy/BuildStrategyInterface.php
Expand Up @@ -17,6 +17,8 @@
*/
interface BuildStrategyInterface
{
const WORKDIR = "/home/project";

/**
* Create builds for a project
*
Expand Down
96 changes: 80 additions & 16 deletions src/Joli/JoliCi/BuildStrategy/TravisCiBuildStrategy.php
Expand Up @@ -24,7 +24,24 @@
class TravisCiBuildStrategy implements BuildStrategyInterface
{
private $defaultTestCommand = array(
'php' => 'phpunit'
'php' => 'phpunit',
'node_js' => 'npm test',
'ruby' => 'bundle install && bundle exec rake'
);

private $languageVersionKeyMapping = array(
'php' => 'php',
'ruby' => 'rvm'
);

private $encapsulation = array(
'php' => '%s',
'ruby' => '/bin/bash -l -c "rvm use default && %s"'
);

private $installMapping = array(
'php' => '',
'ruby' => 'bundle install'
);

/**
Expand Down Expand Up @@ -61,25 +78,30 @@ public function createBuilds($directory)
$config = Yaml::parse($directory.DIRECTORY_SEPARATOR.".travis.yml");

$language = $config['language'];
$additionalRunContent = $this->parseBeforeScript($config);
$versionKey = isset($this->languageVersionKeyMapping[$language]) ? $this->languageVersionKeyMapping[$language] : $language;
$beforeScriptContent = $this->parseBeforeScript($config);
$cmdContent = $this->parseScript($config);
$buildRoot = $this->buildPath.DIRECTORY_SEPARATOR.uniqid();
$commonContent = file_get_contents($this->resourcesPath."/Dockerfile");
$installContent = $this->parseInstall($config);
$beforeInstallContent = $this->parseBeforeInstall($config);

if (isset($config[$language]) && file_exists($this->resourcesPath.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR."Dockerfile.pre")) {
if (isset($config[$versionKey]) && file_exists($this->resourcesPath.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR."Dockerfile.pre")) {
$languageContentPre = file_get_contents($this->resourcesPath.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR."Dockerfile.pre");
$languageContentPost = file_get_contents($this->resourcesPath.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR."Dockerfile.post");

foreach ($config[$language] as $version) {
foreach ($config[$versionKey] as $version) {
if (file_exists($this->resourcesPath.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.$version.DIRECTORY_SEPARATOR."Dockerfile")) {
$versionContent = file_get_contents($this->resourcesPath.DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.$version.DIRECTORY_SEPARATOR."Dockerfile");

$dockerFileContent = sprintf("%s\n%s\n%s\n%s\n%s\n%s",
$dockerFileContent = sprintf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
$commonContent,
$languageContentPre,
$versionContent,
$languageContentPost,
$additionalRunContent,
$beforeInstallContent,
$installContent,
$beforeScriptContent,
$cmdContent
);

Expand Down Expand Up @@ -130,13 +152,10 @@ private function parseBeforeScript($config)
}

if (is_array($config['before_script'])) {
$config['before_script'] = array_map(function ($value) {
return sprintf("(%s || echo -n '')", $value);
}, $config['before_script']);
return sprintf("RUN cd /project && %s", implode(" && ", $config['before_script']));
} else {
return sprintf("RUN cd /project && (%s || exit 0)", $config['before_script']);
$config['before_script'] = sprintf("%s", implode(" && ", $config['before_script']));
}

return sprintf("RUN cd %s && %s", self::WORKDIR, $this->encapsulateCmd($config['before_script']));
}

/**
Expand All @@ -149,13 +168,58 @@ private function parseBeforeScript($config)
private function parseScript($config)
{
if (!isset($config['script'])) {
return sprintf("CMD %s", $this->defaultTestCommand[$config['language']]);
$config['script'] = sprintf("%s", $this->defaultTestCommand[$config['language']]);
}

if (is_array($config['script'])) {
return sprintf("CMD %s", implode(" && ", $config['script']));
} else {
return sprintf("CMD %s", $config['script']);
$config['script'] = sprintf("%s", implode(" && ", $config['script']));
}

return sprintf("CMD %s", $this->encapsulateCmd($config, $config['script']));
}

/**
* Return content for task before install
*
* @param array $config TravisCi Configuration parsed
*
* @return string Content to add to Dockerfile
*/
private function parseBeforeInstall($config)
{
if (!isset($config['before_install'])) {
return "";
}

if (is_array($config['before_install'])) {
$config['before_install'] = sprintf("%s", implode(" && ", $config['before_install']));
}

return sprintf("RUN cd %s && %s", self::WORKDIR, $this->encapsulateCmd($config, $config['before_install']));
}

/**
* Return command for task install
*
* @param array $config TravisCi Configuration parsed
*
* @return string Content to add to Dockerfile
*/
private function parseInstall($config)
{
return sprintf("RUN cd %s && %s", self::WORKDIR, $this->encapsulateCmd($config, $this->installMapping[$config['language']]));
}

/**
* Encapsulate command for given language
*
* @param array $config TravisCi Configuration parsed
* @param string $cmd Command to encapsulate
*
* @return string Content to add to Dockerfile
*/
private function encapsulateCmd($config, $cmd)
{
return sprintf($this->encapsulation[$config['language']], $cmd);
}
}
4 changes: 2 additions & 2 deletions src/Joli/JoliCi/Command/RunCommand.php
Expand Up @@ -12,8 +12,10 @@

use Docker\Docker;
use Docker\Http\Client;
use Joli\JoliCi\Builder;
use Joli\JoliCi\Executor;
use Joli\JoliCi\BuildStrategy\JoliCiBuildStrategy;
use Joli\JoliCi\BuildStrategy\TravisCiBuildStrategy;
use Joli\JoliCi\Log\SimpleFormatter;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
Expand All @@ -23,8 +25,6 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Filesystem;
use Joli\JoliCi\Builder;
use Joli\JoliCi\BuildStrategy\TravisCiBuildStrategy;


class RunCommand extends Command
Expand Down

0 comments on commit 7d85d78

Please sign in to comment.