Skip to main content

Managing Compute Minutes and Costs

1. Understanding Compute Minutes Allocation

Resource Description Allocation Details
Project Compute Minutes Measures the CPU time consumed by CI/CD jobs in a project Allocated based on plan limits; tracked per project
Group Compute Minutes Tracks compute minutes across all projects in a group Aggregates usage from member projects
Instance Compute Minutes Tracks compute minutes across all projects in a GitLab instance Used for instance-level billing and analytics

Example: Compute Minutes Usage

gitlab-ci-lint --project-id 12345 --job-name build-job
Note: Compute minutes are consumed based on the actual runtime of jobs, including any queued time.

Key Points

  • Compute minutes are a core metric for tracking CI/CD resource consumption
  • Each job's runtime contributes to the project’s compute minutes
  • Usage is tracked at project, group, and instance levels
  • Billing is based on the total compute minutes consumed

2. Tracking Compute Usage

Usage Type Description Tracking Method Notes
Pipeline Minutes Compute time consumed by CI/CD pipelines gitlab-ci.yml job configuration and timeout settings Minutes are counted per job, not per pipeline
Runner Type Runner used for pipeline execution (shared or dedicated) Runner configuration in .gitlab-ci.yml Different runners may have different cost structures
Project Usage Total compute minutes used by a project over time GitLab UI dashboard or API endpoint /projects/:id/usage Used to calculate cost and enforce limits

Example: Pipeline with Timeout Configuration

job_name:
  script:
    - echo "Running job"
  timeout: 30 minutes
Note: Compute minutes are consumed based on actual runtime, not wall clock time. Jobs that exceed their timeout will be terminated and still count towards the total minutes used.

3. Optimizing Job Duration

Strategy Description Impact
Parallel Jobs Run independent jobs in parallel using parallel keyword in job definitions. Reduces total pipeline time by executing multiple tasks simultaneously.
Caching Dependencies Use cache to store and reuse dependencies between jobs. Eliminates redundant setup steps, speeding up job execution.
Resource Constraints Limit job resource usage with max_timeout, timeout, and resources. Prevents long-running jobs from consuming excessive compute minutes.
Job Scheduling Use scheduled pipelines to run jobs during off-peak hours. Reduces cost by leveraging lower-cost compute time slots.

Example: Parallel Jobs

job1:
  script:
    - echo "Running task 1"
job2:
  script:
    - echo "Running task 2"
parallel_job:
  parallel:
    matrix:
      - JOB: job1
      - JOB: job2

Example: Caching Dependencies

cache:
  key: "dependencies"
  paths:
    - node_modules/
Note: Compute minutes are charged based on job runtime and resource usage. Optimizing job duration directly impacts cost efficiency.

4. Using Cost Factors

Factor Description Default Value Notes
compute_time Time spent running jobs in seconds 0 Used to calculate cost based on job duration
pipeline_count Number of pipelines executed 0 Counts each pipeline run as a unit
job_count Total number of jobs in pipelines 0 Each job contributes to compute minutes
runner_type Type of runner used (shared or dedicated) shared Dedicated runners cost more than shared

Example: Cost Factor Configuration

cost_factors:
  compute_time: 0.01
  pipeline_count: 5
  job_count: 0.2
  runner_type:
    shared: 0.005
    dedicated: 0.02
Note: Cost factors are used to calculate the compute minutes consumed by CI/CD pipelines in GitLab.

5. Implementing Resource Limits

Resource Limit Type Default Value Description
max_job_timeout Seconds 3600 Maximum time a job can run before being terminated
max_artifacts_size MB 100 Maximum size of artifacts that can be stored per job
max_cache_size MB 100 Maximum size of cache that can be used per job
max_parallel_jobs Count 10 Maximum number of jobs that can run in parallel within a pipeline

Example: Setting Resource Limits in .gitlab-ci.yml

job_name:
  script:
    - echo "Running job"
  timeout: 3600
  artifacts:
    expire_in: 1 week
  cache:
    key: "cache-key"
    paths:
      - node_modules/
Note: Resource limits can be set at the project, group, or instance level using GitLab's admin settings.

6. Using Timeout Optimization

