Skip to main content
In this guide, you’ll enhance an existing GitLab CI/CD pipeline by:
  • Renaming jobs for clarity
  • Introducing a dedicated Docker stage
  • Controlling execution order with the needs keyword
By the end, you’ll understand how to build a Directed Acyclic Graph (DAG) of jobs that enforces logical progression and efficient failure handling.

1. Original Workflow

Here’s the starting pipeline, which builds ASCII art, runs tests, and deploys:

2. Renaming Jobs and Cleaning Up

Rename jobs for readability and remove the unnecessary sleep command:
At this point, the pipeline runs three sequential stages: build, test, then deploy.

3. Introducing a Docker Stage

Add a new docker stage with three placeholder jobs:
By default, GitLab runs all three Docker jobs in parallel once the test stage completes.
When jobs share the same stage, GitLab CI/CD executes them in parallel. This may cause docker_push to run before docker_build, or allow failures in docker_testing without halting docker_push.

4. Docker Jobs Overview

5. Sequencing with needs

Use the needs keyword to enforce a DAG of dependencies and ensure correct ordering:
After committing, the UI will reflect this sequence:
build → test → docker_build → docker_testing → docker_push.
If docker_testing fails, docker_push is automatically skipped.
Console output for docker_testing:

6. Ignoring Stage Order

You can also launch jobs as soon as their dependencies complete, even if they’re in later stages. For example:
Here, docker_build starts immediately after build_file, running in parallel with test_file.

7. Conclusion

Using the needs keyword allows you to:
  • Sequence jobs within the same stage
  • Override default stage ordering for earlier execution
  • Visualize your pipeline as a clear DAG
This gives you precise control over dependencies and failure handling in your GitLab CI/CD workflows.

Watch Video

Practice Lab