Skip to content

Latest commit

 

History

History
194 lines (131 loc) · 3.12 KB

File metadata and controls

194 lines (131 loc) · 3.12 KB

if Condition in GitHub Actions

Understanding Conditional Execution for Jobs and Steps

What is an if expression?

if: is used to control whether a step or job should run.

It is evaluated before the job or step starts.

Syntax:

if: ${{ condition }}

If the condition is true → it runs If the condition is false → it is skipped


Why do we need if?

Workflows often require decisions such as:

  • Deploy only from main branch
  • Skip deployment for feature branches
  • Run tests only on pull requests
  • Run jobs only when a tag is pushed
  • Run steps only if previous step succeeded
  • Skip CI if commit message contains skip-ci

if: expressions help create smart CI/CD pipelines.


Most Important Use Case

Run Deployment Job Only on Main Branch

name: Branch Based Deployment
on:
  push:

jobs:

  build:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Building application"

  deploy:
    runs-on: ubuntu-latest
    needs: build
    if: ${{ github.ref == 'refs/heads/main' }}
    steps:
      - run: echo "Deploying to production"

What happens?

  • Push to main → build + deploy
  • Push to dev or feature → only build (deploy skipped)

Common Branch Conditions

Main branch

if: ${{ github.ref == 'refs/heads/main' }}

Dev branch

if: ${{ github.ref == 'refs/heads/dev' }}

Feature branches

if: ${{ startsWith(github.ref, 'refs/heads/feature/') }}

Tags

if: ${{ startsWith(github.ref, 'refs/tags/') }}

Pull Requests

if: ${{ github.event_name == 'pull_request' }}

More Useful Examples

1. Skip deployment if not main

if: ${{ github.ref != 'refs/heads/main' }}

2. Run only on push events

if: ${{ github.event_name == 'push' }}

3. Run only if previous job succeeded

if: ${{ success() }}

4. Run only if previous job failed

if: ${{ failure() }}

5. Run only if commit message contains a keyword

if: ${{ contains(github.event.head_commit.message, 'build') }}

6. Skip CI if commit message contains skip-ci

if: ${{ !contains(github.event.head_commit.message, 'skip-ci') }}

7. Run only if secret exists

if: ${{ secrets.PROD_TOKEN != '' }}

Full CI/CD Example with Conditional Deployment

name: Full Deployment Pipeline

on:
  push:

jobs:

  build:
    runs-on: ubuntu-latest
    steps:
      - run: echo "Building application"

  test:
    runs-on: ubuntu-latest
    needs: build
    steps:
      - run: echo "Running tests"

  deploy:
    runs-on: ubuntu-latest
    needs: test
    if: ${{ github.ref == 'refs/heads/main' }}
    steps:
      - run: echo "Deploying to production"

Workflow Behavior

  • main branch: build → test → deploy
  • dev branch: build → test
  • feature branch: build → test

Deployment runs only on main.


Summary

  • Use if: to control when jobs and steps run
  • Use github.ref to detect branch
  • Use github.event_name to detect event type
  • Use string functions like startsWith and contains
  • Use success() and failure() for job status
  • Use branch-based conditions for real deployments