Skip to content

somaz94/ternary-operator

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

Ternary Operator Action

CI License Latest Tag GitHub Marketplace

A GitHub Action for evaluating conditional expressions and setting dynamic outputs based on the results. Perfect for creating flexible, condition-driven workflows.


Features

  • Multiple Conditions: Evaluate up to 10 conditions in a single step
  • Rich Operators: Support for comparison (==, !=, <, >, <=, >=), logical (&&, ||, NOT), special (IN), string (CONTAINS, STARTS_WITH, ENDS_WITH), regex (MATCHES), and validation (EMPTY, NOT_EMPTY) operators
  • Case Sensitivity Control: Optional case-insensitive comparison mode
  • Default Values: Fallback values when condition evaluation fails
  • JSON Result Output: Combined JSON output for easy multi-condition access
  • Simple Syntax: Clean, readable condition expressions
  • Debug Mode: Detailed logging for troubleshooting
  • Zero Dependencies: Lightweight Docker-based action
  • Fast Execution: Efficient condition evaluation

Quick Start

name: Conditional Deploy
on: [push]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      
      # Set environment variables
      - name: Set Variables
        run: |
          echo "SERVICE=game" >> $GITHUB_ENV
          echo "ENVIRONMENT=prod" >> $GITHUB_ENV
      
      # Evaluate conditions
      - name: Check Deployment Rules
        id: check
        uses: somaz94/ternary-operator@v1
        with:
          conditions: 'SERVICE IN game,batch && ENVIRONMENT == prod'
          true_values: 'deploy-allowed'
          false_values: 'deploy-blocked'
      
      # Use the result
      - name: Deploy
        if: steps.check.outputs.output_1 == 'deploy-allowed'
        run: ./deploy.sh

Use Cases

  • Conditional Deployments: Deploy based on service, environment, or branch
  • Dynamic Configuration: Select configs based on multiple conditions
  • Feature Flags: Enable/disable features conditionally
  • Multi-Environment CI/CD: Different strategies per environment
  • Resource Scaling: Adjust resources based on conditions

Documentation


Core Documentation


Operators at a Glance

Category Operators Example
Comparison == != < > <= >= VERSION >= 1.5
Logical && || NOT SERVICE == game && ENV == prod
Special IN SERVICE IN game,batch,api
String CONTAINS STARTS_WITH ENDS_WITH BRANCH STARTS_WITH feature/
Regex MATCHES TAG MATCHES ^v[0-9]+\.[0-9]+$
Validation EMPTY NOT_EMPTY API_KEY NOT_EMPTY

→ See detailed operator documentation


Common Examples


Example 1: Simple Condition

- uses: somaz94/ternary-operator@v1
  id: check
  with:
    conditions: 'ENVIRONMENT == prod'
    true_values: 'production-config'
    false_values: 'development-config'

Example 2: IN Operator (Simplified OR)

- uses: somaz94/ternary-operator@v1
  id: check
  with:
    # Instead of: SERVICE == game || SERVICE == batch || SERVICE == api
    conditions: 'SERVICE IN game,batch,api'
    true_values: 'valid-service'
    false_values: 'invalid-service'

Example 3: Multiple Conditions

- uses: somaz94/ternary-operator@v1
  id: checks
  with:
    conditions: >-
      SERVICE IN game,batch,api,
      ENVIRONMENT == prod,
      VERSION >= 1.5
    true_values: 'service-ok,prod-deploy,new-version'
    false_values: 'service-fail,no-deploy,old-version'

# Use outputs
- run: echo "Service: ${{ steps.checks.outputs.output_1 }}"
- run: echo "Environment: ${{ steps.checks.outputs.output_2 }}"
- run: echo "Version: ${{ steps.checks.outputs.output_3 }}"

Example 4: Complex Logic

- uses: somaz94/ternary-operator@v1
  id: deploy
  with:
    conditions: 'SERVICE IN game,api && ENVIRONMENT == prod || BRANCH == hotfix'
    true_values: 'deploy'
    false_values: 'skip'
    debug_mode: true

→ See more examples


Local Testing

Test your changes before pushing to GitHub:

# Run unit tests with pytest (135 tests with coverage)
make test

# Run integration tests (42 test cases)
make test-local

# Run all tests
make test-all

Or without Makefile:

# Unit tests (pytest)
python3 -m pytest tests/ -v --ignore=tests/test_local.py

# Integration tests
python3 tests/test_local.py

# Bash tests
./tests/test_local.sh

Test Coverage:

  • Unit tests: 135 tests (operators, parser, evaluator, colors)
  • Integration tests: 42 test cases (end-to-end subprocess tests)
  • Bash tests: 17 core tests

→ See testing guide


Inputs & Outputs


Inputs

Input Required Description Example
conditions Yes Comma-separated conditions (max 10) SERVICE IN game,batch
true_values Yes Values when conditions are true deploy,skip
false_values Yes Values when conditions are false skip,deploy
default_values No Fallback values on evaluation error fallback1,fallback2
case_sensitive No Case-sensitive comparison (default: true) false
debug_mode No Enable debug logging (default: false) true

Outputs

  • output_1 through output_10 - Results of evaluated conditions
  • result - JSON object containing all outputs (e.g. {"output_1": "value1", "output_2": "value2"})

→ See complete API reference


Tips & Best Practices

  1. Use IN operator for multiple value checks:

    # Good
    SERVICE IN game,batch,api
    
    # Avoid
    SERVICE == game || SERVICE == batch || SERVICE == api
  2. Keep conditions simple for readability:

    # Good - split into multiple conditions
    conditions: 'SERVICE == game, ENVIRONMENT == prod'
    
    # Harder to read
    conditions: 'SERVICE == game && ENV == prod && BRANCH == main && VERSION >= 1.0'
  3. Enable debug mode when troubleshooting:

    debug_mode: true  # Shows detailed evaluation process
  4. Test locally before pushing:

    make test-all

→ See more best practices


Integration

Works great with:

  • env-output-setter - Set environment variables and outputs
  • GitHub Environments - Environment-specific deployments
  • Matrix Strategies - Conditional logic per matrix combination
# Example with env-output-setter
- name: Set Variables
  uses: somaz94/env-output-setter@v1
  with:
    env_key: 'SERVICE,ENVIRONMENT'
    env_value: 'game,prod'

- name: Evaluate
  uses: somaz94/ternary-operator@v1
  with:
    conditions: 'SERVICE == game && ENVIRONMENT == prod'

→ See integration examples


Troubleshooting

Common Issues:

Condition not evaluating as expected?
  1. Check variable is set: echo "$VARIABLE" >> $GITHUB_ENV
  2. Enable debug mode: debug_mode: true
  3. Check for case sensitivity: gameGame
  4. Verify operator syntax: See operators guide
Array length mismatch error?

Ensure same number of:

  • Conditions
  • True values
  • False values
# Correct - all have 2 items
conditions: 'A == 1, B == 2'
true_values: 'yes,ok'
false_values: 'no,fail'
Maximum conditions exceeded?

Split into multiple action calls or combine with IN operator:

# Instead of 12 individual conditions
SERVICE == game, SERVICE == batch, ...

# Use IN operator
SERVICE IN game,batch,...

→ See full troubleshooting guide


Contributing

Contributions welcome! Please:

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/my-feature
  3. Make your changes
  4. Test locally: make test-all
  5. Commit: git commit -am "feat: Add new feature"
  6. Push and create a Pull Request

→ See development guide


License

This project is licensed under the MIT License - see the LICENSE file for details.


Support


Made for better GitHub Actions workflows

Documentation | Examples | Contributing