Skip to content

Commit

Permalink
make context parameter replacement available to every task
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaslin committed Apr 29, 2015
1 parent 03397c2 commit 244482e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package com.netflix.spinnaker.orca.batch

import com.netflix.spinnaker.orca.TaskResult
import com.netflix.spinnaker.orca.pipeline.model.Execution
import com.netflix.spinnaker.orca.pipeline.model.Pipeline
import com.netflix.spinnaker.orca.pipeline.model.Stage
import com.netflix.spinnaker.orca.pipeline.util.ContextParameterProcessor
import groovy.transform.Canonical
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
Expand All @@ -42,6 +44,14 @@ class ExecutionContextManager {
}
}

if(stage.context) {
def augmentedContext = [:] + stage.context
if (stage.execution instanceof Pipeline) {
augmentedContext.put('trigger', ((Pipeline) stage.execution).trigger)
}
stage.context.putAll(ContextParameterProcessor.process(stage.context, augmentedContext))
}

return stage
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,26 @@ class ExecutionContextManagerSpec extends Specification {
["key": "value"] | ["global-key": "global-value"] || ["key": "value"]
[:] | ["global-key": "global-value"] || ["key": "global-value"]
}

@Unroll
def "should convert SPEL expressions into actual values"() {
given:
def stage = new PipelineStage(new Pipeline(), null, [ "key": "normal-string", "replaceKey": '${#alphanumerical(key)}' ] )
def chunkContext = Mock(ChunkContext) {
1 * getStepContext() >> {
return Mock(StepContext) {
1 * getJobExecutionContext() >> {
return [:]
}
}
}
}

when:
ExecutionContextManager.retrieve(stage, chunkContext)

then:
stage.context == ["key":"normal-string", "replaceKey": "normalstring"]

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,7 @@ class StartJenkinsJobTask implements RetryableTask {
TaskResult execute(Stage stage) {
String master = stage.context.master
String job = stage.context.job

Map context = objectMapper.convertValue(stage.context, Map)

if(stage.execution instanceof Pipeline) {
context.trigger = ((Pipeline) stage.execution).trigger
}

Map<String,String> parameters = stage.context.parameters
Map parsedParameters = ContextParameterProcessor.process(parameters, context)

Map<String, Object> build = igorService.build(master, job, parsedParameters)
new DefaultTaskResult(ExecutionStatus.SUCCEEDED, [buildNumber: build.number] + ( parsedParameters ? [parsedParameters:parsedParameters] : [:] ) )
Map<String, Object> build = igorService.build(master, job, stage.context.parameters)
new DefaultTaskResult(ExecutionStatus.SUCCEEDED, [buildNumber: build.number] )
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import com.netflix.spinnaker.orca.Task
import com.netflix.spinnaker.orca.TaskResult
import com.netflix.spinnaker.orca.kato.api.KatoService
import com.netflix.spinnaker.orca.kato.api.TaskId
import com.netflix.spinnaker.orca.pipeline.model.Pipeline
import com.netflix.spinnaker.orca.pipeline.model.Stage
import com.netflix.spinnaker.orca.pipeline.util.ContextParameterProcessor
import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode
import groovy.util.logging.Slf4j
Expand Down Expand Up @@ -67,16 +65,12 @@ class CreateDeployTask implements Task {
"kato.last.task.id" : taskId,
"kato.task.id" : taskId, // TODO retire this.
"deploy.account.name": deployOperations.credentials
] + deployOperations )
] )
}

private Map deployOperationFromContext(Stage stage) {
def operation = [:]
def augmentedContext = [:] + stage.context
if (stage.execution instanceof Pipeline) {
augmentedContext.put('trigger', ((Pipeline) stage.execution).trigger)
}
def context = ContextParameterProcessor.process(stage.context, augmentedContext)
def context = stage.context

if (context.containsKey("cluster")) {
operation.putAll(context.cluster as Map)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,29 +218,4 @@ class CreateDeployTaskSpec extends Specification {
result.outputs."kato.task.id" == taskId
}

def "replaces stack name with jenkins branch"(){
given:
def operations = []
task.kato = Mock(KatoService) {
1 * requestOperations(*_) >> {
operations.addAll(it.flatten())
Observable.from(taskId)
}
}
stage.context.stack = '${scmInfo.branch.replaceAll("-", "")}'
stage.execution.trigger.putAll(
[buildInfo: [scm: [[branch: 'name-of-my-stack']]]]
)

when:
def result = task.execute(stage.asImmutable())

then:
operations.find {
it.containsKey("basicAmazonDeployDescription")
}.basicAmazonDeployDescription.stack == "nameofmystack"

result.stageOutputs.stack == 'nameofmystack'

}
}

0 comments on commit 244482e

Please sign in to comment.