apiVersion: tekton.dev/v1alpha1
kind: Condition
metadata:
  name: file-exists
spec:
  params:
    - name: "path"
  resources:
    - name: workspace
      type: git
  check:
    image: alpine
    script: 'test -f $(resources.workspace.path)/$(params.path)'
---
apiVersion: tekton.dev/v1alpha1
kind: PipelineResource
metadata:
  name: pipeline-git
spec:
  type: git
  params:
    - name: revision
      value: main
    - name: url
      value: https://github.com/tektoncd/pipeline
---
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: create-readme-file
spec:
  outputs:
    resources:
      - name: workspace
        type: git
  steps:
    - name: write-new-stuff
      image: public.ecr.aws/ubuntu/ubuntu:latest
      script: 'touch $(outputs.resources.workspace.path)/README.md'
---
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: echo-hello
spec:
  steps:
    - name: echo
      image: public.ecr.aws/ubuntu/ubuntu:latest
      script: 'echo hello'
---
apiVersion: tekton.dev/v1alpha1
kind: Pipeline
metadata:
  name: conditional-pipeline
spec:
  resources:
    - name: source-repo
      type: git
  params:
    - name: "path"
      default: "README.md"
  tasks:
    - name: first-create-file
      taskRef:
        name: create-readme-file
      resources:
        outputs:
          - name: workspace
            resource: source-repo
    - name: then-check
      conditions:
        - conditionRef: "file-exists"
          params:
            - name: "path"
              value: "$(params.path)"
          resources:
            - name: workspace
              resource: source-repo
              from: [first-create-file]
      taskRef:
        name: echo-hello
---
apiVersion: tekton.dev/v1alpha1
kind: PipelineRun
metadata:
  name: conditional-pr
spec:
  pipelineRef:
    name: conditional-pipeline
  serviceAccountName: 'default'
  resources:
    - name: source-repo
      resourceRef:
        name: pipeline-git
