Gitlab CI Parallel Jobs Example - YAML Configuration

Learn how to configure parallel jobs in GitLab CI using this example YAML configuration. This guide demonstrates setting up parallel jobs, dependencies, and viewing results.

Gitlab CI Parallel Jobs Example

variables:
  RANDOM_WORD: hello

image: busybox:latest

stages:
  - single-job
  - parallel-jobs
  - parallel-jobs-with-dependencies
  - results

job-one:
  stage: single-job
  script:
    - HOSTNAME=$(hostname)
    - echo "hostname=${HOSTNAME}, random-number=${RANDOM}, pipeline-id=${CI_PIPELINE_ID}" > file.txt
  artifacts:
    paths:
      - file.txt
      
job-two:
  stage: parallel-jobs
  script:
    - HOSTNAME=$(hostname)
    - echo "hostname=${HOSTNAME}, random-number=${RANDOM}, pipeline-id=${CI_PIPELINE_ID}" >> file.txt
  artifacts:
    paths:
      - file.txt

job-three:
  stage: parallel-jobs
  script:
    - HOSTNAME=$(hostname)
    - echo "hostname=${HOSTNAME}, random-number=${RANDOM}, pipeline-id=${CI_PIPELINE_ID}" >> file.txt
  artifacts:
    paths:
      - file.txt

job-four:
  stage: parallel-jobs-with-dependencies
  needs: [job-three]
  script:
    - HOSTNAME=$(hostname)
    - echo "hostname=${HOSTNAME}, random-number=${RANDOM}, pipeline-id=${CI_PIPELINE_ID}" >> file.txt
  artifacts:
    paths:
      - file.txt

job-five:
  stage: parallel-jobs-with-dependencies
  needs: [job-four]
  dependencies:
    - job-four
  script:
    - HOSTNAME=$(hostname)
    - echo "hostname=${HOSTNAME}, random-number=${RANDOM}, pipeline-id=${CI_PIPELINE_ID}" >> file.txt
  artifacts:
    paths:
      - file.txt

job-six:
  stage: parallel-jobs-with-dependencies
  needs: [job-four]
  dependencies:
    - job-four
  script:
    - HOSTNAME=$(hostname)
    - echo "hostname=${HOSTNAME}, random-number=${RANDOM}, pipeline-id=${CI_PIPELINE_ID}" >> file.txt
  artifacts:
    paths:
      - file.txt

view-results:
  stage: results
  script:
    - cat file.txt

This example demonstrates a GitLab CI configuration for parallel jobs. Notice the use of stages and dependencies to manage the execution flow. The artifacts section ensures that output from each job is collected.

Further Reading

For more information on GitLab CI, refer to the official documentation: