Pipeline is skipping task when it shouldn't?

24 views Asked by At

I have an Openshift/Tekton pipeline. I have a task that outputs a true/false passed result if certain tests pass:

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: test-repo
spec:
  results:
    - name: passed
      type: string
  steps:
    - image: 'mcr.microsoft.com/powershell:latest'
      name: repo-passed
      script: |
        #!/usr/bin/env pwsh
        if (Run-Tests){
          Set-Content -Path $(results.passed.path) -Value $true -Force
        }

I have a second task that is supposed to run if the above task returns 'True', and a third task that outputs the passed result for debugging purposes:

apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: my-pipeline
spec:
  tasks:
    - name: test-repo-task
      taskRef:
        kind: Task
        name: test-repo
    - name: do-stuff
      runAfter:
        - test-repo-task
      taskRef:
        kind: Task
        name: other-task
      when:
        - input: 'True'
          operator: in
          values:
            - $(tasks.test-repo.results.passed)
    - name: show-task-result
      params:
        - name: PS_COMMANDS
          value: echo "$(tasks.test-repo-docs.results.passed)"
        - name: PATH_CONTEXT
          value: .
        - name: POWERSHELL_IMAGE
          value: mcr.microsoft.com/powershell:latest
      runAfter:
        - do-stuff
      taskRef:
        kind: ClusterTask
        name: powershell

The show-task-result outputs True, but the do-stuff task is still skipping every time. I've tried changing the when condition to all lower case ('true'), I've also tried changing the value I output in the PowerShell script from a boolean ($true) to string ('true') but there was no difference.

I also tried swapping the when condition input/values:

when:
  - input: $(tasks.test-repo.results.passed)
    operator: in
    values:
      - 'true'

What am I doing wrong here?

1

There are 1 answers

0
jeremywat On

Thanks to this note from the documentation, my result output was appending a newline, causing the when test to fail.

To fix this I used the -NoNewLine parameter of Set-Content (akin to the tr example in the docs: tr -d \n):

steps:
  - image: 'mcr.microsoft.com/powershell:latest'
    name: repo-passed
    script: |
      #!/usr/bin/env pwsh
      if (Run-Tests){
        Set-Content -Path $(results.passed.path) -Value $true -Force -NoNewLine
      }