<p>I want to execute a task in Argo workflow if a string starts with a particular substring.
For example, my string is <code>tests/dev-or.yaml</code> and I want to execute task if my string starts with <code>tasks/</code></p>
<p>Here is my workflow but the condition is not being validated properly</p>
<pre><code>apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: conditional-
spec:
  entrypoint: conditional-example
  arguments:
    parameters:
    - name: should-print
      value: &quot;tests/dev-or.yaml&quot;

  templates:
  - name: conditional-example
    inputs:
      parameters:
      - name: should-print
    steps:
    - - name: print-hello
        template: whalesay
        when: &quot;{{inputs.parameters.should-print }} startsWith 'tests/'&quot;

  - name: whalesay
    container:
      image: docker/whalesay:latest
      command: [sh, -c]
      args: [&quot;cowsay hello&quot;]
</code></pre>
<p>Below is the error it is giving when I run the workflow</p>
<pre><code>WorkflowFailed 7s workflow-controller  Invalid 'when' expression 'tests/dev-or.yaml startsWith 'tests/'': Unable to access unexported field 'yaml' in token 'or.yaml'
</code></pre>
<p>Seems it is not accepting <code>-</code>, <code>.yaml</code> and <code>/</code> while evaluating the when condition.</p>
<p>Any mistake am making in my workflow? What's the right way to use this condition?</p>

<p>tl;dr - use this: <code>when: &quot;'{{inputs.parameters.should-print}}' =~ '^tests/'&quot;</code></p>
<p>Parameter substitution happens before the <code>when</code> expression is evaluated. So the when expression is actually <code>tests/dev-or.yaml startsWith 'tests/'</code>. As you can see, the first string needs quotation marks.</p>
<p>But even if you had <code>when: &quot;'{{inputs.parameters.should-print}}' startsWith 'tests/'&quot;</code> (single quotes added), the expression would fail with this error: <code>Cannot transition token types from STRING [tests/dev-or.yaml] to VARIABLE [startsWith]</code>.</p>
<p>Argo Workflows <a href="https://github.com/argoproj/argo-workflows/tree/master/examples#conditionals" rel="nofollow noreferrer">conditionals</a> are evaluated as <a href="https://github.com/Knetic/govaluate" rel="nofollow noreferrer">govaluate</a> expressions. govaluate <a href="https://github.com/Knetic/govaluate/blob/master/MANUAL.md#built-in-functions" rel="nofollow noreferrer">does not have any built-in functions</a>, and Argo Workflows does not augment it with any functions. So <code>startsWith</code> is not defined.</p>
<p>Instead, you should use govaluate's <a href="https://github.com/Knetic/govaluate/blob/master/MANUAL.md#regex-comparators--" rel="nofollow noreferrer">regex comparator</a>. The expression will look like this: <code>when: &quot;'{{inputs.parameters.should-print}}' =~ '^tests/'&quot;</code>.</p>
<p>This is the functional Workflow:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: argoproj.io/v1alpha1
kind: Workflow
metadata:
  generateName: conditional-
spec:
  entrypoint: conditional-example
  arguments:
    parameters:
      - name: should-print
        value: &quot;tests/dev-or.yaml&quot;
  templates:
    - name: conditional-example
      inputs:
        parameters:
          - name: should-print
      steps:
        - - name: print-hello
            template: whalesay
            when: &quot;'{{inputs.parameters.should-print}}' =~ '^tests/'&quot;
    - name: whalesay
      container:
        image: docker/whalesay:latest
        command: [sh, -c]
        args: [&quot;cowsay hello&quot;]
</code></pre>
