Skip to content

Commit

Permalink
Fix wonky variable resolution in standalone pipeline scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
sagebind committed Feb 21, 2020
1 parent 05ec13a commit 5b2cacf
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .buildkite/pipeline.extra-steps.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
8 changes: 2 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -24,7 +25,10 @@ class BuildkitePlugin implements Plugin<Project> {
// 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'
Expand All @@ -34,13 +38,21 @@ class BuildkitePlugin implements Plugin<Project> {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait ConfigurableEnvironment {
closure = (Closure) closure.clone()
closure.delegate = map
closure.resolveStrategy = Closure.OWNER_FIRST
closure()
closure.call()
environment(map)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 5b2cacf

Please sign in to comment.