Sobes.tech
Junior — Middle

Explain the purpose and usage of the 'only' and 'except' keywords in GitLab CI/CD configuration.

sobes.tech AI

Answer from AI

In GitLab CI/CD, the keywords only and except are used to control under what conditions certain jobs (tasks) are run.

  • only specifies the conditions under which a job should run. For example, you can specify branches, tags, file changes, or events (push, merge_request, etc.). The job will only execute if the condition in only is met.

  • except is the opposite, setting conditions under which the job should not run.

Example:

job:
  script: echo "Hello"
  only:
    - master
    - tags
  except:
    - develop

In this example, the job will only run when pushing to the master branch or creating a tag, but will not run when pushing to the develop branch.

Using these keywords helps optimize the pipeline, running tasks only when necessary, such as tests only for certain branches or deployment only during releases.