MOTOSHARE 🚗🏍️
Turning Idle Vehicles into Shared Rides & Earnings
From Idle to Income. From Parked to Purpose.
Earn by Sharing, Ride by Renting.
Where Owners Earn, Riders Move.
Owners Earn. Riders Move. Motoshare Connects.
With Motoshare, every parked vehicle finds a purpose.
Owners earn. Renters ride.
🚀 Everyone wins.
The needs: keyword allows you to define explicit dependencies between jobs, creating a Directed Acyclic Graph (DAG) of your pipeline. This means a job can start as soon as the specific jobs it needs have completed successfully, rather than waiting for the entire previous stage to finish. This can significantly speed up your pipeline execution by running jobs earlier than they would in a strict stage-based order.
Example .gitlab-ci.yml with needs:
YAML
# .gitlab-ci.yml
stages:
- build
- test
- deploy
default:
image: alpine:latest
# For 'needs' to automatically download artifacts from prerequisite jobs,
# those jobs must define 'artifacts'. We'll add simple artifacts for demonstration.
# --- Build Stage Jobs ---
build_backend:
stage: build
script:
- echo "--- Building Backend ---"
- sleep 10 # Simulate build time
- mkdir -p backend_build/
- echo "backend_app_v1" > backend_build/version.txt
- echo "Backend built successfully."
artifacts:
paths:
- backend_build/
expire_in: 1 hour
build_frontend:
stage: build
script:
- echo "--- Building Frontend ---"
- sleep 15 # Simulate build time
- mkdir -p frontend_build/
- echo "frontend_app_v1" > frontend_build/ui_bundle.js
- echo "Frontend built successfully."
artifacts:
paths:
- frontend_build/
expire_in: 1 hour
# --- Test Stage Jobs ---
# This job will start as soon as 'build_backend' finishes, without waiting for 'build_frontend'.
test_backend_api:
stage: test
needs: ["build_backend"] # Explicitly depends on 'build_backend'
script:
- echo "--- Testing Backend API ---"
- echo "Checking backend build artifacts..."
- if [ -f "backend_build/version.txt" ]; then cat backend_build/version.txt; else echo "Backend version file not found!"; exit 1; fi
- sleep 8 # Simulate test time
- echo "Backend API tests PASSED."
# This job will start as soon as 'build_frontend' finishes, without waiting for 'build_backend'.
test_frontend_ui:
stage: test
needs: ["build_frontend"] # Explicitly depends on 'build_frontend'
script:
- echo "--- Testing Frontend UI ---"
- echo "Checking frontend build artifacts..."
- if [ -f "frontend_build/ui_bundle.js" ]; then echo "Frontend bundle found."; else echo "Frontend bundle not found!"; exit 1; fi
- sleep 10 # Simulate test time
- echo "Frontend UI tests PASSED."
# This job needs both build jobs to complete before it can start.
# It will start as soon as BOTH 'build_backend' AND 'build_frontend' are done.
integration_tests:
stage: test
needs: ["build_backend", "build_frontend"] # Depends on multiple jobs
script:
- echo "--- Running Integration Tests ---"
- echo "Verifying backend and frontend integration..."
- if [ -f "backend_build/version.txt" ] && [ -f "frontend_build/ui_bundle.js" ]; then echo "All necessary artifacts found."; else echo "Missing artifacts for integration!"; exit 1; fi
- sleep 12 # Simulate integration test time
- echo "Integration tests PASSED."
# --- Deploy Stage Job ---
# This job will start as soon as 'integration_tests' finishes.
# It also implicitly benefits from artifacts if integration_tests produced any relevant for deployment.
deploy_application:
stage: deploy
needs: ["integration_tests"]
script:
- echo "--- Deploying Application ---"
- echo "Deploying based on successful integration tests."
- sleep 10 # Simulate deployment time
- echo "Application deployed successfully!"
when: on_success # Default, but good for deploy jobs
Code language: PHP (php)
Explanation:
needs:Keyword:- Used within a job definition to specify an array of other jobs that must complete successfully before this job can start.
- It allows you to create a more granular dependency chain than what
stagesprovide.
stages:Still Define the Overall Flow (and Visualization):- Jobs still belong to
stages. Stages are used for:- Visual grouping in the GitLab CI/CD pipeline graph.
- A fallback execution order if
needs:is not used. - Determining the order if
needs:creates parallel paths that eventually converge on a stage.
- However,
needs:can make a job start earlier than its defined stage if itsneedsdependencies are met.
- Jobs still belong to
test_backend_apiJob:stage: testneeds: ["build_backend"]: This is the key.test_backend_apiwill not wait forbuild_frontend(which is also in thebuildstage) to complete. As soon asbuild_backendfinishes successfully,test_backend_apican start, even ifbuild_frontendis still running.
test_frontend_uiJob:stage: testneeds: ["build_frontend"]: Similar to the backend test, this job starts as soon asbuild_frontendis done, independently ofbuild_backend‘s completion ortest_backend_api‘s status (unlesstest_backend_apiwas also in itsneedslist).
integration_testsJob:stage: testneeds: ["build_backend", "build_frontend"]: This job depends on bothbuild_backendandbuild_frontend. It will only start after both of these prerequisite jobs have completed successfully.
deploy_applicationJob:stage: deployneeds: ["integration_tests"]: The deployment will only start afterintegration_testsis successful.
- Artifacts and
needs:- When you use
needs:, by default, GitLab will attempt to download artifacts from all the jobs listed in theneedsarray. This is equivalent toneeds: [{ job: "job_name", artifacts: true }]. - In the example,
build_backendproduces an artifact inbackend_build/.test_backend_api(whichneeds: ["build_backend"]) will have access to thisbackend_build/directory and its contents. - If you don’t want artifacts from a needed job, you can specify
needs: [{ job: "job_name", artifacts: false }].
- When you use
- Impact of Failure:
- If any job listed in a
needsarray fails (or is skipped, or canceled), the dependent job will be skipped by default and will not run.
- If any job listed in a
How needs: Speeds Up Pipelines:
Imagine the build stage without needs:
build_backend(10s) andbuild_frontend(15s) run in parallel.- The
buildstage finishes after the longest job, so after 15s. - Only then would the
teststage begin.
With needs::
build_backendfinishes in 10s.test_backend_apican start immediately.build_frontendfinishes in 15s.test_frontend_uican start immediately.integration_testsstarts when bothbuild_backendandbuild_frontendare done (i.e., after 15s in this example).- The overall pipeline can proceed much faster because testing for completed build components starts earlier.
Key Concepts and Benefits:
- DAG (Directed Acyclic Graph):
needs:allows you to define your pipeline as a graph of dependencies, rather than a strict linear sequence of stages. - Reduced Latency: Jobs start as soon as their true dependencies are met, leading to shorter overall pipeline durations.
- Increased Efficiency: Better utilization of runners as jobs don’t wait unnecessarily.
- Implicit Artifact Download: Simplifies passing artifacts between dependent jobs.
- Optional Dependencies: You can also use
needs: [{ job: "optional_job", optional: true }]. Ifoptional_jobfails or is skipped, the current job will still run (but it won’t download artifacts from the optional job if it didn’t produce them).
Using needs: is a powerful way to optimize your GitLab CI/CD pipelines, especially for projects with multiple independent build components or test suites that can run as soon as their specific dependencies are ready.