📖 Scenario: You are working on a small project and want to automate testing using GitLab CI. You will create a simple GitLab CI configuration file to run a test job automatically when you push code.
🎯 Goal: Build a basic .gitlab-ci.yml file that defines a job named test which runs a simple shell command echo "Running tests...".
📋 What You'll Learn
Create a .gitlab-ci.yml file with a job named test
The test job must run the command echo "Running tests..."
Use the script keyword to define the command
Ensure the job runs in the test stage
💡 Why This Matters
🌍 Real World
GitLab CI is used to automate testing and deployment in software projects, saving time and reducing errors.
💼 Career
Understanding GitLab CI basics is essential for DevOps roles and developers working with continuous integration pipelines.
Progress0 / 4 steps
1
Create the basic GitLab CI file
Create a file named .gitlab-ci.yml and add the line stages: with a list containing test to define the stages.
Git
Hint
Use YAML syntax to define stages: followed by a list with - test.
2
Add the test job
Add a job named test that runs in the test stage. Use the stage keyword with value test.
Git
Hint
Define a job by writing its name followed by stage: test.
3
Add the script to run
Inside the test job, add a script section that runs the command echo "Running tests...".
Git
Hint
Use script: followed by a list with the echo command.
4
Check the job output
Run the GitLab CI pipeline and verify that the output contains the text Running tests.... Write a command to simulate this by printing Running tests....
Git
Hint
Use echo "Running tests..." to show the output.
Practice
(1/5)
1. What is the main purpose of the .gitlab-ci.yml file in GitLab CI?
easy
A. To create branches automatically
B. To store the source code of the project
C. To configure user permissions in GitLab
D. To define the stages and jobs for automated tasks like build and test
Solution
Step 1: Understand the role of .gitlab-ci.yml
This file is used to tell GitLab CI what tasks to run automatically, such as building or testing code.
Step 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 D
2. Which of the following is the correct way to define a job named test in .gitlab-ci.yml?
easy
A. job test {
run: echo "Running tests"
}
B. test:
script:
- echo "Running tests"
C. job:
name: test
command: echo "Running tests"
D. task test:
commands:
- echo "Running tests"
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 B
Quick Check:
Job name + script list = correct job definition [OK]
Hint: Job name + script list is the YAML way to define jobs [OK]
Common Mistakes:
Using curly braces or incorrect keywords like 'job' or 'task'
Not using a list for script commands
Incorrect indentation or missing colons
3. Given this .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?
medium
A. Both jobs run at the same time
B. test_job runs first, then build_job
C. build_job runs first, then test_job
D. Only build_job runs
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 C
Quick Check:
Stages run in order listed [OK]
Hint: Jobs run by stage order top to bottom [OK]
Common Mistakes:
Assuming jobs run in alphabetical order
Thinking jobs run in parallel by default
Ignoring the stages list order
4. You wrote this job in .gitlab-ci.yml but it never runs:
deploy_job:
script:
- echo "Deploying"
only:
- main
What is the likely reason?
medium
A. The job is missing a stage definition
B. The branch name is incorrect; it should be 'master' instead of 'main'
C. The 'only' keyword is deprecated; use 'rules' instead
D. The script command is invalid
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 A
Quick Check:
Default stage 'test' must be in 'stages' list [OK]
Hint: Default stage is 'test'; must be defined in 'stages' if used [OK]
Common Mistakes:
Assuming branch name is wrong without checking
Forgetting to define a stage (defaults to 'test' if missing)
Thinking script command syntax is wrong
5. You want a job to run only on the develop branch and after the build stage completes successfully. Which .gitlab-ci.yml snippet correctly achieves this?