Skip to content

Conversation

@majiayu000
Copy link

Summary

This PR fixes #5976

Changes

  • Added MatrixIndex and MatrixCount fields to RunContext struct to track the current matrix job index (0-based) and total number of matrix jobs
  • Updated newRunContext function signature to accept and initialize these new fields
  • Added job-index and job-total attributes to the strategy context in both NewExpressionEvaluatorWithEnv and newStepExpressionEvaluator functions
  • These values are now accessible via strategy.job-index and strategy.job-total expressions in workflows, matching GitHub Actions behavior

The implementation follows GitHub Actions specification where:

  • job-index: 0-based index of the current job in the matrix (e.g., 0, 1, 2, 3 for 4 jobs)
  • job-total: Total number of jobs in the matrix

Generated with Claude Code

This commit adds missing job-index and job-total attributes to the
strategy context to match GitHub Actions behavior.

- Added MatrixIndex and MatrixCount fields to RunContext
- Updated newRunContext to accept and store job index and total count
- Added job-index and job-total to strategy context in expression evaluators

Fixes nektos#5976

Signed-off-by: majiayu000 <1835304752@qq.com>
@ChristopherHX
Copy link
Contributor

Thank you for taking a look,

You missed updating composite action runcontext creation

func newCompositeRunContext(ctx context.Context, parent *RunContext, step actionStep, actionPath string) *RunContext {
env := evaluateCompositeInputAndEnv(ctx, parent, step)
// run with the global config but without secrets
configCopy := *(parent.Config)
configCopy.Secrets = nil
// create a run context for the composite action to run in
compositerc := &RunContext{
Name: parent.Name,
JobName: parent.JobName,
Run: &model.Run{
JobID: parent.Run.JobID,
Workflow: &model.Workflow{
Name: parent.Run.Workflow.Name,
Jobs: map[string]*model.Job{
parent.Run.JobID: {},
},
},
},
Config: &configCopy,
StepResults: map[string]*model.StepResult{},
JobContainer: parent.JobContainer,
ActionPath: actionPath,
Env: env,
GlobalEnv: parent.GlobalEnv,
Masks: parent.Masks,
ExtraPath: parent.ExtraPath,
Parent: parent,
EventJSON: parent.EventJSON,
nodeToolFullPath: parent.nodeToolFullPath,
}
compositerc.ExprEval = compositerc.NewExpressionEvaluator(ctx)
return compositerc
}

We also need tests for both workflow and composite action, so we can ensure this will continue to work in the future.

Tests can be workflows in pkg/runner/testdata//push.yml

use "act -W /push.yml" in pkg/runner/testdata/ to run them manually. I can help adding entries to runner_test.go


I would suggest to try writing test workflows yourself, without the help from AI tooling to learn more about act itself.

This commit adds comprehensive tests for the job-index and job-total
strategy context attributes and fixes a boundary condition:

- Added MatrixIndex and MatrixCount to the test RunContext fixture
- Created createRunContextWithoutMatrix helper for testing non-matrix jobs
- Added TestStrategyContext to verify job-index and job-total values
- Added TestStrategyContextInStep to verify values in step evaluator
- Fixed boundary condition: job-index and job-total are now only set
  when there's an actual matrix (MatrixCount > 0)

The fix ensures that when a job has a Strategy but no actual matrix
combinations (MatrixCount == 0), the job-index and job-total properties
are not set in the strategy context, matching GitHub Actions behavior.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@pull-request-size pull-request-size bot added size/L and removed size/S labels Dec 31, 2025
majiayu000 and others added 2 commits December 31, 2025 15:29
This ensures strategy.job-index and strategy.job-total are accessible
within composite actions.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@majiayu000 majiayu000 closed this Jan 5, 2026
@pull-request-size pull-request-size bot added size/XS and removed size/L labels Jan 5, 2026
@majiayu000 majiayu000 reopened this Jan 5, 2026
@pull-request-size pull-request-size bot added size/L and removed size/XS labels Jan 5, 2026
@majiayu000
Copy link
Author

@ChristopherHX Thanks for your review. Added the requested integration tests:

  • workflow: pkg/runner/testdata/strategy-job-index/push.yml
  • composite action: pkg/runner/testdata/strategy-job-index-composite/{push.yml,composite_action/action.yml}
  • wired into pkg/runner/runner_test.go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing job-index and job-total attributes in strategy context

2 participants