Option Values Notes
timeout 1h Maximum time a job can run before being canceled
timeout (per job) 30m Default timeout for individual jobs in a pipeline
timeout (pipeline) 2h Maximum time a pipeline can run before being canceled

Example: Setting Timeout in .gitlab-ci.yml

job_name:
  script:
    - echo "Running job"
  timeout: 30m
Note: Setting a timeout prevents long-running jobs from consuming excessive compute minutes.
Warning: If a job exceeds its timeout, it will be marked as failed and the pipeline may continue running other jobs.

7. Reducing Unnecessary Pipelines

Example: Using only and except to limit pipeline runs

deploy:
  only:
    - main
    - /^release\/.*$/
  except:
    - branches
Option Description Notes
only main, tags, or branches Restricts pipeline execution to specific refs
except branches, tags Excludes specified refs from pipeline execution
Note: Using only and except can significantly reduce compute minutes by preventing pipelines from running on every commit.

Example: Using rules to conditionally run jobs

test:
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request"'
      when: on_success
    - if: '$CI_COMMIT_BRANCH == "main"'
      when: always
Rule Condition Action Notes
if: '$CI_PIPELINE_SOURCE == "merge_request"' when: on_success Runs job only for merge requests
if: '$CI_COMMIT_BRANCH == "main"' when: always Runs job on every commit to main branch
Warning: Misconfigured rules can cause pipelines to run on unintended branches or merge requests, increasing compute costs.

Best Practices for Reducing Pipeline Costs

  • Use only and except to limit pipeline execution to necessary branches or tags.
  • Implement rules for conditional job execution based on commit source or branch.
  • Avoid running pipelines on every commit to feature branches unless necessary.

8. Implementing Cost Alerts

Example: Setting up a cost alert in GitLab

curl --request POST \
  --url https://gitlab.example.com/api/v4/projects/123/alerts \
  --header 'PRIVATE-TOKEN: glpat-abc123' \
  --header 'Content-Type: application/json' \
  --data '{
    "name": "Compute Minutes Alert",
    "description": "Alert when compute minutes exceed 1000",
    "condition": "> 1000",
    "metric": "compute_minutes",
    "threshold": 1000,
    "notification_channel": "email"
  }'
Field Description Values
name Name of the alert string
description Human-readable description of the alert string
condition Comparison operator for threshold check >, <, =
metric Resource metric to monitor compute_minutes, pipeline_runs
threshold Value to trigger alert number
notification_channel How to notify users email, slack, webhook
Note: Alerts are available in GitLab Premium and above.
Warning: Cost alerts are not available in GitLab Community Edition.

9. Using Efficient Docker Images

Image Size (MB) Build Time Notes
alpine:latest 5.5 2s Minimal size, good for lightweight tasks
node:alpine 120 15s Node.js with Alpine, efficient for JS builds
python:3.11-slim 180 25s Lightweight Python image, ideal for CI
ubuntu:22.04 250 30s Full Ubuntu, less efficient but more compatible

Example: Using Alpine-based image in GitLab CI

job_name:
  image: alpine:latest
  script:
    - apk add --no-cache git
    - git clone https://gitlab.com/user/repo.git
Note: Alpine images are smaller but may lack some system libraries. Ensure compatibility with your tools.
Warning: Avoid using latest in production CI to prevent unexpected behavior from image updates.
Metric Description Tracking Method Notes
Compute Minutes Total minutes of CI/CD runner time consumed Usage metrics in GitLab UI Used to calculate cost based on plan limits
Runner Type Shared vs. dedicated runners used Runner settings and logs Dedicated runners cost more per minute
Pipeline Duration Average time per pipeline execution Job logs and analytics dashboard Longer pipelines increase compute cost
Monthly Cost Total cost for the month based on compute minutes Billing dashboard or admin settings Reflects usage and plan limits

Example: Cost Trend Query

curl --header "PRIVATE-TOKEN: <your_access_token>" \
  "https://gitlab.example.com/api/v4/projects/<project_id>/usage_trends"
Note: Compute minutes are tracked at the project level and can be viewed in the Settings > CI/CD > Usage Trends section.
Warning: Exceeding your plan's compute minutes may result in additional charges or pipeline halts.

Was this article helpful?