'This GitLab CI configuration is invalid: root config contains unknown keys: tags

I am trying to run a simple python project on Gitlab. I setup a runner on an EC2 instance and I am trying to use it for the project. This is the .gitlab-ci.yml file:

tags:
    - gr
    - grun
    - runner
stages:
    - build
build:
    image: python:3.7
    script:
        - echo "building"
        - pip install -r requirements.txt
        - python test.py

I get this error-

 This GitLab CI configuration is invalid: root config contains unknown keys: tags

I get the same error when I use jobs and environment in the .gitlab-ci.yml file. The runner is active. How to fix this issue?



Solution 1:[1]

If you see a Ci Gitlab documentation, Tags is not a root config.

https://docs.gitlab.com/ee/ci/yaml/#tags

Solution 2:[2]

You can't use tags as root element. Only these are allowed root keywords:

Keyword Description
default Custom default values for job keywords.
include Import configuration from other YAML files.
stages The names and order of the pipeline stages.
variables Define CI/CD variables for all job in the pipeline.
workflow Control what types of pipeline run.

If you want to define default tags, you must include a default section:

stages:
  - build
default:
  tags:
    - gr
    - grun
    - runner
build:
  stage: build
  image: python:3.7
  script:
    - echo "building"
    - pip install -r requirements.txt
    - python test.py

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gary Houbre
Solution 2 OscarGarcia