Comparing Service Dependency and Background Steps in Harness CI : Functionality and Use Cases

Introduction:

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
 - step:
    type: Background
    name: Background_4
    identifier: Background_4
    spec:
      shell: Sh
      command: 'watch -n0 "docker images; docker ps -a" '

  - step:
      type: Background
      name: Background_4
      identifier: Background_4
      spec:
        connectorRef: account.harnessImage
        image: 'Dind:latest'
        shell: Sh
        command: 'watch -n0 "docker images; docker ps -a" '

 

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.

image

- 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.


Step Location

  • 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:

  1. Running tests in the background while continuing with pipeline execution.
  2. Starting multiple services such as Redis, Mongo, and Postgres in the background.
  3. Running a process on a virtual machine throughout the pipeline, such as monitoring a log file.
  4. Testing containers using Docker-in-Docker.
1 Like