From 5b2cacff128baefaff5b988e46388c492472e040 Mon Sep 17 00:00:00 2001 From: "Stephen M. Coakley" Date: Thu, 20 Feb 2020 18:48:16 -0600 Subject: [PATCH] Fix wonky variable resolution in standalone pipeline scripts --- .buildkite/pipeline.extra-steps.gradle | 7 +++++ build.gradle | 8 ++--- .../plugins/buildkite/BuildkitePlugin.groovy | 20 ++++++++++--- .../buildkite/ConfigurableEnvironment.groovy | 2 +- .../plugins/buildkite/PipelineScript.groovy | 30 +++++++++++++++++++ 5 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 buildSrc/src/main/groovy/com/widen/plugins/buildkite/PipelineScript.groovy diff --git a/.buildkite/pipeline.extra-steps.gradle b/.buildkite/pipeline.extra-steps.gradle index 4738afe..4e8f65d 100644 --- a/.buildkite/pipeline.extra-steps.gradle +++ b/.buildkite/pipeline.extra-steps.gradle @@ -19,6 +19,13 @@ commandStep { } commandStep { + command "run-unit-tests.sh" + + environment { + // In standalone pipeline files we can access the root project via "project". + PROJECT_VERSION = project.version + } + // Common Buildkite plugins are also part of the DSL. dockerCompose { run 'unit' diff --git a/build.gradle b/build.gradle index 8f75634..bb30c27 100644 --- a/build.gradle +++ b/build.gradle @@ -20,12 +20,8 @@ jar { from "${rootProject.rootDir}/buildSrc/build/classes/groovy/main" } -System.getenv("GRADLE_PUBLISH_KEY").with { - System.setProperty("gradle.publish.key", it) -} -System.getenv("GRADLE_PUBLISH_SECRET").with { - System.setProperty("gradle.publish.secret", it) -} +System.setProperty("gradle.publish.key", System.getenv("GRADLE_PUBLISH_KEY") ?: "") +System.setProperty("gradle.publish.secret", System.getenv("GRADLE_PUBLISH_SECRET") ?: "") gradlePlugin { plugins { diff --git a/buildSrc/src/main/groovy/com/widen/plugins/buildkite/BuildkitePlugin.groovy b/buildSrc/src/main/groovy/com/widen/plugins/buildkite/BuildkitePlugin.groovy index 7e1c45f..14c7021 100644 --- a/buildSrc/src/main/groovy/com/widen/plugins/buildkite/BuildkitePlugin.groovy +++ b/buildSrc/src/main/groovy/com/widen/plugins/buildkite/BuildkitePlugin.groovy @@ -1,5 +1,6 @@ package com.widen.plugins.buildkite +import org.codehaus.groovy.control.CompilerConfiguration import org.gradle.api.Plugin import org.gradle.api.Project @@ -24,7 +25,10 @@ class BuildkitePlugin implements Plugin { // Run anything that needs to be done after plugin configuration has been evaluated. project.afterEvaluate { if (extension.includeScripts) { - def shell = new GroovyShell(project.buildscript.classLoader) + def shell = new GroovyShell(project.buildscript.classLoader, new Binding(project: project), new + CompilerConfiguration( + scriptBaseClass: PipelineScript.class.name + )) project.fileTree(project.rootDir) { include '.buildkite/pipeline*.gradle' @@ -34,13 +38,21 @@ class BuildkitePlugin implements Plugin { word.capitalize() } } ?: 'default' - def closure = (Closure) shell.evaluate("{buildkite -> ${file.text}}", file.name) - extension.pipeline(pipelineName, closure) + + def script = (PipelineScript) shell.parse(file) + + extension.pipeline(pipelineName) { BuildkitePipeline pipeline -> + println(pipeline) + script.setPipeline(pipeline) + script.setBuildkite(extension) + script.setProject(project) + script.run() + } } } extension.pipelines.each { name, config -> - def taskName = name == 'default' ? 'uploadPipeline' : "upload${name}Pipeline" + def taskName = name == 'default' ? 'uploadPipeline' : "upload${name.capitalize()}Pipeline" project.tasks.create(taskName, UploadPipelineTask) { pipelineConfigure = config diff --git a/buildSrc/src/main/groovy/com/widen/plugins/buildkite/ConfigurableEnvironment.groovy b/buildSrc/src/main/groovy/com/widen/plugins/buildkite/ConfigurableEnvironment.groovy index dc9b996..fa889b8 100644 --- a/buildSrc/src/main/groovy/com/widen/plugins/buildkite/ConfigurableEnvironment.groovy +++ b/buildSrc/src/main/groovy/com/widen/plugins/buildkite/ConfigurableEnvironment.groovy @@ -26,7 +26,7 @@ trait ConfigurableEnvironment { closure = (Closure) closure.clone() closure.delegate = map closure.resolveStrategy = Closure.OWNER_FIRST - closure() + closure.call() environment(map) } } diff --git a/buildSrc/src/main/groovy/com/widen/plugins/buildkite/PipelineScript.groovy b/buildSrc/src/main/groovy/com/widen/plugins/buildkite/PipelineScript.groovy new file mode 100644 index 0000000..95bea93 --- /dev/null +++ b/buildSrc/src/main/groovy/com/widen/plugins/buildkite/PipelineScript.groovy @@ -0,0 +1,30 @@ +package com.widen.plugins.buildkite + +import org.gradle.api.Project + +/** + * Base class for scripts + */ +abstract class PipelineScript extends Script { + BuildkitePipeline pipeline + BuildkitePlugin.Extension buildkite + Project project + + Object getProperty(String property) { + if ('buildkite' == property) { + return buildkite + } + if ('project' == property) { + return project + } + return pipeline.getProperty(property) + } + + void setProperty(String property, Object newValue) { + pipeline.setProperty(property, newValue) + } + + Object invokeMethod(String name, Object args) { + return pipeline.invokeMethod(name, args) + } +}