Explain how to quote a joined rules:if expression

What does this MR do?

When a rules:if joins expressions with && or || and is put in quotes, the quotes have to wrap the whole expression. The issue shows what happens when they wrap only a later part:

- if: $CI_PIPELINE_SOURCE == "schedule" && '"$[[ inputs.run_interval ]]" == "weekly"'

YAML keeps those single quotes, so the quoted part is a string in the expression rather than a comparison, and a non-empty string is true: the job is added to every scheduled pipeline, whatever the input is.

As suggested in review, this MR adds two sentences and a one-line example at the end of Join variable expressions together, in doc/ci/jobs/job_rules.md, and leaves the CI/CD inputs examples page as it is on master.

How I checked it

A scratch spec, not part of this MR, run in a local GitLab test environment on this branch (one docs commit on b00391b1ec3). It creates pipelines with Ci::CreatePipelineService from this configuration, with <rule> replaced by each form below and the two variables set as listed:

variables:
  VARIABLE1: "<value>"
  VARIABLE2: "<value>"
build-job:
  script: echo "always runs"
check-job:
  script: echo "runs when the rule is true"
  rules:
    - if: <rule>
whole quoted:             '$VARIABLE1 =~ /^content.*/ && $VARIABLE2 == "something"'
later part quoted:        $VARIABLE1 =~ /^content.*/ && '$VARIABLE2 == "something"'
later part quoted (||):   $VARIABLE1 =~ /^content.*/ || '$VARIABLE2 == "something"'
later part, same quotes:  $VARIABLE1 =~ /^content.*/ && "$VARIABLE2 == "something""
first part quoted:        '$VARIABLE1 =~ /^content.*/' && $VARIABLE2 == "something"
first part quoted (||):   '$VARIABLE1 =~ /^content.*/' || $VARIABLE2 == "something"

whole quoted, VARIABLE2=something            jobs=["build-job", "check-job"]
whole quoted, VARIABLE2=other                jobs=["build-job"]
later part quoted, VARIABLE2=other           jobs=["build-job", "check-job"]
later part quoted, VARIABLE1=other-1         jobs=["build-job"]
later part quoted (||), both other           jobs=["build-job", "check-job"]
later part, same quotes                      error=jobs:check-job:rules:rule if invalid expression syntax
first part quoted                            error=(): did not find expected alphabetic or numeric character while scanning an anchor at line 9 column 40
first part quoted (||)                       error=(): did not find expected comment or line break while scanning a block scalar at line 9 column 40

With the whole expression quoted, the job is added only when both comparisons are true. With a later part quoted, that part is the string $VARIABLE2 == "something", which is always true, so only the unquoted part decides, and with || the job is always added. If that part contains the quote character that wraps it, the expression is invalid. With the first part quoted, YAML refuses the line. In the last two cases, pipeline creation fails with the error shown.

The same spec creates pipelines from the CI/CD inputs example as the page has it (the job is added only when the input is test-branch) and with its single quotes removed, which is not valid YAML: see the thread on that line.

Lint
  • scripts/lint-doc.sh on the page, in the lint image the docs-lint markdown job uses (markdownlint-cli2 0.23.2, Vale 3.21.0): passed, with no markdownlint issues.
  • Vale at suggestion level on this branch and on master: the only finding on either is gitlab_base.ReadingLevel for the page, 7.15 on this branch and 7.17 on master.
  • lychee --offline --no-progress --include-fragments on the page, as the docs-lint links job runs it: 0 errors.
  • The commit message passes scripts/lint/commit_linter.rb and the Danger commit linter (gitlab-dangerfiles 4.12.0).

Closes #628757 (closed)

Author's checklist

If you are a GitLab team member and only adding documentation, do not add any of the following labels:

  • ~"frontend"
  • ~"backend"
  • ~"type::bug"
  • ~"database"

These labels cause the MR to be added to code verification QA issues.

Reviewer's checklist

Documentation-related MRs should be reviewed by a Technical Writer for a non-blocking review, based on Documentation Guidelines and the Style Guide.

If you aren't sure which tech writer to ask, use roulette or ask in the #docs Slack channel.

  • If the content requires it, ensure the information is reviewed by a subject matter expert.
  • Technical writer review items:
    • Ensure docs metadata is present and up-to-date.
    • Ensure the appropriate labels are added to this MR.
    • Ensure a release milestone is set.
    • If relevant to this MR, ensure content topic type principles are in use, including:
      • The headings should be something you'd do a Google search for. Instead of Default behavior, say something like Default behavior when you close an issue.
      • The headings (other than the page title) should be active. Instead of Configuring GDK, say something like Configure GDK.
      • Any task steps should be written as a numbered list.
      • If the content still needs to be edited for topic types, you can create a follow-up issue with the docs-technical-debt label.
  • Review by assigned maintainer, who can always request/require the reviews above. Maintainer's review can occur before or after a technical writer review.
Edited by José M. Requena Plens

Merge request reports

Loading
Loading