Tekton 使用示例

发布时间: 更新时间: 总字数:526 阅读时间:2m 作者: IP属地: 分享 复制网址

Tekton 使用示例

示例

Task 和 TaskRun

https://tekton.dev/docs/pipelines/tasks/

  • hello-task.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: example-task-name
spec:
  params:
    - name: pathToDockerFile
      type: string
      description: The path to the dockerfile to build
      default: /workspace/workspace/Dockerfile
  steps:
    - image: alpine
      command: ["echo"]
      args: ["$(params.pathToDockerFile)"]
  • 创建 Task
root@k8s-master:~/tekton# kubectl apply -f hello-task.yaml
task.tekton.dev/example-task-name created
root@k8s-master:~/tekton# tkn task list
NAME                DESCRIPTION   AGE
example-task-name                 6 minutes ago
  • 结果
root@k8s-master:~/tekton# kubectl get pod
No resources found in default namespace.
root@k8s-master:~/tekton# kubectl get tasks.tekton.dev
NAME                AGE
example-task-name   19s
root@k8s-master:~/tekton# kubectl get taskruns.tekton.dev
No resources found in default namespace.
  • 使用命令行运行 Task,也可以使用 Dashboard 触发
$ tkn task start example-task-name --showlog
? Value for param `pathToDockerFile` of type `string`? (Default is `/workspace/workspace/Dockerfile`) /workspace/workspace/Dockerfile
TaskRun started: example-task-name-run-ltgjs
Waiting for logs to be available...
[unnamed-0] /workspace/workspace/Dockerfile

# 命令行指定参数
$ tkn task start example-task-name --showlog -p pathToDockerFile=/path
  • 运行 Task 后,生成 TaskRun
root@k8s-master:~# tkn taskrun list
NAME                          STARTED         DURATION   STATUS
example-task-name-run-ltgjs   4 seconds ago   ---        Running(Pending)
root@k8s-master:~/tekton# tkn taskrun cancel example-task-name-run-ltgjs
TaskRun cancelled: example-task-name-run-ltgjs

# 查看pod资源
$ kubectl get pod
# 查看日志
$ tkn taskrun logs example-task-name-run-kw5r8

root@k8s-master:~# kubectl describe taskruns.tekton.dev example-task-name-run-ltgjs
Name:         example-task-name-run-ltgjs
Namespace:    default
Labels:       app.kubernetes.io/managed-by=tekton-pipelines
              tekton.dev/task=example-task-name
Annotations:  pipeline.tekton.dev/release: 94055d9
API Version:  tekton.dev/v1beta1
Kind:         TaskRun
Metadata:
  Creation Timestamp:  2022-09-02T04:04:25Z
  Generate Name:       example-task-name-run-
  Generation:          1
  Managed Fields:
    API Version:  tekton.dev/v1beta1
    Fields Type:  FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .:
          f:kubectl.kubernetes.io/last-applied-configuration:
          f:pipeline.tekton.dev/release:
        f:labels:
          f:tekton.dev/task:
    Manager:      Go-http-client
    ...

Workspace 示例

https://tekton.dev/docs/pipelines/workspaces/

spec:
  workspaces:
  - name: ssh-credentials
    description: An .ssh directory with keys, known_host and config files used to clone the repo.
    optional: true
    mountPath: /data
  steps:
  - name: clone-repo
    workspaces:
    - name: ssh-credentials # This Step receives the sensitive workspace; the others do not.
    image: git  # alpine/git:v3.32.0
    script: # git clone ...
  - name: build-source
    image: third-party-source-builder:latest # This image doesn't get access to ssh-credentials.
  - name: lint-source
    image: third-party-source-linter:latest # This image doesn't get access to ssh-credentials.

说明:

  • TaskRun 在 Task Workspace 中适配的卷存储称为 存储卷源(volume source),支持类型如下:
    • persistentVolumeClaim
    • volumeClaimTemplate
    • emptyDir
    • configMap
    • secret

Pipeline 和 PipelineRun

https://tekton.dev/docs/pipelines/pipelines/

  • 示例
spec:
  workspaces:
    - name: pipeline-ws1 # The name of the workspace in the Pipeline
  tasks:
    - name: use-ws-from-pipeline
      taskRef:
        name: gen-code # gen-code expects a workspace with name "output"
      workspaces:
        - name: output
          workspace: pipeline-ws1
    - name: use-ws-again
      taskRef:
        name: commit # commit expects a workspace with name "src"
      runAfter:
        - use-ws-from-pipeline # important: use-ws-from-pipeline writes to the workspace first
      workspaces:
        - name: src
          workspace: pipeline-ws1

其他

  • 使用 nfs-csi-driver 搭建 PVC,为项目构建提供缓存
...
spce:
  workspace:
    - name: golang
      description: golang cache
  steps:
    - name: build
      image: golang
      workingDir: $(workspaces.source.path)/source
      VolumeMounts:
        - name: m2
          mountPath: /<gopath>/src
    ...
  volumes:
    - name: m2
      persistentVolumeClaim:
        claimName: golang-cache

参考

  1. https://tanzu.vmware.com/developer/samples/tekton-examples/
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数