Jobs and steps
This page provides an overview of CircleCI jobs and steps.
Jobs overview
A CircleCI job is a collection of steps. All of the steps in the job are executed in a single unit, either within a fresh container, or a virtual machine. Jobs are orchestrated using workflows.
The following diagram illustrates how data flows between jobs:
-
Workspaces persist data between jobs in a single workflow.
-
Caching persists data between the same job in different workflows runs.
-
Artifacts persist data after a workflow has finished.
Jobs can be run in Docker containers, using the Docker executor, or in virtual machines using the machine
executor, with Linux, macOS, or Windows images. Secondary containers or VMs can be configured to attach services, such as databases, to run alongside your jobs.
When using the Docker executor, images listed under the docker
key specify the containers you want to start for your job. Any public Docker images can be used with the Docker executor, but CircleCI provides convenience images for a variety of use-cases. Full lists of available convenience and VM images are available in the CircleCI Developer Hub.
See the Introduction to Execution Environments document for a comparison of the different executor types, and links to further information for each option.
Job naming
When you configure a job you will give it a name. This is its job property name:
version: 2.1
jobs:
my-job: # the job property name
The job property name can be anything you like with the following restrictions:
-
Must be unique within the jobs stanza.
-
Only ASCII alphabet characters, underscores, and dashes.
-
No spaces.
When you configure a job in a workflow you have the option to give it an alternative name. This alternative name can be useful if you want to call a job more than once in a workflow.
workflows:
jobs:
my-job: # the job property name
name: My Job # the alternative job name within the workflow
The job name within the workflow can be anything you like including spaces and special characters.
Steps overview
Steps are collections of executable commands, which are run during a job.
The checkout
key is required under steps
to checkout your code. The run
key enables addition of arbitrary, multi-line shell command scripting.
In addition to the run
key, keys for save_cache
, restore_cache
, store_artifacts
, store_test_results
, and add_ssh_keys
are nested under steps. For a full list of step options see the Steps key section of the Configuration Reference.
Passing parameters to jobs
Using parameters allows you to run a single job multiple times for different scenarios, such as different package versions or execution environments. An extension of this functionality is matrix jobs. Below is a basic example of passing a parameter to a job when it is run.
Using Docker? Authenticating Docker pulls from image registries is recommended when using the Docker execution environment. Authenticated pulls allow access to private Docker images, and may also grant higher rate limits, depending on your registry provider. For further information see Using Docker authenticated pulls. |
version: 2.1
jobs:
print-a-message:
docker:
- image: cimg/base:2022.03
parameters:
message:
type: string
steps:
- run: echo << parameters.message >>
workflows:
my-workflow:
jobs:
- print-a-message:
message: Hello!
Using a job from an orb
Orbs are packages or reusable configuration that you can use in your projects. Orbs usually contain commands that you can use in your jobs, as well as whole jobs that you can schedule in your workflows.
Take the Slack orb as an example. This orb provides a job called on-hold
, which you can use in your workflows. This job pauses the workflow to require manual approval, and sends a slack notification. To use this job, reference it in your workflow (see line 10):
version: 2.1
orbs:
slack: circleci/slack@4.1
workflows:
on-hold-example:
jobs:
- my_test_job
- slack/on-hold:
context: slack-secrets
requires:
- my_test_job
- pause_workflow:
requires:
- my_test_job
- slack/on-hold
type: approval
- my_deploy_job:
requires:
- pause_workflow
Using a command from an orb in a job
Using the Slack orb as an example again, this orb includes a command called notify
, which is used to notify a specified slack channel. You can reference this command in your job as follows (see line 16):
This example also uses the Node orb.
version: 2.1
orbs:
node: 'circleci/node:4.1'
slack: circleci/slack@4.1
jobs:
deploy:
executor:
name: node/default
steps:
- checkout
- node/install-packages
- run:
command: npm run deploy
- slack/notify:
channel: ABCXYZ
event: fail
template: basic_fail_1
workflows:
deploy_and_notify:
jobs:
- deploy:
context: slack-secrets
Next steps
-
Read more about orchestrating jobs in the Using Workflows to Schedule Jobs page.
-
Read more about passing data between jobs in the Using Workspaces to Share Data between Jobs page.