aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Rendina <rendinam@users.noreply.github.com>2018-06-11 12:25:11 -0400
committerGitHub <noreply@github.com>2018-06-11 12:25:11 -0400
commit9883adb887574f646881a04d436b840466def4d7 (patch)
tree9e8be0e114d9c3beb7b77049769efb671465b7b0
parent53433e4e32dd894447f1581b826aa3c7dfa9a8c9 (diff)
downloadjscu_refactor-9883adb887574f646881a04d436b840466def4d7.tar.gz
Condavenience (#4)1.1.0
* Add BuildConfig properties to support automatic conda environment creation for hosting the job when conda_packages list property is populated. * `.conda_packages = []` * `.conda_override_channels = false` * `.conda_channels = []`
-rw-r--r--.gitignore1
-rw-r--r--src/BuildConfig.groovy4
-rw-r--r--vars/utils.groovy83
3 files changed, 65 insertions, 23 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1377554
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.swp
diff --git a/src/BuildConfig.groovy b/src/BuildConfig.groovy
index 5fd1687..1eb98be 100644
--- a/src/BuildConfig.groovy
+++ b/src/BuildConfig.groovy
@@ -4,6 +4,10 @@ package BuildConfig;
class BuildConfig implements Serializable {
def nodetype = ""
def build_mode = ""
+ def name = ""
+ def conda_packages = []
+ def conda_override_channels = false
+ def conda_channels = []
def env_vars = []
def env_vars_raw = []
def build_cmds = []
diff --git a/vars/utils.groovy b/vars/utils.groovy
index 954f110..536c5b6 100644
--- a/vars/utils.groovy
+++ b/vars/utils.groovy
@@ -41,6 +41,7 @@ def scm_checkout(skip_disable=false) {
def run(configs, concurrent = true) {
def tasks = [:]
for (config in configs) {
+
def myconfig = new BuildConfig() // MUST be inside for loop.
myconfig = SerializationUtils.clone(config)
@@ -49,40 +50,76 @@ def run(configs, concurrent = true) {
tasks["${config.nodetype}/${config.build_mode}"] = {
node(config.nodetype) {
def runtime = []
+ // If conda packages were specified, create an environment containing
+ // them and then 'activate' it.
+ if (myconfig.conda_packages.size() > 0) {
+ def env_name = "tmp_env"
+ def conda_exe = sh(script: "which conda", returnStdout: true).trim()
+ def conda_root = conda_exe.replace("/bin/conda", "").trim()
+ def conda_prefix = "${conda_root}/envs/${env_name}".trim()
+ def packages = ""
+ for (pkg in myconfig.conda_packages) {
+ packages = "${packages} ${pkg}"
+ }
+ // Override removes the implicit 'defaults' channel from the channels
+ // to be used, The conda_channels list is then used verbatim (in
+ // (priority order) by conda.
+ def override = ""
+ if (myconfig.conda_override_channels.toString() == 'true') {
+ override = "--override-channels"
+ }
+ def chans = ""
+ for (chan in myconfig.conda_channels) {
+ chans = "${chans} -c ${chan}"
+ }
+ sh(script: "conda create -q -y -n ${env_name} ${override} ${chans} ${packages}")
+ // Configure job to use this conda environment.
+ myconfig.env_vars.add(0, "CONDA_SHLVL=1")
+ myconfig.env_vars.add(0, "CONDA_PROMPT_MODIFIER=${env_name}")
+ myconfig.env_vars.add(0, "CONDA_EXE=${conda_exe}")
+ myconfig.env_vars.add(0, "CONDA_PREFIX=${conda_prefix}")
+ myconfig.env_vars.add(0, "CONDA_PYTHON_EXE=${conda_prefix}/bin/python")
+ myconfig.env_vars.add(0, "CONDA_DEFAULT_ENV=${env_name}")
+ // Prepend the PATH var adjustment to the list that gets processed below.
+ def conda_path = "PATH=${conda_prefix}/bin:$PATH"
+ myconfig.env_vars.add(0, conda_path)
+ }
// Expand environment variable specifications by using the shell
// to dereference any var references and then render the entire
// value as a canonical path.
for (var in myconfig.env_vars) {
- def varName = var.tokenize("=")[0].trim()
- def varValue = var.tokenize("=")[1].trim()
- // examine var value, if it contains var refs, expand them.
- def expansion = varValue
- if (varValue.contains("\$")) {
- expansion = sh(script: "echo \"${varValue}\"", returnStdout: true)
- }
+ // Process each var in an environment defined by all the prior vars.
+ withEnv(runtime) {
+ def varName = var.tokenize("=")[0].trim()
+ def varValue = var.tokenize("=")[1].trim()
+ // examine var value, if it contains var refs, expand them.
+ def expansion = varValue
+ if (varValue.contains("\$")) {
+ expansion = sh(script: "echo \"${varValue}\"", returnStdout: true)
+ }
- // Change values of '.' and './' to the node's WORKSPACE.
- // Replace a leading './' with the node's WORKSPACE.
- if (expansion == '.' || expansion == './') {
- expansion = env.WORKSPACE
- } else if(expansion.size() > 2 && expansion[0..1] == './') {
- expansion = "${env.WORKSPACE}/${expansion[2..-1]}"
- }
+ // Change values of '.' and './' to the node's WORKSPACE.
+ // Replace a leading './' with the node's WORKSPACE.
+ if (expansion == '.' || expansion == './') {
+ expansion = env.WORKSPACE
+ } else if(expansion.size() > 2 && expansion[0..1] == './') {
+ expansion = "${env.WORKSPACE}/${expansion[2..-1]}"
+ }
- // Replace all ':.' combinations with the node's WORKSPACE.
- expansion = expansion.replaceAll(':\\.', ":${env.WORKSPACE}")
+ // Replace all ':.' combinations with the node's WORKSPACE.
+ expansion = expansion.replaceAll(':\\.', ":${env.WORKSPACE}")
- // Convert var value to canonical based on a WORKSPACE base directory.
- if (expansion.contains('..')) {
- expansion = new File(expansion).getCanonicalPath()
- }
- expansion = expansion.trim()
- runtime.add("${varName}=${expansion}")
+ // Convert var value to canonical based on a WORKSPACE base directory.
+ if (expansion.contains('..')) {
+ expansion = new File(expansion).getCanonicalPath()
+ }
+ expansion = expansion.trim()
+ runtime.add("${varName}=${expansion}")
+ } // end withEnv
}
for (var in myconfig.env_vars_raw) {
runtime.add(var)
}
- println(runtime)
withEnv(runtime) {
stage("Build (${myconfig.build_mode})") {
unstash "source_tree"