Tekton-task入门上


tekton task入门

什么是task

A Task is a collection of Steps that you define and arrange in a specific order of execution as part of your continuous integration flow. A Task executes as a Pod on your Kubernetes cluster. A Task is available within a specific namespace, while a ClusterTask is available across the entire cluster.

任务是步骤的集合,作为持续集成流的一部分,您可以按照特定的执行顺序定义和安排这些步骤。Task作为一个Pod在Kubernetes集群上执行。Task在特定的名称空间中可用,而ClusterTask在整个集群中可用。

A Task declaration includes the following elements:

资源详解

steps

name

task/steps/task-name.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: hello
spec:
  steps:
    - name: hello
      image: ubuntu
      command:
        - echo
      args:
        - "Hello World!"

tkn task start -f task-name.yaml -n tekton

image

task/steps/task-name.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: hello
spec:
  steps:
    - name: hello
      image: ubuntu
      command:
        - echo
      args:
        - "Hello World!"

tkn task start -f task-name.yaml -n tekton

script

task/steps/task-script-shell.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: script
spec:
  steps:
  - image: ubuntu 
    script: |
      #!/usr/bin/env bash
      echo "Hello from Bash!"

task/steps/task-script-python.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: script
spec:
  steps:
  - image: python  # contains python
    script: |
      #!/usr/bin/env python3
      print("Hello from Python!")

task/steps/task-script-node.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: script
spec:
  steps:
  - image: node  # contains node
    script: |
      #!/usr/bin/env node
      console.log("Hello from Node!")

resources

task/steps/task-resources.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: resources
spec:
  steps:
  - name: step-with-limts
    image: ubuntu
    command:
    - echo
    args:
    - "Hello World!"
    resources:
      requests:
        memory: 100Mi
        cpu: 10m
      limits:
        memory: 100Mi
        cpu: 10m

timeout

task/steps/task-timeout.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: timeout
spec:  
  steps:
  - name: sleep-then-timeout
    image: ubuntu
    script: | 
      #!/usr/bin/env bash
      echo "I am supposed to sleep for 60 seconds!"
      sleep 60
    timeout: 5s

args

参数

task/steps/task-name.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: hello
spec:
  steps:
    - name: hello
      image: ubuntu
      command:
        - echo
      args:
        - "Hello World!"

command

命令

task/steps/task-name.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: hello
spec:
  steps:
    - name: hello
      image: ubuntu
      command:
        - echo
      args:
        - "Hello World!"

env

plain

task/steps/task-env.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: env
spec: 
  steps:
  - image: ubuntu
    command: [echo]
    args: ["FOO is $(FOO)"]
    env:
      - name: "FOO"
        value: "baz"

secret-env

secret.yaml

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  username: YWRtaW4=

task/steps/task-env-secret.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: env
spec:  
  steps:
  - image: ubuntu
    command: [echo]
    args: ["FOO is $(FOO)"]
    env:
      - name: "FOO"
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: username

volumeMounts

挂载

task/steps/task-volumeMounts.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: volumemounts
spec:   
  steps:
  - image: docker:20.10.5
    name: client
    script: |
        #!/usr/bin/env sh
        cat > Dockerfile << EOF
        FROM ubuntu
        RUN apt-get update
        ENTRYPOINT ["echo", "hello"]
        EOF
        docker build -t hello . && docker run hello
        docker images
    volumeMounts:
      - mountPath: /var/run/docker.sock
        name: docker-socket
  volumes:
    - name: docker-socket
      hostPath:
        path: /var/run/docker.sock
        type: Socket

workingDir

工作目录

task/steps/task-workingDir.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: workingdir
spec:  
  steps:
  - image: ubuntu
    command: [pwd]
    workingDir: /workspace/src/

securityContext

上下文

task/steps/task-securityContext.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: securitycontext
spec:  
  steps:
  - image: ubuntu
    command: [id]
    securityContext:
      runAsUser: 2000

imagePullPolicy

拉取镜像方式

task/steps/task-imagePullPolicy .yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: imagepullpolicy 
spec:  
  steps:
  - image: ubuntu
    command: [echo]
    args:
    - hello
    imagePullPolicy: IfNotPresent

workspaces

description

描述

task/task-description.yaml

apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: description 
spec: 
  description:  this is a test task
  steps:
  - image: ubuntu
    command: [echo]
    args:
    - hello
    imagePullPolicy: IfNotPresent

Author: 千里
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source 千里 !
  TOC