CollabOps

Conditional Execution

Branching pipelines by branch and event using various if conditions

A pipeline that branches based on branch, event, and previous Job status using Job-level if conditions. 9 Jobs are executed, each with different conditions.

Full Code

name: conditional-pipeline

triggers:
  push:
    branches: [main, develop]
  change_request:
    branches: [main]

jobs:
  # ─────────────────────────────────────────
  # 1. Lint — Always runs (no condition)
  # ─────────────────────────────────────────
  lint:
    steps:
      - name: eslint
        image: node:18
        run: npm run lint

  # ─────────────────────────────────────────
  # 2. Unit test — Only when lint succeeds
  # ─────────────────────────────────────────
  unit-test:
    needs: [lint]
    if: "success()"                # Runs only when lint succeeds
    steps:
      - name: run-tests
        image: node:18
        run: npm test
        env:
          CI: "true"

  # ─────────────────────────────────────────
  # 3. Integration test — Only on push events
  #    (Only unit tests run on CRs)
  # ─────────────────────────────────────────
  integration-test:
    needs: [lint]
    if: "collabops.event_name == 'push'"
    steps:
      - name: setup-db
        image: postgres:15
        run: echo "Setting up test database..."
      - name: run-integration
        image: node:18
        run: npm run test:integration

  # ─────────────────────────────────────────
  # 4. Build — When unit tests succeed
  # ─────────────────────────────────────────
  build:
    needs: [unit-test]
    if: "success()"
    steps:
      - name: build-app
        image: node:18
        run: npm run build

  # ─────────────────────────────────────────
  # 5. Staging deploy — develop branch + success
  # ─────────────────────────────────────────
  deploy-staging:
    needs: [build, integration-test]
    if: "collabops.ref == 'refs/heads/develop' && success()"
    steps:
      - name: deploy
        run: |
          echo "Deploying to staging..."
          kubectl apply -f k8s/staging/

  # ─────────────────────────────────────────
  # 6. Production deploy — main branch push only
  # ─────────────────────────────────────────
  deploy-production:
    needs: [build]
    if: "collabops.ref == 'refs/heads/main' && collabops.event_name == 'push'"
    steps:
      - name: approval-check
        run: echo "Checking deployment approval..."
      - name: deploy
        run: |
          echo "Deploying to production..."
          kubectl apply -f k8s/production/

  # ─────────────────────────────────────────
  # 7. Success notification — On deployment success
  # ─────────────────────────────────────────
  notify-success:
    needs: [deploy-staging, deploy-production]
    if: "success()"
    steps:
      - name: notify
        uses: "collabops/slack-notify@v1"
        with:
          webhook-url: ${{ secrets.SLACK_WEBHOOK }}
          message: "Deployment succeeded: ${{ collabops.ref_name }}"
          color: good

  # ─────────────────────────────────────────
  # 8. Failure notification — On build pipeline failure
  # ─────────────────────────────────────────
  notify-failure:
    needs: [lint, unit-test, build]
    if: "failure()"
    steps:
      - name: notify
        uses: "collabops/slack-notify@v1"
        with:
          webhook-url: ${{ secrets.SLACK_WEBHOOK }}
          message: "Build failed: ${{ collabops.ref_name }} by ${{ collabops.actor }}"
          color: danger

  # ─────────────────────────────────────────
  # 9. Cleanup — Always runs regardless of success/failure
  # ─────────────────────────────────────────
  cleanup:
    needs: [lint, unit-test, integration-test, build, deploy-staging, deploy-production]
    if: "always()"                 # Always runs regardless of outcome
    steps:
      - name: cleanup-artifacts
        run: echo "Cleaning up build artifacts..."

Execution Summary by Condition

JobConditionDescription
lint(none)Always runs
unit-testsuccess()When lint succeeds
integration-testcollabops.event_name == 'push'Only on push events
buildsuccess()When tests succeed
deploy-stagingref == develop && success()develop branch + success
deploy-productionref == main && event == pushmain push only
notify-successsuccess()On deployment success
notify-failurefailure()On pipeline failure
cleanupalways()Always runs

Execution Scope by Event

EventJobs Executed
push (main)lint → test → integration → build → deploy-production → notify → cleanup
push (develop)lint → test → integration → build → deploy-staging → notify → cleanup
change_requestlint → test → build → cleanup (integration tests/deploy skipped)

Table of Contents