cinderella

[unmaintained] simple CI engine
Log | Files | Refs | README | LICENSE

commit acc19061ee94b24b170492b156c1b65017ed7821
parent 9af221159393faaaa779c5978bee7cffd7651dec
Author: Stefan Koch <programming@stefan-koch.name>
Date:   Sun, 15 Sep 2019 15:58:09 +0200

substitute variable names with build context

Diffstat:
M.cinderella.toml | 4+++-
MREADME.md | 6++++++
Msrc/execution.rs | 26+++++++++++++++++++++++---
Msrc/lib.rs | 8+++++++-
4 files changed, 39 insertions(+), 5 deletions(-)

diff --git a/.cinderella.toml b/.cinderella.toml @@ -6,5 +6,7 @@ commands = [ [build-release] commands = [ "cargo build --release", - "cp target/release/cinderella /opt/cinderella/", + "cp target/release/cinderella /opt/cinderella/.cinderella-{{ branch }}", + "rm /opt/cinderella/cinderella-{{ branch }}", + "mv /opt/cinderella/.cinderella-{{ branch }} /opt/cinderella/cinderella-{{ branch }}", ] diff --git a/README.md b/README.md @@ -40,3 +40,9 @@ commands = [ Pipelines are executed in the order in which they are defined. For the given configuration file it is ensured that first `test` is being executed followed by `build-release`. + +You can use variables in your build commands. These variables will get +substituted with actual values from the build context. Currently supported +variables are: + +- `{{ branch }}`: The name of the branch that is built diff --git a/src/execution.rs b/src/execution.rs @@ -1,19 +1,25 @@ +use std::collections::HashMap; use std::process::Command; use crate::pipeline; -pub fn execute(pipelines: &Vec<pipeline::Pipeline>) { +pub fn execute(pipelines: &Vec<pipeline::Pipeline>, + variables: &HashMap<String, String>) { for pipeline in pipelines { - execute_pipeline(pipeline); + execute_pipeline(pipeline, &variables); } } -pub fn execute_pipeline(pipeline: &pipeline::Pipeline) { +pub fn execute_pipeline(pipeline: &pipeline::Pipeline, + variables: &HashMap<String, String>) { println!("Executing Pipeline \"{}\"", pipeline.name); for cmd in &pipeline.commands { println!("Step: {}", cmd); + let cmd = replace_variables(&cmd, &variables); + // TODO: Raise error if some variables remain unsubstituted? + // TODO: Successful argument parsing needs a lot more details, // e.g. for quoted arguments like myprogram "argument 1" // but for a first shot this works @@ -27,3 +33,17 @@ pub fn execute_pipeline(pipeline: &pipeline::Pipeline) { assert!(status.success()); } } + +fn replace_variables(command: &str, variables: &HashMap<String, String>) + -> String +{ + let mut res = String::from(command); + + for (original, replacement) in variables { + // replace "{{ varname }}" with replacement value + let varname = format!("{{{{ {} }}}}", original); + res = res.replace(&varname, replacement); + } + + res +} diff --git a/src/lib.rs b/src/lib.rs @@ -1,3 +1,4 @@ +use std::collections::HashMap; use std::env; use std::path::PathBuf; @@ -46,10 +47,15 @@ pub fn run(repo_ptr: &RepoPointer) { println!("Workdir is at {:?}", workdir.path); + // setup variables for pipelines + let mut variables = HashMap::new(); + // checkout the branch if a branch was provided if let Some(branch) = &repo_ptr.branch { println!("Switching to branch {}", branch); workdir.checkout_branch(&branch); + + variables.insert("branch".to_string(), branch.to_string()); } // Switch to the exported work dir so that all commands @@ -58,7 +64,7 @@ pub fn run(repo_ptr: &RepoPointer) { let cinderella_file = cinderella_file(&workdir.path); if let Some(pipelines) = pipeline::load_pipeline(&cinderella_file) { - execution::execute(&pipelines); + execution::execute(&pipelines, &variables); } else { println!("No Cinderella configuration found"); }