Skip to content

Using conditions to control job execution

Prevent a job from running unless your conditions are met.

Overview

You can use the jobs.<job_id>.if conditional to prevent a job from running unless a condition is met. You can use any supported context and expression to create a conditionals. For more information on which contexts are supported in this key, see “Contexts.”

When you use expressions in an if conditional, you can, optionaly, omit the ${{ }} expression syntax because the orchestrator automatically evaluates the if conditional as an expression. However, this exception does not apply everywhere.

You must always use the ${{ }} expression syntax or escape with '', "", or () when the expression starts with !, since ! is a reserved notation in YAML format. For example:

if: ${{ ! startsWith(opentf.actor, 'ci-') }}

For more information, see “Expressions.”

Example: Only run job for specified actor

This example used if to control when the production-deploy job can run. It will only run if the user or trigger token is named production-deployer. Otherwise, the job will be marked as skipped.

metadata:
  name: example workflow
jobs:
  production-deploy:
    if: opentf.actor == 'production-deployer'
    runs-on: linux
    steps:
    - uses: actions/checkout@v2
      with:
        repository: https://git.example.com/foo.git
    - run: ./deploy.sh

Note

In some parts of the workflow you cannot use environment variables. Instead you can use contexts to access the value of an environment variable. For more information, see “Variables.”