DevOps / GitLab CI Basics Interview Questions
What are GitLab environments and how do you define them in CI/CD?
Environments in GitLab CI/CD represent deployment targets - staging, production, review apps, etc. Defining an environment in a job links that deployment to the environment, enabling GitLab to track which version is deployed where and provide rollback capabilities.
# Basic environment definition deploy-staging: stage: deploy script: - ./deploy.sh staging environment: name: staging url: https://staging.example.com rules: - if: $CI_COMMIT_BRANCH == "main" deploy-production: stage: deploy script: - ./deploy.sh production environment: name: production url: https://example.com rules: - if: $CI_COMMIT_TAG when: manual # requires manual approval # Dynamic review environments per merge request review-app: stage: deploy script: - deploy-to-review.sh $CI_COMMIT_REF_SLUG environment: name: review/$CI_COMMIT_REF_SLUG url: https://$CI_COMMIT_REF_SLUG.review.example.com on_stop: stop-review rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event" stop-review: stage: deploy script: - teardown-review.sh $CI_COMMIT_REF_SLUG environment: name: review/$CI_COMMIT_REF_SLUG action: stop rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event" when: manual
Environments are visible under Deploy > Environments in the GitLab UI, showing the current deployed version, who deployed it, and when. The on_stop option links a cleanup job that tears down ephemeral environments (like review apps) when no longer needed.
More Related questions...