What if your code could test and deploy itself every time you save a change?
Why GitLab CI basics? - Purpose & Use Cases
Imagine you have a small team working on a project. Every time someone makes a change, you have to manually check the code, run tests on your computer, and then deploy the update. This means waiting, switching between tools, and hoping nothing breaks.
Doing all these steps by hand is slow and tiring. You might forget a test, miss a step, or deploy broken code. It's like baking a cake without a recipe and hoping it turns out right every time.
GitLab CI automates these steps. It watches your code, runs tests automatically, and can even deploy your project without you lifting a finger. This means faster, safer updates and more time to focus on building cool features.
git pull run tests manually deploy manually
# .gitlab-ci.yml
stages:
- test
- deploy
test_job:
stage: test
script:
- run tests
deploy_job:
stage: deploy
script:
- deploy appIt enables continuous, automatic checks and deployments so your team can deliver updates confidently and quickly.
A developer pushes code to GitLab. Instantly, GitLab CI runs tests and if all pass, it deploys the new version to the website without anyone needing to do anything else.
Manual testing and deployment is slow and error-prone.
GitLab CI automates these steps with simple configuration.
This leads to faster, safer, and more reliable software delivery.
Practice
.gitlab-ci.yml file in GitLab CI?Solution
Step 1: Understand the role of
This file is used to tell GitLab CI what tasks to run automatically, such as building or testing code..gitlab-ci.ymlStep 2: Identify what the file does not do
It does not store code, manage permissions, or create branches; those are handled elsewhere.Final Answer:
To define the stages and jobs for automated tasks like build and test -> Option DQuick Check:
.gitlab-ci.ymldefines jobs and stages [OK]
.gitlab-ci.yml controls automation steps [OK]- Confusing
.gitlab-ci.ymlwith source code files - Thinking it manages user permissions
- Assuming it creates branches automatically
test in .gitlab-ci.yml?Solution
Step 1: Recall GitLab CI job syntax
A job is defined by its name followed by a script section listing commands to run.Step 2: Compare options to correct YAML format
test: script: - echo "Running tests" uses correct YAML with job name and script list. Others use incorrect keywords or syntax.Final Answer:
test: script: - echo "Running tests" -> Option BQuick Check:
Job name + script list = correct job definition [OK]
- Using curly braces or incorrect keywords like 'job' or 'task'
- Not using a list for script commands
- Incorrect indentation or missing colons
.gitlab-ci.yml snippet:stages:
- build
- test
build_job:
stage: build
script:
- echo "Building"
test_job:
stage: test
script:
- echo "Testing"What will be the order of job execution?
Solution
Step 1: Understand stages order
Stages run in the order listed: first 'build', then 'test'.Step 2: Match jobs to stages
'build_job' is in 'build' stage, so it runs before 'test_job' in 'test' stage.Final Answer:
build_job runs first, then test_job -> Option CQuick Check:
Stages run in order listed [OK]
- Assuming jobs run in alphabetical order
- Thinking jobs run in parallel by default
- Ignoring the stages list order
.gitlab-ci.yml but it never runs:deploy_job:
script:
- echo "Deploying"
only:
- mainWhat is the likely reason?
Solution
Step 1: Check the stage assignment
The job does not specify a 'stage' key, so it defaults to the 'test' stage.Step 2: Impact of missing stage in global stages
If the root-level 'stages' does not include 'test', GitLab fails the job because the stage is undefined in the global stages list.Final Answer:
The job is missing a stage definition -> Option AQuick Check:
Default stage 'test' must be in 'stages' list [OK]
- Assuming branch name is wrong without checking
- Forgetting to define a stage (defaults to 'test' if missing)
- Thinking script command syntax is wrong
develop branch and after the build stage completes successfully. Which .gitlab-ci.yml snippet correctly achieves this?Solution
Step 1: Use 'rules' to run job only on 'develop' branch
The 'rules' keyword with condition '$CI_COMMIT_BRANCH == "develop"' ensures the job runs only on that branch.Step 2: Use 'needs' to wait for 'build_job'
The 'needs' keyword makes this job wait for 'build_job' to finish successfully before running.Final Answer:
deploy_job: stage: deploy script: - echo "Deploying" rules: - if: '$CI_COMMIT_BRANCH == "develop"' when: on_success needs: - build_job -> Option AQuick Check:
'rules' + 'needs' control branch and stage order [OK]
- Using deprecated 'only' or 'except' keywords
- Confusing 'after_script' with job dependencies
- Incorrect 'needs' or 'dependencies' usage
Ready to prove it?
Pass the test and this pattern moves unknown
