From fd6f65597c4b1fc7f67a2eab862943666a6d4517 Mon Sep 17 00:00:00 2001 From: Michael Contento Date: Wed, 4 Nov 2015 10:53:42 +0100 Subject: [PATCH] initial commit --- LICENSE.txt | 20 +++++++ README.md | 88 +++++++++++++++++++++++++++ example/.circleci-matrix.yml | 9 +++ src/circleci-matrix.sh | 111 +++++++++++++++++++++++++++++++++++ 4 files changed, 228 insertions(+) create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 example/.circleci-matrix.yml create mode 100755 src/circleci-matrix.sh diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..8facf8a --- /dev/null +++ b/LICENSE.txt @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..1cc92f7 --- /dev/null +++ b/README.md @@ -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 diff --git a/example/.circleci-matrix.yml b/example/.circleci-matrix.yml new file mode 100644 index 0000000..4c27ed2 --- /dev/null +++ b/example/.circleci-matrix.yml @@ -0,0 +1,9 @@ +env: + - VERSION=5.0 + - VERSION=4.2 + - VERSION=4.1 + - VERSION=4.0 + +command: + - echo 'hi!' + - echo "Version is $VERSION" diff --git a/src/circleci-matrix.sh b/src/circleci-matrix.sh new file mode 100755 index 0000000..17169d2 --- /dev/null +++ b/src/circleci-matrix.sh @@ -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