Skip to content

Commit

Permalink
fix(artifacts): consider requiredArtifactIds in expected artifacts wh…
Browse files Browse the repository at this point in the history
…en trigger is pipeline type (#4489)
  • Loading branch information
nemesisOsorio authored Jul 21, 2023
1 parent 4d4e3f3 commit 45de5bc
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ class DependentPipelineStarter implements ApplicationContextAware {
it.expectedArtifactIds ?: []
}

// we are following a similar approach as triggers above
// expectedArtifacts can be used in triggers and stages
// for now we identified DeployManifestStage
// in ResolveDeploySourceManifestTask using ManifestEvaluator.getRequiredArtifacts
def requiredArtifactIds = pipelineConfig.get("stages", []).collectMany {
it.requiredArtifactIds ?: []
}
expectedArtifactIds.addAll(requiredArtifactIds)

pipelineConfig.trigger = [
type : "pipeline",
user : authenticationDetails?.user ?: user ?: "[anonymous]",
Expand All @@ -93,7 +102,7 @@ class DependentPipelineStarter implements ApplicationContextAware {
parameters : [:],
strategy : suppliedParameters.strategy == true,
correlationId : "${parentPipeline.id}_${parentPipelineStageId}_${pipelineConfig.id}_${parentPipeline.startTime}".toString(),
expectedArtifactIds : expectedArtifactIds
expectedArtifactIds : expectedArtifactIds.toSet().toList()
]
/* correlationId is added so that two pipelines aren't triggered when a pipeline is canceled.
* parentPipelineStageId is added so that a child pipeline (via pipeline stage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,81 @@ class DependentPipelineStarterSpec extends Specification {
result.trigger.artifacts.findAll { it.name == "gcr.io/project/image" }.version.containsAll(["42", "1337"])
}

def "should find expected artifacts when pipeline has requiredArtifactIds and triggered by pipeline stage"() {
given:
def requiredArtifactId = "docker-artifact-id"
def expectedImage = Artifact.builder().type("docker/image").name("docker.io/org/image").build()
ArrayList<ExpectedArtifact> expectedArtifacts = [
ExpectedArtifact.builder().id(requiredArtifactId).matchArtifact(expectedImage).build()
]

def triggeredPipelineConfig = [
name : "triggered-by-stage",
id : "triggered-id",
stages : [
[
name : "Deploy (Manifest)",
type : "deployManifest",
requiredArtifactIds: [requiredArtifactId]
]
],
expectedArtifacts: expectedArtifacts,
triggers : [],
]

Artifact testArtifact = Artifact.builder().type("docker/image").name("docker.io/org/image").version("alpine").build()

def parentPipeline = pipeline {
name = "parent-pipeline"
authentication = new PipelineExecution.AuthenticationDetails("username", "account1")
pipelineConfigId = "f837d603-bcc8-41c4-8ebc-bf0b23f59108"
stage {
id = "stage1"
refId = "1"
outputs = [artifacts: [testArtifact]]
}
}

def executionLauncher = Mock(ExecutionLauncher)
def applicationContext = new StaticApplicationContext()
applicationContext.beanFactory.registerSingleton("pipelineLauncher", executionLauncher)
dependentPipelineStarter = new DependentPipelineStarter(
applicationContext,
mapper,
new ContextParameterProcessor(),
Optional.empty(),
Optional.of(artifactUtils),
new NoopRegistry()
)

and:
executionLauncher.start(*_) >> { _, p ->
return pipeline {
name = p.name
id = p.name
trigger = mapper.convertValue(p.trigger, Trigger)
}
}
artifactUtils.getArtifactsForPipelineId(*_) >> {
return new ArrayList<Artifact>();
}

when:
def result = dependentPipelineStarter.trigger(
triggeredPipelineConfig,
null,
parentPipeline,
[:],
"stage1",
buildAuthenticatedUser("username", [])
)

then:
result.trigger.artifacts.size() == 1
result.trigger.artifacts*.name.contains(testArtifact.name)
result.trigger.artifacts.findAll { it.name == "docker.io/org/image" }.version.containsAll(["alpine"])
}

def "should resolve expressions in trigger"() {
given:
def triggeredPipelineConfig = [name: "triggered", id: "triggered", parameterConfig: [[name: 'a', default: '${2 == 2}']]]
Expand Down

0 comments on commit 45de5bc

Please sign in to comment.