Skip to content

Commit

Permalink
Default Environment (#25)
Browse files Browse the repository at this point in the history
* Add ability to set default environment for pipeline.

* Bump version.

* Fix the version.txt file.
  • Loading branch information
erasmussen39 authored May 4, 2021
1 parent 926205f commit 6af5995
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 95 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.code42.jenkins.pipelinekt.core

import com.code42.jenkins.pipelinekt.core.writer.GroovyScript

interface Environment : GroovyScript
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fun generatePipeline(pipeline: Pipeline, outFile: String, indentStr: String = "
}

data class Pipeline(
val environment: List<Environment> = emptyList(),
val agent: Agent? = null,
val tools: List<Tool> = emptyList(),
val options: List<Option> = emptyList(),
Expand All @@ -30,6 +31,9 @@ data class Pipeline(
}

writer.closure("pipeline") { writer ->
if (environment.isNotEmpty()) {
writer.closure("environment", environment::toGroovy)
}
agent?.toGroovy(writer)
if (tools.isNotEmpty()) {
writer.closure("tools", tools::toGroovy)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.code42.jenkins.pipelinekt.dsl

import com.code42.jenkins.pipelinekt.core.Agent
import com.code42.jenkins.pipelinekt.core.Environment
import com.code42.jenkins.pipelinekt.core.Option
import com.code42.jenkins.pipelinekt.core.Parameter
import com.code42.jenkins.pipelinekt.core.Tool
Expand All @@ -11,6 +12,7 @@ import com.code42.jenkins.pipelinekt.dsl.stage.StageWrapperContext
import com.code42.jenkins.pipelinekt.dsl.stage.TopLevelStageContext

data class PipelineContext(
val environmentContext: DslContext<Environment> = DslContext(),
val agentContext: SingletonDslContext<Agent> = SingletonDslContext(),
val triggersContext: DslContext<Trigger> = DslContext(),
val parametersContext: DslContext<Parameter> = DslContext(),
Expand Down Expand Up @@ -45,7 +47,11 @@ data class PipelineContext(
agentContext.agentBlock()
}

fun post(postBlocck: PostContext.() -> Unit) {
postContext.postBlocck()
fun post(postBlock: PostContext.() -> Unit) {
postContext.postBlock()
}

fun environment(environmentBlock: DslContext<Environment>.() -> Unit) {
environmentContext.environmentBlock()
}
}
144 changes: 89 additions & 55 deletions dsl/src/main/kotlin/com/code42/jenkins/pipelinekt/dsl/PipelineDsl.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.code42.jenkins.pipelinekt.dsl

import com.code42.jenkins.pipelinekt.core.Agent
import com.code42.jenkins.pipelinekt.core.Environment
import com.code42.jenkins.pipelinekt.core.Option
import com.code42.jenkins.pipelinekt.core.Pipeline
import com.code42.jenkins.pipelinekt.core.Post
Expand Down Expand Up @@ -34,12 +35,13 @@ fun <T, Dsl : MethodDsl> Dsl.withConfigurationContext(applyConfiguration: Dsl.()
}

data class PipelineDsl(
val defaultEnvironment: DslContext<Environment>.() -> Unit = {},
val defaultBuildOptions: DslContext<Option>.() -> Unit = {
buildDiscarder(logRotator(10, 10, 10, 10))
ansiColor("xterm")
timestamps()
disableConcurrentBuilds()
},
buildDiscarder(logRotator(10, 10, 10, 10))
ansiColor("xterm")
timestamps()
disableConcurrentBuilds()
},
val beforePrepSteps: DslContext<Step>.() -> Unit = { },
val afterPrepSteps: DslContext<Step>.() -> Unit = { },
val beforeLocalStage: DslContext<Step>.() -> Unit = { },
Expand All @@ -48,73 +50,75 @@ data class PipelineDsl(
val afterRemoteStage: DslContext<Step>.() -> Unit = { },
val beforePipelinePost: PostContext.() -> Unit = { },
val afterPipelinePost: PostContext.() -> Unit = {
cleanup {
cleanWs()
}
},
cleanup {
cleanWs()
}
},
val beforeLocalStagePost: PostContext.() -> Unit = { },
val afterLocalStagePost: PostContext.() -> Unit = { },
val beforeRemoteStagePost: PostContext.() -> Unit = { },
val afterRemoteStagePost: PostContext.() -> Unit = {
cleanup {
cleanWs()
}
},
cleanup {
cleanWs()
}
},
val remoteStageOptions: DslContext<StageOption>.() -> Unit = { },
val defaultAgent: SingletonDslContext<Agent>.() -> Unit = { },
override val pipelineMethodRegistry: PipelineMethodRegistry = PipelineMethodRegistry(),
val stages: Deque<Stage> = LinkedBlockingDeque()
) : MethodDsl {

fun DslContext<Option>.defaultBuildOptions() { defaultBuildOptions.invoke(this) }
fun DslContext<Step>.beforePrepSteps() { beforePrepSteps.invoke(this) }
fun DslContext<Step>.afterPrepSteps() { afterPrepSteps.invoke(this) }
fun DslContext<Environment>.defaultEnvirontment() {
defaultEnvironment.invoke(this)
}

fun DslContext<Option>.defaultBuildOptions() {
defaultBuildOptions.invoke(this)
}

fun PostContext.beforePipelinePost() { beforePipelinePost.invoke(this) }
fun PostContext.afterPipelinePost() { afterPipelinePost.invoke(this) }
fun DslContext<Step>.beforePrepSteps() {
beforePrepSteps.invoke(this)
}

fun DslContext<Step>.afterPrepSteps() {
afterPrepSteps.invoke(this)
}

fun PostContext.beforePipelinePost() {
beforePipelinePost.invoke(this)
}

fun PostContext.afterPipelinePost() {
afterPipelinePost.invoke(this)
}

fun pipeline(
prepSteps: DslContext<Step>.() -> Unit = { },
pipelineBlock: PipelineContext.() -> Unit
): Pipeline {
val context = PipelineContext(
topLevelStageContext = topLevelStageWrapperContext(),
pipelineMethodRegistry = pipelineMethodRegistry)
topLevelStageContext = topLevelStageWrapperContext(),
pipelineMethodRegistry = pipelineMethodRegistry
)
context.pipelineBlock()

val pipeline = Pipeline(
agent = context.agentContext.drainAll().firstOrNull() ?: SingletonDslContext.into(defaultAgent),
tools = context.toolContext.drainAll(),
parameters = context.parametersContext.drainAll(),
options = applyDefaultPipelineOptions(context.optionContext.drainAll()),
triggers = context.triggersContext.drainAll(),
stages = prepStage(prepSteps) + context.topLevelStageContext.drainAll(),
post = applyBeforeAndAfterPipelinePost(context.postContext.toPost()),
methods = pipelineMethodRegistry.methods())
environment = applyDefaultEnvironment(context.environmentContext.drainAll()),
agent = context.agentContext.drainAll().firstOrNull() ?: SingletonDslContext.into(defaultAgent),
tools = context.toolContext.drainAll(),
parameters = context.parametersContext.drainAll(),
options = applyDefaultPipelineOptions(context.optionContext.drainAll()),
triggers = context.triggersContext.drainAll(),
stages = prepStage(prepSteps) + context.topLevelStageContext.drainAll(),
post = applyBeforeAndAfterPipelinePost(context.postContext.toPost()),
methods = pipelineMethodRegistry.methods()
)
pipelineMethodRegistry.reset()
return pipeline
}

private fun topLevelStageWrapperContext(): StageWrapperContext<TopLevelStageContext> =
StageWrapperContext(
pipelineMethodRegistry,
beforeLocalStage,
afterLocalStage,
beforeRemoteStage,
afterRemoteStage,
beforeLocalStagePost,
afterLocalStagePost,
beforeRemoteStagePost,
afterRemoteStagePost,
remoteStageOptions,
defaultAgent,
{ TopLevelStageContext(
parallelStageContext = nestedStageWrapperContext(),
nestedStageContext = nestedStageWrapperContext(),
matrixContext = MatrixContext(nestedStageWrapperContext())) },
LinkedBlockingDeque())

private fun nestedStageWrapperContext(): StageWrapperContext<NestedStageContext> = StageWrapperContext(
pipelineMethodRegistry,
beforeLocalStage,
afterLocalStage,
Expand All @@ -126,8 +130,31 @@ data class PipelineDsl(
afterRemoteStagePost,
remoteStageOptions,
defaultAgent,
{ NestedStageContext(nestedStageContext = nestedStageWrapperContext()) },
LinkedBlockingDeque())
{
TopLevelStageContext(
parallelStageContext = nestedStageWrapperContext(),
nestedStageContext = nestedStageWrapperContext(),
matrixContext = MatrixContext(nestedStageWrapperContext())
)
},
LinkedBlockingDeque()
)

private fun nestedStageWrapperContext(): StageWrapperContext<NestedStageContext> = StageWrapperContext(
pipelineMethodRegistry,
beforeLocalStage,
afterLocalStage,
beforeRemoteStage,
afterRemoteStage,
beforeLocalStagePost,
afterLocalStagePost,
beforeRemoteStagePost,
afterRemoteStagePost,
remoteStageOptions,
defaultAgent,
{ NestedStageContext(nestedStageContext = nestedStageWrapperContext()) },
LinkedBlockingDeque()
)

private fun prepStage(stepsBlock: DslContext<Step>.() -> Unit): List<Stage> {

Expand All @@ -139,13 +166,13 @@ data class PipelineDsl(

if (!prepSteps.isEmpty()) {
return topLevelStageWrapperContext()
.into {
stage("Prep") {
steps {
add(prepSteps.toStep())
}
.into {
stage("Prep") {
steps {
add(prepSteps.toStep())
}
}
}
} else {
return emptyList()
}
Expand All @@ -163,10 +190,17 @@ data class PipelineDsl(
return beforePost.merge(post).merge(afterPost)
}

private fun applyDefaultEnvironment(environment: List<Environment>): List<Environment> {
val defaultEnvironment = DslContext
.into<Environment> { defaultEnvirontment() }
.filter { defaultEnvironment -> environment.none { it.javaClass == defaultEnvironment.javaClass } }
return defaultEnvironment + environment
}

private fun applyDefaultPipelineOptions(options: List<Option>): List<Option> {
val defaultOptions = DslContext
.into<Option> { defaultBuildOptions() }
.filter { defaultOption -> options.none { it.javaClass == defaultOption.javaClass } }
.into<Option> { defaultBuildOptions() }
.filter { defaultOption -> options.none { it.javaClass == defaultOption.javaClass } }
return defaultOptions + options
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.code42.jenkins.pipelinekt.dsl.environment

import com.code42.jenkins.pipelinekt.core.Environment
import com.code42.jenkins.pipelinekt.dsl.DslContext
import com.code42.jenkins.pipelinekt.internal.environment.EnvVar

fun DslContext<Environment>.envVar(name: String, value: String) {
add(EnvVar(name, value))
}
Loading

0 comments on commit 6af5995

Please sign in to comment.