FG
☁️ Cloud & DevOpsMicrosoft

Conditional operator or function for expression syntax

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

Describe the enhancement I'd like some kind of conditional operation added to expression syntax. This can be an actual ternary operator (`?` `:`) or a built-in function (e.g., `if(<condition>, <true-value>, <false-value>)`). Additional information There is a workaround: using shell scripts to evaluate the condition, setting step outputs, and having other steps reference those outputs. But it would be much cleaner to have some kind of ternary / if/then/else / conditional branching built in to the expression syntax.

Unverified for your environment

Select your OS to check compatibility.

2 Fixes

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Implement Conditional Operator in Expression Syntax

Medium Risk

The current expression syntax lacks a built-in mechanism for conditional operations, which leads to complex workarounds using shell scripts. This limitation restricts the ease of use and readability of CI/CD workflows, making it difficult to implement simple conditional logic directly within the expression syntax.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Define the Ternary Operator Syntax

    Introduce a new syntax for the ternary operator that allows users to evaluate conditions directly within expressions. The proposed syntax is `condition ? true_value : false_value`.

    yaml
    result = condition ? 'Value if true' : 'Value if false';
  2. 2

    Create Built-in If Function

    Develop a built-in function `if(condition, true_value, false_value)` that can be used in expressions. This function should evaluate the condition and return the appropriate value based on the evaluation.

    yaml
    result = if(condition, 'Value if true', 'Value if false');
  3. 3

    Update Documentation

    Revise the existing documentation to include examples and usage guidelines for the new conditional operator and function. This will help users understand how to implement these features effectively.

    markdown
    # Example usage:
    # Using ternary operator:
    result = condition ? 'True' : 'False';
    
    # Using if function:
    result = if(condition, 'True', 'False');
  4. 4

    Test New Features

    Create unit tests to validate the functionality of the new conditional operator and if function. Ensure that all edge cases are covered and that the implementation behaves as expected.

    python
    assert(if(true, 'Yes', 'No') == 'Yes');
    assert(if(false, 'Yes', 'No') == 'No');
  5. 5

    Deploy Changes

    Merge the changes into the main branch and deploy the updated version of the expression syntax to the production environment. Monitor for any issues post-deployment.

    bash
    git merge feature/conditional-operator
    # Deploy command

Validation

To confirm the fix worked, create a sample CI/CD workflow that utilizes the new conditional operator and if function. Verify that the workflow executes correctly and produces the expected outputs based on different conditions.

Sign in to verify this fix

1 low-confidence fix
Unverified Fix
New Fix – Awaiting Verification

Implement Conditional Expression Syntax in GitHub Actions

Medium Risk

Currently, GitHub Actions does not support a built-in conditional operator or function within its expression syntax, which limits the ability to perform inline conditional evaluations. This results in reliance on external scripts for conditional logic, complicating workflows and reducing clarity.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Define the Conditional Operator

    Introduce a ternary operator syntax (e.g., `condition ? true_value : false_value`) or a built-in function (e.g., `if(condition, true_value, false_value)`) in the expression language of GitHub Actions.

    yaml
    if(condition, true_value, false_value)
  2. 2

    Update Documentation

    Revise the GitHub Actions documentation to include examples and usage guidelines for the new conditional syntax. This will help users understand how to implement the feature effectively in their workflows.

    yaml
    # Example usage in a workflow
    steps:
      - name: Check Condition
        run: echo "${{ if(eq(steps.step_id.outputs.result, 'success'), 'Success', 'Failure') }}"
  3. 3

    Implement Backward Compatibility

    Ensure that the new conditional syntax does not break existing workflows. Maintain support for the current method of using shell scripts for condition evaluation while introducing the new feature.

    yaml
    # Existing method
    steps:
      - name: Set Output
        id: step_id
        run: |
          if [ condition ]; then
            echo "result=success" >> $GITHUB_ENV
          else
            echo "result=failure" >> $GITHUB_ENV
          fi
  4. 4

    Test the New Feature

    Create a suite of tests to validate the functionality of the new conditional operator or function. Ensure that it behaves as expected across various scenarios and integrates seamlessly with existing features.

    yaml
    # Test case example
    steps:
      - name: Validate Conditional Logic
        run: |
          echo "${{ if(eq(steps.step_id.outputs.result, 'success'), 'Success', 'Failure') }}"
  5. 5

    Deploy the Enhancement

    Roll out the new conditional syntax in a stable release of GitHub Actions, ensuring that users are notified of the new feature and its capabilities.

Validation

To confirm the fix worked, create a sample GitHub Actions workflow that utilizes the new conditional syntax and verify that it executes correctly without errors. Check the output to ensure the conditional logic is applied as expected.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

github-actionsci-cdrunner