Skip to content

Commit 5f16465

Browse files
Copilotpelikhan
andcommitted
fix: use agent-downstream artifact prefix in buildGitHubScriptStep and buildCustomActionStep
buildSafeOutputJob-based jobs (e.g. upload_assets) only depend on the agent job, not the activation job. Using needs.activation.outputs.artifact_prefix in those jobs would break workflow_call workflows since activation is not in their needs. Use artifactPrefixExprForAgentDownstreamJob (needs.agent.outputs.artifact_prefix) in buildGitHubScriptStep and buildCustomActionStep instead. Also adds two tests verifying the correct prefix expression is used per context. Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
1 parent 7a12b55 commit 5f16465

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

‎pkg/workflow/safe_output_helpers_test.go‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,3 +682,62 @@ func TestBuildGitHubScriptStepNoWorkingDirectory(t *testing.T) {
682682
t.Error("Expected step to contain script")
683683
}
684684
}
685+
686+
// TestBuildGitHubScriptStepWorkflowCallArtifactPrefix verifies that agent artifact downloads
687+
// in buildGitHubScriptStep use needs.agent.outputs.artifact_prefix in workflow_call context.
688+
// These steps are used in jobs that depend on the agent job (not activation), so the
689+
// agent-downstream prefix expression must be used.
690+
func TestBuildGitHubScriptStepWorkflowCallArtifactPrefix(t *testing.T) {
691+
compiler := &Compiler{}
692+
workflowData := &WorkflowData{
693+
Name: "Test Workflow",
694+
On: "workflow_call",
695+
}
696+
697+
config := GitHubScriptStepConfig{
698+
StepName: "Test Step",
699+
StepID: "test_step",
700+
MainJobName: "agent",
701+
Script: "console.log('test');",
702+
}
703+
704+
steps := compiler.buildGitHubScriptStep(workflowData, config)
705+
stepsStr := strings.Join(steps, "")
706+
707+
// In workflow_call context, the download must reference needs.agent (not needs.activation)
708+
// because buildSafeOutputJob-based jobs depend on agent, not activation.
709+
agentPrefix := "${{ needs.agent.outputs.artifact_prefix }}agent"
710+
if !strings.Contains(stepsStr, agentPrefix) {
711+
t.Errorf("Expected buildGitHubScriptStep to use %q in workflow_call context, but it was not found.\nGenerated steps:\n%s", agentPrefix, stepsStr)
712+
}
713+
714+
// Ensure activation prefix is NOT used
715+
if strings.Contains(stepsStr, "needs.activation.outputs.artifact_prefix") {
716+
t.Errorf("Expected buildGitHubScriptStep NOT to use needs.activation.outputs.artifact_prefix (job does not depend on activation).\nGenerated steps:\n%s", stepsStr)
717+
}
718+
}
719+
720+
// TestBuildGitHubScriptStepNonWorkflowCallNoArtifactPrefix verifies no prefix is added
721+
// in non-workflow_call context.
722+
func TestBuildGitHubScriptStepNonWorkflowCallNoArtifactPrefix(t *testing.T) {
723+
compiler := &Compiler{}
724+
workflowData := &WorkflowData{
725+
Name: "Test Workflow",
726+
On: "issues",
727+
}
728+
729+
config := GitHubScriptStepConfig{
730+
StepName: "Test Step",
731+
StepID: "test_step",
732+
MainJobName: "agent",
733+
Script: "console.log('test');",
734+
}
735+
736+
steps := compiler.buildGitHubScriptStep(workflowData, config)
737+
stepsStr := strings.Join(steps, "")
738+
739+
// In non-workflow_call context, no artifact prefix should be used
740+
if strings.Contains(stepsStr, "artifact_prefix") {
741+
t.Errorf("Expected buildGitHubScriptStep NOT to use artifact_prefix in non-workflow_call context.\nGenerated steps:\n%s", stepsStr)
742+
}
743+
}

‎pkg/workflow/safe_outputs_jobs.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ func (c *Compiler) buildCustomActionStep(data *WorkflowData, config GitHubScript
190190

191191
// Add artifact download steps before the custom action step.
192192
// In workflow_call context, use the per-invocation prefix to avoid artifact name clashes.
193-
steps = append(steps, buildAgentOutputDownloadSteps(artifactPrefixExprForDownstreamJob(data))...)
193+
// These steps are used in jobs that depend on the agent job (not activation), so use
194+
// the agent-downstream prefix expression.
195+
steps = append(steps, buildAgentOutputDownloadSteps(artifactPrefixExprForAgentDownstreamJob(data))...)
194196

195197
// Step name and metadata
196198
steps = append(steps, fmt.Sprintf(" - name: %s\n", config.StepName))
@@ -290,7 +292,9 @@ func (c *Compiler) buildGitHubScriptStep(data *WorkflowData, config GitHubScript
290292

291293
// Add artifact download steps before the GitHub Script step.
292294
// In workflow_call context, use the per-invocation prefix to avoid artifact name clashes.
293-
steps = append(steps, buildAgentOutputDownloadSteps(artifactPrefixExprForDownstreamJob(data))...)
295+
// These steps are used in jobs that depend on the agent job (not activation), so use
296+
// the agent-downstream prefix expression.
297+
steps = append(steps, buildAgentOutputDownloadSteps(artifactPrefixExprForAgentDownstreamJob(data))...)
294298

295299
// Step name and metadata
296300
steps = append(steps, fmt.Sprintf(" - name: %s\n", config.StepName))

0 commit comments

Comments
 (0)