apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  name: build
spec:
  timeouts:
    pipeline: "2h0m0s"
    tasks: "1h0m0s"
    finally: "0h30m0s"
  taskRunTemplate:
    serviceAccountName: build-bot
  pipelineSpec:
    params:
      - name: image-name
        description: The name for the built image
        type: string
      - name: path-to-image-context
        description: The path to the build context
        type: string
      - name: path-to-dockerfile
        description: The path to the Dockerfile
        type: string
    tasks:
      - name: detect-tag
        taskSpec:
          metadata: {}
          stepTemplate:
            image: alpine:3.20
            computeResources:
              requests:
                cpu: 10m
                memory: 600Mi
            workingDir: "$(workspaces.repo.path)"
          results:
            - name: tag
              description: The tag to use for the docker container.
          steps:
            - image: alpine/git:v2.34.2
              name: detect-tag-step
              script: |
                #!/usr/bin/env sh
                set -euo pipefail
                git fetch --tags
                current_tag=$(git tag --points-at HEAD --list 'v*.*.*')
                if [ -z "$current_tag" ]; then
                  echo "No tag at current commit"
                  exit 1
                else
                  echo -n "${current_tag}" | tee $(results.tag.path)
                fi
        workspaces:
          - name: repo
            workspace: git-source
        runAfter:
          - fetch-repository
      - name: report-pending
        taskRef:
          resolver: git
          params:
            - name: url
              value: https://github.com/tektoncd/catalog.git
            - name: revision
              value: df36b3853a5657fd883015cdbf07ad6466918acf
            - name: pathInRepo
              value: task/gitea-set-status/0.1/gitea-set-status.yaml
        runAfter:
          - fetch-repository
        params:
          - name: CONTEXT
            value: "$(params.JOB_NAME)"
          - name: REPO_FULL_NAME
            value: "$(params.REPO_OWNER)/$(params.REPO_NAME)"
          - name: GITEA_HOST_URL
            value: code.fizz.buzz
          - name: SHA
            value: "$(tasks.fetch-repository.results.commit)"
          - name: DESCRIPTION
            value: "Build $(params.JOB_NAME) has started"
          - name: STATE
            value: pending
          - name: TARGET_URL
            value: "https://tekton.fizz.buzz/#/namespaces/$(context.pipelineRun.namespace)/pipelineruns/$(context.pipelineRun.name)"
      - name: fetch-repository
        taskRef:
          resolver: git
          params:
            - name: url
              value: https://github.com/tektoncd/catalog.git
            - name: revision
              value: df36b3853a5657fd883015cdbf07ad6466918acf
            - name: pathInRepo
              value: task/git-clone/0.9/git-clone.yaml
        workspaces:
          - name: output
            workspace: git-source
        params:
          - name: url
            value: $(params.REPO_URL)
          - name: revision
            value: $(params.PULL_BASE_SHA)
          - name: deleteExisting
            value: "true"
      - name: build-image
        taskRef:
          resolver: git
          params:
            - name: url
              value: https://github.com/tektoncd/catalog.git
            - name: revision
              value: df36b3853a5657fd883015cdbf07ad6466918acf
            - name: pathInRepo
              value: task/kaniko/0.6//kaniko.yaml
        params:
          - name: IMAGE
            value: "$(params.image-name):$(tasks.detect-tag.results.tag)"
          - name: CONTEXT
            value: $(params.path-to-image-context)
          - name: DOCKERFILE
            value: $(params.path-to-dockerfile)
          - name: BUILDER_IMAGE
            value: "gcr.io/kaniko-project/executor:v1.12.1"
          - name: EXTRA_ARGS
            value:
              - "--destination=$(params.image-name)" # Also write the :latest image
              - --cache=true
              - --cache-copy-layers
              - --cache-repo=harbor.fizz.buzz/kanikocache/cache
              - --use-new-run # Should result in a speed-up
              - --reproducible # To remove timestamps so layer caching works.
              - --snapshot-mode=redo
              - --skip-unused-stages=true
              - --registry-mirror=dockerhub.dockerhub.svc.cluster.local
        workspaces:
          - name: source
            workspace: git-source
          - name: dockerconfig
            workspace: docker-credentials
        runAfter:
          - detect-tag
    finally:
      - name: report-success
        when:
          - input: "$(tasks.status)"
            operator: in
            values: ["Succeeded", "Completed"]
        taskRef:
          resolver: git
          params:
            - name: url
              value: https://github.com/tektoncd/catalog.git
            - name: revision
              value: df36b3853a5657fd883015cdbf07ad6466918acf
            - name: pathInRepo
              value: task/gitea-set-status/0.1/gitea-set-status.yaml
        params:
          - name: CONTEXT
            value: "$(params.JOB_NAME)"
          - name: REPO_FULL_NAME
            value: "$(params.REPO_OWNER)/$(params.REPO_NAME)"
          - name: GITEA_HOST_URL
            value: code.fizz.buzz
          - name: SHA
            value: "$(tasks.fetch-repository.results.commit)"
          - name: DESCRIPTION
            value: "Build $(params.JOB_NAME) has succeeded"
          - name: STATE
            value: success
          - name: TARGET_URL
            value: "https://tekton.fizz.buzz/#/namespaces/$(context.pipelineRun.namespace)/pipelineruns/$(context.pipelineRun.name)"
      - name: report-failure
        when:
          - input: "$(tasks.status)"
            operator: in
            values: ["Failed"]
        taskRef:
          resolver: git
          params:
            - name: url
              value: https://github.com/tektoncd/catalog.git
            - name: revision
              value: df36b3853a5657fd883015cdbf07ad6466918acf
            - name: pathInRepo
              value: task/gitea-set-status/0.1/gitea-set-status.yaml
        params:
          - name: CONTEXT
            value: "$(params.JOB_NAME)"
          - name: REPO_FULL_NAME
            value: "$(params.REPO_OWNER)/$(params.REPO_NAME)"
          - name: GITEA_HOST_URL
            value: code.fizz.buzz
          - name: SHA
            value: "$(tasks.fetch-repository.results.commit)"
          - name: DESCRIPTION
            value: "Build $(params.JOB_NAME) has failed"
          - name: STATE
            value: failure
          - name: TARGET_URL
            value: "https://tekton.fizz.buzz/#/namespaces/$(context.pipelineRun.namespace)/pipelineruns/$(context.pipelineRun.name)"
    workspaces:
      - name: git-source
      - name: docker-credentials
  workspaces:
    - name: git-source
      volumeClaimTemplate:
        spec:
          storageClassName: "nfs-client"
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 10Gi
      subPath: rust-source
    - name: docker-credentials
      secret:
        secretName: harbor-plain
  params:
    - name: image-name
      value: "harbor.fizz.buzz/private/webhook-bridge"
    - name: path-to-image-context
      value: .
    - name: path-to-dockerfile
      value: docker/webhook_bridge/Dockerfile