A GitHub Action for evaluating conditional expressions and setting dynamic outputs based on the results. Perfect for creating flexible, condition-driven workflows.
- 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
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- 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
- API Reference - Complete input/output specification
- Operators Guide - All supported operators with examples
- Usage Examples - Real-world patterns and scenarios
- Troubleshooting - Common issues and solutions
- Development Guide - Contributing and local testing
| 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
- uses: somaz94/ternary-operator@v1
id: check
with:
conditions: 'ENVIRONMENT == prod'
true_values: 'production-config'
false_values: 'development-config'- 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'- 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 }}"- 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: trueTest 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-allOr 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- Unit tests: 135 tests (operators, parser, evaluator, colors)
- Integration tests: 42 test cases (end-to-end subprocess tests)
- Bash tests: 17 core tests
| 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 |
output_1throughoutput_10- Results of evaluated conditionsresult- JSON object containing all outputs (e.g.{"output_1": "value1", "output_2": "value2"})
-
Use IN operator for multiple value checks:
# Good SERVICE IN game,batch,api # Avoid SERVICE == game || SERVICE == batch || SERVICE == api
-
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'
-
Enable debug mode when troubleshooting:
debug_mode: true # Shows detailed evaluation process
-
Test locally before pushing:
make test-all
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'Condition not evaluating as expected?
- Check variable is set:
echo "$VARIABLE" >> $GITHUB_ENV - Enable debug mode:
debug_mode: true - Check for case sensitivity:
game≠Game - 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
Contributions welcome! Please:
- Fork the repository
- Create a feature branch:
git checkout -b feature/my-feature - Make your changes
- Test locally:
make test-all - Commit:
git commit -am "feat: Add new feature" - Push and create a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Documentation: Full Documentation
Made for better GitHub Actions workflows