Skip to main content

Working with Jobs

1. Defining Basic Jobs

Property Description Example
jobs List of jobs to run in the pipeline { build: { steps: [...] } }
name Unique identifier for the job build
steps Ordered list of steps to execute [checkout, run]

Example: Basic job definition

jobs:
  build:
    steps:
      - checkout
      - run: echo "Hello World"

2. Adding Job Steps

Step Type Description Usage
checkout Checks out source code from the repository - checkout
run Executes a command or script - run: npm install
store_artifacts Stores files generated during the job - store_artifacts: path: dist

Example: Adding multiple steps

steps:
  - checkout
  - run: npm install
  - run: npm test
  - store_artifacts:
      path: coverage

3. Setting Resource Classes

Resource Class Description Usage
small Default resource class with 1 CPU and 2GB RAM resource_class: small
medium 2 CPUs and 4GB RAM resource_class: medium
large 4 CPUs and 8GB RAM resource_class: large
xlarge 8 CPUs and 16GB RAM resource_class: xlarge

Example: Using resource class

jobs:
  build:
    resource_class: medium
    steps:
      - checkout
      - run: npm install

4. Configuring Job Environment

Environment Variable Description Example
environment Set environment variables for the job environment: NODE_ENV: production
working_directory Directory to run commands in working_directory: /home/circleci/project
shell Shell to use for commands shell: /bin/bash

Example: Setting environment variables

jobs:
  build:
    environment:
      NODE_ENV: production
      CI: true
    steps:
      - checkout

5. Using Working Directory

Property Description Example
working_directory Directory to run commands in working_directory: /home/circleci/project
checkout Automatically checks out to working directory - checkout

Example: Setting working directory

jobs:
  build:
    working_directory: /home/circleci/project
    steps:
      - checkout
      - run: echo "Current directory is $(pwd)"

6. Setting Job Shell

Shell Description Usage
/bin/bash Default shell with full POSIX support shell: /bin/bash
/bin/sh Basic POSIX shell shell: /bin/sh
/bin/zsh Zsh shell with extended features shell: /bin/zsh

Example: Using custom shell

jobs:
  build:
    shell: /bin/zsh
    steps:
      - checkout
      - run: echo "Using Zsh shell"

7. Using Parallelism (parallelism key)

Property Description Example
parallelism Number of parallel containers to run parallelism: 4
matrix Define matrix parameters for parallel jobs matrix: { params: [a, b] }

Example: Using parallelism

jobs:
  test:
    parallelism: 4
    steps:
      - checkout
      - run: npm test

8. Choosing Resource Classes

Resource Class CPU Count Memory (GB) Use Case
small 1 2 Basic builds and tests
medium 2 4 Medium-sized builds or tests
large 4 8 Large builds or complex tests
xlarge 8 16 Very large builds or intensive tasks

Example: Choosing resource class

jobs:
  build:
    resource_class: large
    steps:
      - checkout
      - run: npm install

9. Using GPU Resources

Property Description Example
resource_class Use GPU-enabled resource class resource_class: gpu-medium
machine Use machine executor with GPU support machine: { image: ubuntu-2004:2023.09.1 }

Example: Using GPU resources

jobs:
  train-model:
    resource_class: gpu-medium
    steps:
      - checkout
      - run: python train.py

10. Implementing Job Dependencies

Property Description Example
requires List of jobs that must complete before this job runs requires: [build]
needs Alternative syntax for job dependencies needs: [build]

Example: Implementing job dependencies

jobs:
  build:
    steps:
      - checkout
      - run: npm install

  test:
    requires: [build]
    steps:
      - checkout
      - run: npm test

  deploy:
    requires: [test]
    steps:
      - checkout
      - run: npm run deploy

Was this article helpful?