DevOps / GitLab CI Basics Interview Questions
What is the 'extends' keyword and how does it enable job templates?
The extends keyword lets one job inherit configuration from another job (or a hidden job used as a template). This reduces duplication and keeps the pipeline DRY (Don't Repeat Yourself).
# Define a hidden job template (starts with .) .base-deploy: image: alpine:latest before_script: - apk add --no-cache curl - echo "Logging into registry..." after_script: - echo "Deployment complete" rules: - if: $CI_COMMIT_BRANCH == "main" # Jobs extend the template deploy-staging: extends: .base-deploy script: - ./deploy.sh staging environment: name: staging deploy-production: extends: .base-deploy script: - ./deploy.sh production environment: name: production rules: # overrides the template rules - if: $CI_COMMIT_TAG when: manual # extends with multiple parents (array form) .logging: after_script: - send-logs.sh my-job: extends: - .base-deploy - .logging # merges config from both (later parent wins on conflict)
Merge behaviour: when a job extends a template, YAML hashes are merged deeply. Arrays (like script, rules) in the child job replace (not append to) the parent's arrays. A job can extend multiple templates by providing an array - conflicts are resolved with the last-listed parent winning, then the child overriding all.
More Related questions...