Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add comments to .gitlab-ci.yaml file?

Tags:

gitlab-ci

I tried commenting out some lines from a .gitlab-ci.yml file and the pipeline is failing saying the YAML is not properly formatted.

Below is the picture of what I see in the GitLab CI-Pipeline UI.

enter image description here

Below is the .gitlab-ci.yml file that is error.

stages:
  - build
  - test
#  - release

build-demo-commands:
    extends: .npm-job
    stage: build
    script:
        - npm run build-demo-prod

include:
  - project: 'myproj/gitlab-ci'
    ref: '1.2.0'
    file: 'templates/npm.gitlab-ci.yml'
#  - project: 'myproj/gitlab-ci'
#    ref: '1.2.0'
#    file: 'templates/Kaniko-npm.gitlab-ci.yml'

I suspect that there is a pre-processor for the CI-pipeline which supports a comment syntax.

What is the correct way to comment out lines in a .gitlab-ci.yml file?

Searching for answer

  • I found this Add comments to .gitlab-ci.yml, which was merged two years ago. Now I just need to find the page that says how to do it!

  • https://docs.gitlab.com/ee/development/code_comments.html - not helpful

  • https://docs.gitlab.com/ee/development/cicd/templates.html#template-types -

  • https://docs.gitlab.com/ee/development/cicd/templates.html#explain-the-template-with-comments - Looks like the answer so why does it complain?

  • https://blog.wplauncher.com/add-comments-to-yaml-file/#:~:text=In%20order%20to%20add%20comments,line%20in%20a%20YAML%20file. This says the # is the way to do comments.

EDIT/UPDATE:

I believe the problem is with the included scripts and not with adding the comments. When I removed the comments I still got an error. The error was:

Found errors in your .gitlab-ci.yml:
npm-deploy-snapshot job: chosen stage does not exist; available stages are .pre
build
test
.post
You can also test your .gitlab-ci.yml in CI Lint

So I now believe that using the hash (#) character is the correct way to comment a .gitlab-ci.yml file.

like image 497
PatS Avatar asked Apr 30 '21 01:04

PatS


People also ask

How do I add a comment in YAML?

In order to add comments to a YAML file, you simply have to use the # (hashtag symbol) at the start of the line. For example, below is an example of one commented line in a YAML file.

How do you comment a block in YAML?

YAML does not support block or multi-line comments. In order to create a multi-line comment, we need to suffix each line with a # . # block comments.


Video Answer


1 Answers

Comments in YAML use a # character which comments out the remainder of the line

Wikipedia says:

Comments begin with the number sign (#), can start anywhere on a line and continue until the end of the line. Comments must be separated from other tokens by whitespace characters.[16] If # characters appear inside of a string, then they are number sign (#) literals.

like image 77
PatS Avatar answered Oct 19 '22 02:10

PatS