apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: duplicate-key-warning
spec:
  params:
      - name: path
        type: string
        description: The path of the file to be created
  workspaces:
    - name: source
      description: |
        This workspace is shared among all the pipeline tasks to read/write common resources
  tasks:
    - name: create-file # when expression using parameter, evaluates to true
      when:
        - input: "$(params.path)"
          operator: in
          values: ["README.md"]
      workspaces:
        - name: source
          workspace: source
      taskSpec:
        workspaces:
          - name: source
            description: The workspace to create the readme file in
        steps:
          - name: write-new-stuff
            image: ubuntu
            script: 'touch $(workspaces.source.path)/README.md'
    - name: status
      taskSpec:
        workspaces:
          - name: source
            description: The workspace to create the readme file in
        steps:
          - name: write-new-stuff
            image: ubuntu
    - name: check-file
      params:
        - name: path
          value: "$(params.path)"
      workspaces:
        - name: source
          workspace: source
      runAfter:
        - create-file
      taskSpec:
        params:
          - name: path
        workspaces:
          - name: source
            description: The workspace to check for the file
        results:
          - name: exists
            description: indicates whether the file exists or is missing
        steps:
          - name: check-file
            image: alpine
            script: |
              if test -f $(workspaces.source.path)/$(params.path); then
                printf yes | tee /tekton/results/exists
              else
                printf no | tee /tekton/results/exists
              fi
    - name: echo-file-exists # when expression using task result, evaluates to true
      when:
        - input: "$(tasks.check-file.results.exists)"
          operator: in
          values: ["yes"]
      taskSpec:
        steps:
          - name: echo
            image: ubuntu
            script: 'echo file exists'
    - name: task-should-be-skipped-1
      when:
        - input: "$(tasks.check-file.results.exists)" # when expression using task result, evaluates to false
          operator: in
          values: ["missing"]
      taskSpec:
        steps:
          - name: echo
            image: ubuntu
            script: exit 1
    - name: task-should-be-skipped-2 # when expression using parameter, evaluates to false
      when:
        - input: "$(params.path)"
          operator: notin
          values: ["README.md"]
      taskSpec:
        steps:
          - name: echo
            image: ubuntu
            script: exit 1
    - name: task-should-be-skipped-3 # task with when expression and run after
      runAfter:
        - echo-file-exists
      when:
        - input: "monday"
          operator: in
          values: ["friday"]
      taskSpec:
        steps:
          - name: echo
            image: ubuntu
            script: exit 1
  finally:
    - name: finally-task-should-be-skipped-1 # when expression using execution status, evaluates to false
      when:
        - input: "$(tasks.echo-file-exists.status)"
          operator: in
          values: ["Failure"]
      taskSpec:
        steps:
          - name: echo
            image: ubuntu
            script: exit 1
    - name: finally-task-should-be-skipped-2 # when expression using task result, evaluates to false
      when:
        - input: "$(tasks.check-file.results.exists)"
          operator: in
          values: ["missing"]
      taskSpec:
        steps:
          - name: echo
            image: ubuntu
            script: exit 1
    - name: finally-task-should-be-skipped-3 # when expression using parameter, evaluates to false
      when:
        - input: "$(params.path)"
          operator: notin
          values: ["README.md"]
      taskSpec:
        steps:
          - name: echo
            image: ubuntu
            script: exit 1
    - name: finally-task-should-be-skipped-4 # when expression using tasks execution status, evaluates to false
      when:
        - input: "$(tasks.status)"
          operator: in
          values: ["Failure"]
      taskSpec:
        steps:
          - name: echo
            image: ubuntu
            script: exit 1
    - name: finally-task-should-be-executed # when expression using execution status, tasks execution status, param, and results
      when:
        - input: "$(tasks.echo-file-exists.status)"
          operator: in
          values: ["Succeeded"]
        - input: "$(tasks.status)"
          operator: in
          values: ["Succeeded"]
        - input: "$(tasks.check-file.results.exists)"
          operator: in
          values: ["yes"]
        - input: "$(params.path)"
          operator: in
          values: ["README.md"]
      taskSpec:
        steps:
          - name: echo
            image: ubuntu
            script: 'echo finally done'
