
#installo task community "git-clone"
#kubectl apply -f https://api.hub.tekton.dev/v1/resource/tekton/task/git-clone/0.10/raw -n 
kubectl apply -f https://github.com/tektoncd/catalog/raw/main/task/git-clone/0.10/git-clone.yaml -n tekton-pipelines


cat <<EOF | kubectl -n tekton-pipelines apply -f  -
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: clone-read
spec:
  description: | 
    This pipeline clones a git repo, then echoes the README file to the stout.
  params:
  - name: repo-url
    type: string
    description: The git repo URL to clone from.
  - name: image
    type: string
    description: The name (reference) of the image to build.
  - name: dockerfile
    type: string
    description: The path to the Dockerfile to execute (default: ./Dockerfile)
  workspaces:
  - name: shared-data
    description: | 
      This workspace contains the cloned repo files, so they can be read by the
      next task.
  tasks:
  - name: fetch-source
    taskRef:
      name: git-clone
    workspaces:
    - name: output
      workspace: shared-data
    params:
    - name: url
      value: $(params.repo-url)
  - name: show-readme
    runAfter: ["fetch-source"]
    taskRef:
      name: show-readme
    workspaces:
    - name: source
      workspace: shared-data
  - name: docker-build
    runAfter: ["show-readme"]
    taskRef:
      name: docker-build
    workspaces:
    - name: output
      workspace: shared-data
    params:
    - name: image
      value: $(params.image)
	- name: dockerfile
      value: $(params.dockerfile)  
 ---
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: show-readme
spec:
  description: Read and display README file.
  workspaces:
  - name: source
  steps:
  - name: read
    image: alpine:latest
    script: | 
      #!/usr/bin/env sh
      cat $(workspaces.source.path)/README.md	 
EOF




cat <<EOF | kubectl -n tekton-pipelines create -f  -
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  generateName: clone-read-run-
spec:
  pipelineRef:
    name: clone-read
  podTemplate:
    securityContext:
      fsGroup: 65532
  workspaces:
  - name: shared-data
    volumeClaimTemplate:
      spec:
        accessModes:
        - ReadWriteOnce
		storageClassName: longhorn
        resources:
          requests:
            storage: 1Gi
  params:
  - name: repo-url
    value: https://gitea.pigreco66.it/pigreco/poc.git
  - name: image
    value: nginx
  - name: dockerfile
    value: ./container/nginx/dockerfile
	
EOF


#BUILD Container
#installo task community "kaniko"
#kubectl apply -f https://api.hub.tekton.dev/v1/resource/tekton/task/kaniko/0.7/raw

kubectl apply -f https://github.com/tektoncd/catalog/raw/main/task/kaniko/0.7/kaniko.yaml -n -n tekton-pipelines


cat <<EOF | kubectl -n tekton-pipelines apply -f  -
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: clone-build-push
spec:
  description: |
    This pipeline clones a git repo, builds a Docker image with Kaniko and
    pushes it to a registry
  params:
  - name: repo-url
    type: string
  - name: image-reference
    type: string
  - name: dockerfile
    type: string
  workspaces:
  - name: shared-data
  - name: docker-credentials
  tasks:
  - name: fetch-source
    taskRef:
      name: git-clone
    workspaces:
    - name: output
      workspace: shared-data
    params:
    - name: url
      value: $(params.repo-url)
  - name: build-push
    runAfter: ["fetch-source"]
    taskRef:
      name: kaniko
    workspaces:
    - name: source
      workspace: shared-data
    - name: dockerconfig
      workspace: docker-credentials
    params:
    - name: IMAGE
      value: $(params.image-reference)
    - name: DOCKERFILE
      value: $(params.dockerfile)
EOF

