GitLab CI basics - Time & Space Complexity
We want to understand how the time GitLab CI takes to run grows as the number of jobs or stages increases.
How does adding more jobs affect the total time GitLab CI spends executing pipelines?
Analyze the time complexity of this simple GitLab CI pipeline configuration.
stages:
- build
- test
build_job:
stage: build
script:
- echo "Building..."
test_job_1:
stage: test
script:
- echo "Testing part 1..."
test_job_2:
stage: test
script:
- echo "Testing part 2..."
This pipeline has two stages: build and test. The build stage has one job, and the test stage has two jobs running in parallel.
Look for repeated actions that affect total execution time.
- Primary operation: Running each job's script commands.
- How many times: Once per job, so total jobs count.
As you add more jobs, the total commands to run increase roughly by the number of jobs.
| Input Size (jobs) | Approx. Operations (job scripts run) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The total work grows directly with the number of jobs.
Time Complexity: O(n)
This means the total time to run the pipeline grows linearly with the number of jobs if jobs run sequentially.
[X] Wrong: "Adding more jobs always makes the pipeline take that many times longer."
[OK] Correct: Jobs in the same stage run in parallel, so adding jobs in one stage may not increase total time as much as expected.
Understanding how pipeline time grows helps you design efficient CI workflows and explain your choices clearly in discussions.
"What if all jobs were in a single stage running sequentially? How would the time complexity change?"
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
