feat(linter): propagate workflow:rules:variables: into job rule evaluation
workflow:rules: can define variables: on matching rules (GitLab CI 15.0+).
These variables are now injected into the evaluation context before job
rules:if: expressions are evaluated, making patterns like:
workflow:
rules:
- if: '$CI_COMMIT_BRANCH == "main"'
variables:
DEPLOY_TARGET: production
deploy:
rules:
- if: '$DEPLOY_TARGET == "production"'
work correctly with glint check --branch main.
Changes:
- model.Rule: add Variables map[string]any field (yaml:"variables")
- cicontext.Context: add pinned map tracking which vars must not be
overwritten; New() pins all shortcut and --var variables; add
Inject(key, value) which writes only when key is not pinned
- cicontext.ExtractStringVars: shared helper that converts map[string]any
variable blocks (plain string or {value:...} form) to map[string]string
- cicontext.EvalWorkflow: returns (bool, map[string]string) — the vars of
the matching workflow rule alongside the runs/no-runs result
- cmd/glint/main.go: enrichContext() injects pipeline-level variable
defaults then workflow-rule variables before printContext; applied in
both cmdCheck and cmdGraph
Injection priority (highest wins):
--var CLI overrides > --branch/--tag/--source shortcuts
> workflow-rule variables > pipeline variables: defaults
Adds 15 unit tests (TestEvalWorkflow, TestContextInject,
TestExtractStringVars, TestWorkflowVarsJobEval) and a testdata fixture
(workflow_vars.yml) validated across four branch contexts.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -147,6 +147,7 @@ Examples:
|
||||
|
||||
ctx := cicontext.New(*branch, *tag, *source, vars)
|
||||
if !ctx.IsEmpty() {
|
||||
enrichContext(ctx, p)
|
||||
printContext(p, ctx)
|
||||
}
|
||||
|
||||
@@ -272,6 +273,9 @@ Examples:
|
||||
resolver.Resolve(p) //nolint:errcheck
|
||||
|
||||
ctx := cicontext.New(*branch, *tag, *source, vars)
|
||||
if !ctx.IsEmpty() {
|
||||
enrichContext(ctx, p)
|
||||
}
|
||||
|
||||
switch mode {
|
||||
case "default":
|
||||
@@ -300,6 +304,24 @@ Examples:
|
||||
}
|
||||
}
|
||||
|
||||
// enrichContext injects pipeline-level variable defaults and then
|
||||
// workflow-rule-generated variables into ctx before job evaluation.
|
||||
// Injection respects pinned variables (--branch/--tag/--source/--var always win).
|
||||
func enrichContext(ctx *cicontext.Context, p *model.Pipeline) {
|
||||
// Pipeline variables: injected as defaults (lowest priority).
|
||||
for k, v := range cicontext.ExtractStringVars(p.Variables) {
|
||||
ctx.Inject(k, v)
|
||||
}
|
||||
// Workflow rules: evaluate to find which rule matches, then inject its variables.
|
||||
runs, ruleVars := cicontext.EvalWorkflow(p, ctx)
|
||||
if !runs {
|
||||
fmt.Fprintln(os.Stderr, "[WARNING] workflow:rules: pipeline would not start for this context")
|
||||
}
|
||||
for k, v := range ruleVars {
|
||||
ctx.Inject(k, v)
|
||||
}
|
||||
}
|
||||
|
||||
func printContext(p *model.Pipeline, ctx *cicontext.Context) {
|
||||
fmt.Printf("Context: %s\n\n", ctx.Summary())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user