Cannot get !reference to work in .gitlab-ci.yaml
Asked Answered
H

2

9

I am working with GitLab 14.6.2-ee.

I am trying to use the YAML reference feature to re-use code snippets in my GitLab configuration files. I've boiled it down to what I think is the simplest use case, but cannot get the file to validate.

.gitlab-ci.yaml:

...
.hidden:
  .update-static-site:
    rules:
        if: $CI_COMMIT_BRANCH =~ /(^develop$|^master$|^staging-.*)/

...
trigger-downstream-pipeline:
  stage: .post
  trigger:
    project: project-x/project-x-manifest
    branch: master
  variables:
    SOURCE_COMMIT_SHA: $CI_COMMIT_SHA
    SOURCE_COMMIT_REF_NAME: $CI_COMMIT_REF_NAME
    APP_NAME: project-x
  needs:
    - job: push-project-x-to-harbor
      artifacts: false
  rules:
    - !reference [.hidden, .update-static-site, rules]

I cannot get the !reference line to validate. I keep getting Unresolved tag: !reference.

I found a SO posting (GitLab CI - Reuse rules with !reference tag) that addresses exactly what I'm trying to do, but it does not work.

Hypso answered 9/9, 2022 at 18:24 Comment(4)
Can you try changing it to rules: !reference [.hidden, .update-static-site, rules] and see if that works? i.e. not a list, but a single value for rules:Surfacetoair
That didn't work either. Tried it on the same line as the rules: statement as well as indented on the next line.Hypso
Bummer, I'm afraid I don't have any other suggestions. Hopefully, someone else will be able to help you out.Surfacetoair
Hmmm. I'm not able to reproduce this problem on the latest version of GitLab. Are you getting this as a YAML validation error? When I copy/paste your provided YAML into the GitLab CI lint validator on gitlab.com it works without any errors (assuming the job push-project-x-to-harbor exists). Are you sure you've provided a reproducible example? Maybe there's a problem lost in translation here that's not in the question. I don't think it's a GitLab version issue since this feature doesn't have any change notes for newer versions, but that may be a possibility.Araucaria
H
7

So, the main problem was that I needed to change my VS Code config to accept the YAML reference tags. Once that was done, no more validation errors.

When I committed the changes and watched the pipeline, there were no issues.

Hypso answered 12/9, 2022 at 19:8 Comment(1)
Would be good to share how did you change your VSCode config. For anyone looking for it I had to put: "yaml.customTags": [ "!reference sequence" ] in .vscode/settings.jsonStarrstarred
H
0

configure your IDE to support reference tags

The following formatting should works as well (at least it does for me when referencing scripts):

script:
  - !reference
    - .hiddenjob
    - script
Heartstricken answered 21/6 at 8:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.