de6a526560
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>
52 lines
1.2 KiB
YAML
52 lines
1.2 KiB
YAML
---
|
|
# workflow_vars.yml
|
|
# Exercises workflow:rules:variables: injection.
|
|
# The matching workflow rule sets DEPLOY_TARGET; job rules use it.
|
|
#
|
|
# Expected behaviour per context:
|
|
# (no context) → all jobs active (no context evaluation)
|
|
# --branch main → deploy-prod active, deploy-staging skipped
|
|
# --branch develop → deploy-prod skipped, deploy-staging active
|
|
# --branch feat/x → deploy-prod skipped, deploy-staging skipped (manual)
|
|
|
|
stages:
|
|
- build
|
|
- deploy
|
|
|
|
variables:
|
|
DEPLOY_TARGET:
|
|
value: ""
|
|
description: "Deployment target — set by workflow rules"
|
|
|
|
workflow:
|
|
rules:
|
|
- if: '$CI_COMMIT_BRANCH == "main"'
|
|
variables:
|
|
DEPLOY_TARGET: production
|
|
- if: '$CI_COMMIT_BRANCH == "develop"'
|
|
variables:
|
|
DEPLOY_TARGET: staging
|
|
- when: always
|
|
|
|
build:
|
|
stage: build
|
|
script: make build
|
|
rules:
|
|
- when: always
|
|
|
|
deploy-prod:
|
|
stage: deploy
|
|
script: make deploy ENV=production
|
|
rules:
|
|
- if: '$DEPLOY_TARGET == "production"'
|
|
when: on_success
|
|
- when: never
|
|
|
|
deploy-staging:
|
|
stage: deploy
|
|
script: make deploy ENV=staging
|
|
rules:
|
|
- if: '$DEPLOY_TARGET == "staging"'
|
|
when: on_success
|
|
- when: never
|