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

fix(artifacts): consider requiredArtifactIds in expected artifacts when trigger is pipeline type (backport #4489) #4491

Merged
merged 1 commit into from
Jul 21, 2023
Merged
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
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 @@ -575,6 +575,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