Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customizable stage names #133

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/net/wooga/jenkins/pipeline/check/CheckCreator.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CheckCreator {
Closure junitCheck(Platform platform, Step testStep, Step analysisStep) {
def mainClosure = createCheck(testStep, analysisStep).pack(platform)
def catchClosure = {throw it}
def finallyClosure = {
def finallyClosure = { _ ->
jenkins.junit allowEmptyResults: true, testResults: "**/build/test-results/**/*.xml"
}

Expand All @@ -35,7 +35,7 @@ class CheckCreator {
throw e
}
}
def finallyClosure = {
def finallyClosure = { _ ->
jenkins.nunit failIfNoResults: false, testResultsPattern: '**/build/reports/unity/test*/*.xml'
jenkins.archiveArtifacts artifacts: '**/build/logs/**/*.log', allowEmptyArchive: true
jenkins.archiveArtifacts artifacts: '**/build/reports/unity/**/*.xml', allowEmptyArchive: true
Expand Down
12 changes: 6 additions & 6 deletions src/net/wooga/jenkins/pipeline/check/Checks.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ class Checks {
Docker docker
Gradle gradle
GradleSteps steps
EnclosureCreator enclosureCreator
NodeCreator nodeCreator
Enclosures enclosures
CheckCreator checkCreator

//jenkins CPS-transformations doesn't work inside constructors, so we have to keep these as simple as possible.
//for non-trivial constructors, prefer static factories.
static Checks create(Object jenkinsScript, Docker docker, Gradle gradle, int buildNumber) {
def enclosureCreator = new EnclosureCreator(jenkinsScript, buildNumber)
def enclosures = new Enclosures(jenkinsScript, docker, enclosureCreator)
def enclosureCreator = new NodeCreator(jenkinsScript)
def enclosures = new Enclosures(jenkinsScript, docker, enclosureCreator, buildNumber)
def checkCreator = new CheckCreator(jenkinsScript, enclosures)
def steps = new GradleSteps(jenkinsScript, gradle)

return new Checks(docker, gradle, steps, enclosureCreator, enclosures, checkCreator)
}

private Checks(Docker docker, Gradle gradle, GradleSteps steps, EnclosureCreator enclosureCreator,
Enclosures enclosures, CheckCreator checkCreator) {
private Checks(Docker docker, Gradle gradle, GradleSteps steps, NodeCreator enclosureCreator,
Enclosures enclosures, CheckCreator checkCreator) {
this.docker = docker
this.gradle = gradle
this.steps = steps
this.enclosureCreator = enclosureCreator
this.nodeCreator = enclosureCreator
this.enclosures = enclosures
this.checkCreator = checkCreator
}
Expand Down
36 changes: 0 additions & 36 deletions src/net/wooga/jenkins/pipeline/check/EnclosureCreator.groovy

This file was deleted.

30 changes: 21 additions & 9 deletions src/net/wooga/jenkins/pipeline/check/Enclosures.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,42 @@ class Enclosures {

private Object jenkins
private Docker docker
private EnclosureCreator enclosureCreator
private NodeCreator enclosureCreator
private int buildNumber

Enclosures(Object jenkins, Docker docker, EnclosureCreator enclosureCreator) {
Enclosures(Object jenkins, Docker docker, NodeCreator enclosureCreator, int buildNumber) {
this.jenkins = jenkins
this.docker = docker
this.enclosureCreator = enclosureCreator
this.buildNumber = buildNumber
}

def withDocker(Platform platform, PackedStep mainCls, Closure catchCls = {throw it}, PackedStep finallyCls = {}) {
return enclosureCreator.withNodeAndEnv(platform,
Closure withDocker(Platform platform, PackedStep mainCls, Closure catchCls = {throw it}, Closure finallyCls = {ex ->}) {
return forPlatform(platform,
withCheckout(platform.checkoutDirectory, { docker.runOnImage(mainCls) }),
catchCls,
withCleanup(platform.clearWs, finallyCls)
)
}

def simple(Platform platform, PackedStep mainClosure, Closure catchCls = {throw it}, PackedStep finallyCls = {}) {
return enclosureCreator.withNodeAndEnv(platform,
Closure simple(Platform platform, PackedStep mainClosure, Closure catchCls = {throw it}, Closure finallyCls = {ex ->}) {
return forPlatform(platform,
withCheckout(platform.checkoutDirectory, mainClosure),
catchCls,
withCleanup(platform.clearWs, finallyCls)
)
}

private Closure forPlatform(Platform platform, PackedStep mainClosure,
Closure catchCls = {throw it}, Closure finallyCls = {ex -> }) {
def testEnvironment = platform.testEnvironment +
["TRAVIS_JOB_NUMBER=${buildNumber}.${platform.name.toUpperCase()}"] as List<String>
def platformLabels = platform.generateTestLabelsString()
def nodeLabels = platformLabels && !platformLabels.empty ?
"atlas && ${platform.generateTestLabelsString()}" : "atlas"
return enclosureCreator.nodeWithEnv(nodeLabels, testEnvironment, mainClosure.&call, catchCls, finallyCls)
}

private PackedStep withCheckout(String checkoutDir, PackedStep step) {
return {
jenkins.dir(checkoutDir) {
Expand All @@ -41,9 +53,9 @@ class Enclosures {
}
}

private PackedStep withCleanup(boolean hasCleanup, PackedStep step) {
return {
step()
private Closure withCleanup(boolean hasCleanup, Closure step) {
return { exception ->
step(exception)
if(hasCleanup) {
jenkins.cleanWs()
}
Expand Down
80 changes: 80 additions & 0 deletions src/net/wooga/jenkins/pipeline/check/NodeCreator.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package net.wooga.jenkins.pipeline.check

class NodeCreator {

final Object jenkins

NodeCreator(Object jenkins) {
this.jenkins = jenkins
}

Closure nodeWithEnv(String nodeLabels, List<String> environment,
Closure mainCls, Closure catchCls, Closure finallyCls) {
return {
jenkins.node(nodeLabels) {
runSteps(environment, [], mainCls, catchCls, finallyCls)
}
}
}

Closure node(Closure paramsCls) {
def paramsMap = [:]
def clsClone = paramsCls.clone() as Closure
clsClone.setDelegate(paramsMap)
clsClone(paramsMap)
return node(paramsMap)
}

Closure node(Map params) {
def fullParams = [
label : null,
when : { -> true },
environment: [:],
credentials: [],
steps : {},
onError : {ex -> throw ex},
after : { maybeException -> }
]
fullParams.putAll(params)
node(fullParams.label as String,
fullParams.environment as Map<String, String>,
fullParams.credentials as List,
fullParams.when as Closure<Boolean>,
fullParams.steps as Closure,
fullParams.onError as Closure,
fullParams.after as Closure)
}

Closure node(String label, Map<String, String> environment, List<Object> credentials, Closure<Boolean> when = { -> true },
Closure steps, Closure onError, Closure after) {
return {
if (when()) {
def envList = environment.collect {"${it.key}=${it.value}".toString() }
if (label) {
jenkins.node(label) {
runSteps(envList, credentials, steps, onError, after)
}
} else {
runSteps(envList, credentials, steps, onError, after)
}
}
}
}

private def runSteps(List<String> environment, List credentials, Closure steps, Closure onError, Closure after) {
def maybeException = null as Exception
jenkins.withEnv(environment) {
jenkins.withCredentials(credentials) {
try {
steps.call()
} catch (Exception e) {
maybeException = e
onError?.call(e)
} finally {
after?.call(maybeException)
}
}
}
}

}
30 changes: 0 additions & 30 deletions src/net/wooga/jenkins/pipeline/config/JavaConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -62,34 +62,4 @@ class JavaConfig implements PipelineConfig {
PipelineTools getPipelineTools() {
return PipelineTools.fromConfig(baseConfig.jenkins, this)
}

boolean equals(o) {
if (this.is(o)) return true
if (getClass() != o.class) return false

JavaConfig that = (JavaConfig) o

if (checkArgs != that.checkArgs) return false
if (conventions != that.conventions) return false
if (dockerArgs != that.dockerArgs) return false
if (gradleArgs != that.gradleArgs) return false
if (jenkins != that.jenkins) return false
if (metadata != that.metadata) return false
if (!Arrays.equals(platforms, that.platforms)) return false

return true
}

int hashCode() {
int result
result = (jenkins != null ? jenkins.hashCode() : 0)
result = 31 * result + (conventions != null ? conventions.hashCode() : 0)
result = 31 * result + (platforms != null ? Arrays.hashCode(platforms) : 0)
result = 31 * result + (metadata != null ? metadata.hashCode() : 0)
result = 31 * result + (gradleArgs != null ? gradleArgs.hashCode() : 0)
result = 31 * result + (dockerArgs != null ? dockerArgs.hashCode() : 0)
result = 31 * result + (checkArgs != null ? checkArgs.hashCode() : 0)
return result
}

}
2 changes: 2 additions & 0 deletions src/net/wooga/jenkins/pipeline/config/Platform.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Platform {
}

static Platform forJS(String platformName, Map config, boolean isMain) {

return new Platform(
(config.checkoutDir?: ".") as String,
(config.checkDir?: ".") as String,
Expand Down Expand Up @@ -96,6 +97,7 @@ class Platform {
}

List<String> getTestEnvironment() {

return testEnv.collect { item ->
if (item instanceof Closure) {
return item.call().toString()
Expand Down
6 changes: 4 additions & 2 deletions src/net/wooga/jenkins/pipeline/stages/Stage.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ package net.wooga.jenkins.pipeline.stages
import net.wooga.jenkins.pipeline.config.PipelineConfig

class Stage {
String name
Closure when
Closure action

static Stage fromClosure(Map jenkinsParams, PipelineConfig config, Closure cls) {
def clsClone = cls.clone() as Closure
def stage = new Stage(null, null)
def stage = new Stage(null, null, null)
clsClone.delegate = stage
clsClone(stage, jenkinsParams, config)
return stage
}

Stage(Closure when, Closure action) {
Stage(String name, Closure when, Closure action) {
this.name = name
this.when = when
this.action = action
}
Expand Down
4 changes: 2 additions & 2 deletions src/net/wooga/jenkins/pipeline/stages/Stages.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Stages {

static Stages standard(Map jenkinsParams, PipelineConfig config) {
return new Stages(
new Stage(null, null),
new Stage(null, null),
new Stage(null, null, null),
new Stage(null, null, null),
{ Closure cls -> Stage.fromClosure(jenkinsParams , config, cls) })
}

Expand Down
9 changes: 3 additions & 6 deletions test/groovy/scripts/BuildGradlePluginSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,15 @@ class BuildGradlePluginSpec extends DeclarativeJenkinsSpec {
def buildGradlePlugin = loadSandboxedScript(SCRIPT_PATH) {
params.RELEASE_TYPE = "not-snapshot"
params.RELEASE_SCOPE = "any"
env.GRGIT_USR = "usr"
env.GRGIT_PSW = "pwd"
}

when: "running buildGradlePlugin pipeline"
inSandbox { buildGradlePlugin() }

then: "sets up GRGIT environment"
def env = buildGradlePlugin.binding.env
env["GRGIT"] == credentials['github_access']
env["GRGIT_USER"] == "usr" //"${GRGIT_USR}"
env["GRGIT_PASS"] == "pwd" //"${GRGIT_PSW}"
def env = usedEnvironments.last() as Map<String, String>
env["GRGIT_USER"] == "usr"
env["GRGIT_PASS"] == "pwd"
and: "sets up github environment"
env["GITHUB_LOGIN"] == "usr" //"${GRGIT_USR}"
env["GITHUB_PASSWORD"] == "pwd" //"${GRGIT_PSW}"
Expand Down
16 changes: 6 additions & 10 deletions test/groovy/scripts/BuildJavaLibraryOSSRHSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,23 @@ class BuildJavaLibraryOSSRHSpec extends DeclarativeJenkinsSpec {
currentBuild["result"] = null
params.RELEASE_TYPE = "not-snapshot"
params.RELEASE_SCOPE = "any"
env.GRGIT_USR = "usr"
env.GRGIT_PSW = "pwd"
}

when: "running buildJavaLibrary pipeline"
inSandbox { buildJavaLibrary() }

then: "sets up GRGIT environment"
def env = buildJavaLibrary.binding.env
env["GRGIT"] == credentials['github_access']
def env = usedEnvironments.first()
env["GRGIT_USER"] == "usr" //"${GRGIT_USR}"
env["GRGIT_PASS"] == "pwd" //"${GRGIT_PSW}"
and: "sets up github environment"
env["GITHUB_LOGIN"] == "usr" //"${GRGIT_USR}"
env["GITHUB_PASSWORD"] == "pwd" //"${GRGIT_PSW}"
and: "sets up ossrh keys"
def publishEnv = usedEnvironments.last()
publishEnv["OSSRH_USERNAME"] == "user"
publishEnv["OSSRH_PASSWORD"] == "key"
publishEnv["OSSRH_SIGNING_KEY"] == "signing_key"
publishEnv["OSSRH_SIGNING_KEY_ID"] == "signing_key_id"
publishEnv["OSSRH_SIGNING_PASSPHRASE"] == "signing_passwd"
env["OSSRH_USERNAME"] == "user"
env["OSSRH_PASSWORD"] == "key"
env["OSSRH_SIGNING_KEY"] == "signing_key"
env["OSSRH_SIGNING_KEY_ID"] == "signing_key_id"
env["OSSRH_SIGNING_PASSPHRASE"] == "signing_passwd"
}
}
5 changes: 1 addition & 4 deletions test/groovy/scripts/BuildJavaLibrarySpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,13 @@ class BuildJavaLibrarySpec extends DeclarativeJenkinsSpec {
currentBuild["result"] = null
params.RELEASE_TYPE = "not-snapshot"
params.RELEASE_SCOPE = "any"
env.GRGIT_USR = "usr"
env.GRGIT_PSW = "pwd"
}

when: "running buildJavaLibrary pipeline"
inSandbox { buildJavaLibrary() }

then: "sets up GRGIT environment"
def env = buildJavaLibrary.binding.env
env["GRGIT"] == credentials['github_access']
def env = usedEnvironments.last()
env["GRGIT_USER"] == "usr" //"${GRGIT_USR}"
env["GRGIT_PASS"] == "pwd" //"${GRGIT_PSW}"
and: "sets up github environment"
Expand Down
Loading