yaml – How reference variables in job rules in gitlab ci?

yaml – How reference variables in job rules in gitlab ci?

Using !reference-keyword in referenced section with !reference is not possible at this moment.

!reference documentation:

You can’t reuse a section that already includes a !reference tag. Only
one level of nesting is supported.

For your needs you could use YAML-anchors. (not tested)

include:
  - template: Workflows/Branch-Pipelines.gitlab-ci.yml

.staging_variables: &staging_variables
  variables:
    CONFIG_NAME: staging

.staging_rules: &staging_rules
  rules:
    - if: $CI_COMMIT_BRANCH == $STAGING_BRANCH
      variables: *staging_variables

stages:
  - staging

staging:
  stage: staging
  rules:
    - *staging_rules
  script:
    - echo $CONFIG_NAME
  tags:
    - staging

As stated in the comment above you produce a syntax error for the GitLab CI linting tool which tries to resolve the variables array from the referenced section.

Change your config to have the !reference tag like below:

staging:
  stage: staging
  rules: !reference [.staging_rules, rules]
  script:
    - echo $CONFIG_NAME
  tags:
    - staging

Note that here the - !reference has changed to rules: !reference […]

This should fix your error

yaml – How reference variables in job rules in gitlab ci?

Leave a Reply

Your email address will not be published.