Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelcontento committed Nov 4, 2015
0 parents commit fd6f655
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2015 Michael Contento

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
[circleci-matrix][]
===================

Small utility to mirror [TravisCI][]'s [build matrix][] on [CircleCI][].

## Installation

**TBD**

## Usage

First you need to define your build matrix in a new file called
`.circleci-matrix.yml` like this:

env:
- VERSION=5.0
- VERSION=4.2
- VERSION=4.1
- VERSION=4.0

command:
- echo 'hi!'
- echo "Version is $VERSION"

Now you're ready to execute it with:

$ circleci-matrix
INFO: circleci-matrix version: 0.1.0
INFO: circleci node total: 1
INFO: circleci node index: 0

INFO: Running env: VERSION=5.0
INFO: Running command: echo 'hi!'
hi!
INFO: Running command: echo "Version is $VERSION"
Version is 5.0

INFO: Running env: VERSION=4.2
INFO: Running command: echo 'hi!'
hi!
INFO: Running command: echo "Version is $VERSION"
Version is 4.2

INFO: Running env: VERSION=4.1
INFO: Running command: echo 'hi!'
hi!
INFO: Running command: echo "Version is $VERSION"
Version is 4.1

INFO: Running env: VERSION=4.0
INFO: Running command: echo 'hi!'
hi!
INFO: Running command: echo "Version is $VERSION"
Version is 4.0

INFO: Done

All commands has been executed with the right value in `$VERSION`.

## Parallelism

[CircleCI][]'s [parallelism][] is supported out of the box! Have a look at the following
example where I set the `CIRCLE_NODE_TOTAL` manually:

$ CIRCLE_NODE_TOTAL=4 circleci-matrix.sh
INFO: circleci-matrix version: 0.1.0
INFO: circleci node total: 4
INFO: circleci node index: 0

INFO: Running env: VERSION=5.0
INFO: Running command: echo 'hi!'
hi!
INFO: Running command: echo "Version is $VERSION"
Version is 5.0

INFO: Skipping env: VERSION=4.2

INFO: Skipping env: VERSION=4.1

INFO: Skipping env: VERSION=4.0

INFO: Done

[circleci-matrix]: https://github.com/michaelcontento/circleci-matrix
[CircleCI]: https://circleci.com/
[TravisCI]: https://travis-ci.org/
[build matrix]: http://docs.travis-ci.com/user/customizing-the-build/#Build-Matrix
[parallelism]: https://circleci.com/docs/setting-up-parallelism
9 changes: 9 additions & 0 deletions example/.circleci-matrix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
env:
- VERSION=5.0
- VERSION=4.2
- VERSION=4.1
- VERSION=4.0

command:
- echo 'hi!'
- echo "Version is $VERSION"
111 changes: 111 additions & 0 deletions src/circleci-matrix.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#!/usr/bin/env bash
set -e

VERSION="0.1.0"
CONFIG_FILE=".circleci-matrix.yml"

# Ensure sane defaults
CIRCLE_NODE_TOTAL=${CIRCLE_NODE_TOTAL:-1}
CIRCLE_NODE_INDEX=${CIRCLE_NODE_INDEX:-0}

error() {
local message=$1
echo >&2 "ERROR: $message"
exit 1
}

info() {
local message=$1
if [ "$message" == "" ]; then
echo ""
else
echo "INFO: $message"
fi
}

ensure_file() {
if [ ! -f $CONFIG_FILE ]; then
error "No $CONFIG_FILE file found!"
fi
}

read_file() {
# 1) Remove leading spaces
# 2) Remove leading dashes
# 3) Remove comment lines
# 4) Remove empty lines
sed \
-e 's/^ *//' \
-e 's/^- //' \
-e '/^#.*/d' \
-e '/^$/d' \
$CONFIG_FILE
}

process_commands() {
local line=""
local mode=""

read_file | while read line; do
# Detect mode
if [ "env:" == "$line" ]; then
mode="env"
continue
elif [ "command:" == "$line" ]; then
mode="command"
continue
fi

# Process commands
if [ "command" == "$mode" ]; then
info "Running command: $line"
eval $line
continue
fi
done
}

process_envs() {
local line=""
local mode=""
local i=0

read_file | while read line; do
# Detect mode
if [ "env:" == "$line" ]; then
mode="env"
continue
elif [ "command:" == "$line" ]; then
mode="command"
continue
fi

# Process envs
if [ "env" == "$mode" ]; then
if [ $(($i % $CIRCLE_NODE_TOTAL)) -eq $CIRCLE_NODE_INDEX ]; then
info "Running env: $line"
export $line
process_commands
else
info "Skipping env: $line"
fi
((i=i+1))
info ""
continue
fi
done
}

main() {
info "circleci-matrix version: $VERSION"
info "circleci node total: $CIRCLE_NODE_TOTAL"
info "circleci node index: $CIRCLE_NODE_INDEX"
info ""

ensure_file
process_envs

info "Done"
}

main

0 comments on commit fd6f655

Please sign in to comment.