Harness CI pipelines consist of various steps, among which two types that are commonly used are Service Dependency and Background steps. In this document, we will compare the functionality and use cases of these two step types, highlighting their similarities and differences. This guide is intended for Harness CI users who are familiar with Harness CI pipeline concepts and want to understand how to effectively use Service Dependency and Background steps in their CI pipelines.
Command execution Inside container:
- Service dependency does not allow Command execution inside containers beyond the docker entry point.
- The Background Step enables the execution of commands inside containers, including POSIX shell script commands beyond the entry point.
Runs on Vm and containers both
- Service dependency runs as an isolated container that is available to all other steps through the same Docker network.
- Background steps can be executed both within a container or directly on a virtual machine. If the pipeline is running on a virtual machine build infrastructure, the background service can be run directly on the VM instead of in a container by leaving the Container Registry and Image fields empty.
|Runs on VM||Runs inside a container|
Conditional Execution Support:
- Service dependencies are the starting point of any pipeline execution and are therefore guaranteed to start execution first.
- Background step supports Conditional Execution ie you can select an option for step execution based on previous stage success, failure, or always perform along with JEXL Condition.
- step: type: Background name: Background_4 identifier: Background_4 spec: shell: Sh command: 'watch -n0 "docker images; docker ps -a" ' when: stageStatus: Failure condition: <+trigger.sourceBranch> == "dev"
Lopping Strategy Support
- Service dependency step dose does not support a looping strategy
- Background Steps can be repeated using looping strategies, allowing multiple inputs to be processed without duplicating the Step. This simplifies the Pipeline, making it more readable, organized, and easier to maintain.
- step: type: Background name: Background_4 identifier: Background_4 spec: connectorRef: account.harnessImage image: <+repeat.item> shell: Sh command: python --version when: stageStatus: Success failureStrategies:  strategy: repeat: items: - 'pytohn:2.7' - 'python:3.7' maxConcurrency: 2
Publish Test Reports
- Background step can Publish test results in JUnit format using the Report Path variable set in a Background step and view the dashboard after CI Pipeline execution.
- If a Service Dependecy is defined then it is considered as that dependency is vital for the pipeline to proceed ahead and hence it always starts execution before any other step in the CI pipeline
- Background steps can be used at any point in pipeline workflow in a parallel or synchronous fashion similar to any other CI step.
Background Steps are useful in the following situations:
- Running tests in the background while continuing with pipeline execution.
- Starting multiple services such as Redis, Mongo, and Postgres in the background.
- Running a process on a virtual machine throughout the pipeline, such as monitoring a log file.
- Testing containers using Docker-in-Docker.