cat <<EOF | kubectl -n tekton-pipelines create -f  -
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  generateName: clone-build-push-run-
spec:
  pipelineRef:
    name: clone-build-push
  podTemplate:
    securityContext:
      fsGroup: 65532
  workspaces:
  - name: shared-data
    volumeClaimTemplate:
      spec:
        accessModes:
        - ReadWriteOnce
        storageClassName: longhorn
        resources:
          requests:
            storage: 1Gi
  - name: docker-credentials
    secret:
      secretName: harbor-push-secret
  params:
  - name: repo-url
    value: https://gitea.pigreco66.it/pigreco/poc.git
  - name: image-reference
    value: harbor.pigreco66.it/library/nginx:1.30
  - name: dockerfile
    value: ./container/nginx/dockerfile
EOF

#  Build, push, deploy

cat <<EOF | kubectl  apply -f  -
apiVersion: v1
kind: ServiceAccount
metadata:
  name: tekton-deployer
  namespace: tekton-pipelines
imagePullSecrets:
  - name: harbor-regcred  
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: tekton-deployer
  namespace: poc
rules:
- apiGroups: ["", "apps", "batch", "networking.k8s.io"]
  resources: ["*"]
  verbs: ["*"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: tekton-deployer
  namespace: poc
subjects:
- kind: ServiceAccount
  name: tekton-deployer
  namespace: tekton-pipelines
roleRef:
  kind: Role
  name: tekton-deployer
  apiGroup: rbac.authorization.k8s.io
EOF


cat <<EOF | kubectl  -n tekton-pipelines apply -f  -
apiVersion: tekton.dev/v1
kind: Task
metadata:
  name: kubectl-apply
  namespace: tekton-pipelines
spec:
  workspaces:
  - name: source
  params:
  - name: namespace
    type: string
  - name: manifest
    type: string
    default: infrastructure.yaml
  steps:
  - name: apply
    image: bitnami/kubectl:latest
    script: |
      set -e
      echo "Deploying $(params.manifest) to namespace $(params.namespace)"
      kubectl apply \
        -f $(workspaces.source.path)/$(params.manifest) \
        -n $(params.namespace)
EOF

cat <<EOF | kubectl  -n tekton-pipelines apply -f  -
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: clone-build-push
spec:
  description: |
    This pipeline clones a git repo, builds a Docker image with Kaniko and
    pushes it to a registry
  params:
  - name: repo-url
    type: string
  - name: image-reference
    type: string
  - name: dockerfile
    type: string
  - name: namespace
    type: string	
  workspaces:
  - name: shared-data
  - name: docker-credentials
  tasks:
  - name: fetch-source
    taskRef:
      name: git-clone
    workspaces:
    - name: output
      workspace: shared-data
    params:
    - name: url
      value: $(params.repo-url)
  - name: build-push
    runAfter: ["fetch-source"]
    taskRef:
      name: kaniko
    workspaces:
    - name: source
      workspace: shared-data
    - name: dockerconfig
      workspace: docker-credentials
    params:
    - name: IMAGE
      value: $(params.image-reference)
    - name: DOCKERFILE
      value: $(params.dockerfile)
  - name: deploy
    runAfter: [build-push]
    taskRef:
      name: kubectl-apply
    params:
    - name: namespace
      value: $(params.namespace)
    workspaces:
    - name: source
      workspace: shared-data	  
EOF	  
	  
	  
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  generateName: clone-build-push-run-
spec:
  serviceAccountName: tekton-deployer
  pipelineRef:
    name: deploy-infrastructure
  podTemplate:
    securityContext:
      fsGroup: 65532
  workspaces:
  - name: shared-data
    volumeClaimTemplate:
      spec:
        accessModes:
        - ReadWriteOnce
        storageClassName: longhorn
        resources:
          requests:
            storage: 1Gi
  - name: docker-credentials
    secret:
      secretName: harbor-push-secret
  params:
  - name: repo-url
    value: https://gitea.pigreco66.it/pigreco/poc.git
  - name: image-reference
    value: harbor.pigreco66.it/library/nginx:1.30
  - name: dockerfile
    value: ./container/nginx/dockerfile	  
  - name: namespace
    value: poc