Skip to content

Instantly share code, notes, and snippets.

@sub-mod
Last active October 17, 2019 14:15
Show Gist options
  • Select an option

  • Save sub-mod/45baf9a187bd64b48dcb738867239d17 to your computer and use it in GitHub Desktop.

Select an option

Save sub-mod/45baf9a187bd64b48dcb738867239d17 to your computer and use it in GitHub Desktop.
Tekton Notes

TektonCD

Slide from Google : http://files.informatandm.com/uploads/2019/5/Apr_18_Dan_Lorenc_The_Future_of_Cloud_Native_CI_CD.pdf
Blog IBM : https://developer.ibm.com/tutorials/knative-build-app-development-with-tekton/

Openshift Namespace

namespace: tekton-demo  
sa:  tekton-demo  

Catalog & Examples

https://github.com/tektoncd/catalog
https://github.com/openshift/pipelines-catalog
https://github.com/sub-mod/openshift-pipelines-examples
https://github.com/sub-mod/tf-tekton

Create Tasks

oc login -u developer -p developer
oc project tekton-demo
oc apply -f https://raw.githubusercontent.com/tektoncd/catalog/master/openshift-client/openshift-client-task.yaml
oc apply -f https://raw.githubusercontent.com/tektoncd/catalog/master/s2i/s2i.yaml
oc apply -f https://raw.githubusercontent.com/tektoncd/catalog/master/buildah/buildah.yaml
oc apply -f https://raw.githubusercontent.com/tektoncd/catalog/master/kaniko/kaniko.yaml
# oc get tasks
NAME               AGE
buildah            28h
kaniko             64s
openshift-client   29h
s2i                29h

Example: Task

apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: openshift-client
spec:
  inputs:
    params:
      - name: ARGS
        description: The OpenShift CLI arguments to run
        default: help
  steps:
    - name: oc
      image: quay.io/openshift-pipeline/openshift-cli:0.5.0
      command: ["/usr/local/bin/oc"]
      args:
        - "${inputs.params.ARGS}"

Example: TaskRun

apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
  name: whoami
spec:
  # Use service account with git and image repo credentials
  serviceAccount: tekton-demo
  taskRef:
    name: openshift-client
  inputs:
    params:
    - name: ARGS
      value: whoami

CI/CD

Source to Image Strategy

Use s2i Task to pass git resource and build application and Buildah push Image
Example: s2i-python3-build and push

oc create -f https://raw.githubusercontent.com/openshift/pipelines-catalog/master/s2i-python-3/s2i-python-3-task.yaml
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
  name: s2i-python3-taskrun
spec:
  # Use service account with git and image repo credentials
  serviceAccount: tekton-demo
  taskRef:
    name: s2i-python-3
  inputs:
    resources:
    - name: source
      resourceSpec:
        type: git
        params:
        - name: url
          value: https://github.com/sclorg/s2i-python-container.git
    params:
    - name: PATH_CONTEXT
      value: "examples/app-home-test-app/"
    - name: TLSVERIFY
      value: "false"
  outputs:
    resources:
    - name: image
      resourceSpec:
        type: image
        params:
        - name: url
          value: image-registry.openshift-image-registry.svc:5000/tekton-demo/s2i-py3-build:latest

DockerBuild Strategy

Use buildah Task to pass Dockerfile and Buildah bud an Image
https://github.com/sub-mod/tf-tekton/blob/master/pipeline.yml#L24-L38

Create resources

Use openshift-client Task to use oc client for creating resources

Configure Templates/Manifests

  1. https://mikefarah.github.io/yq/write/
yq w <yaml_file> <path> <new value>
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: Update-param
spec:
  inputs:
    resources:
      - name: source-repo
        type: git       
    params:
      - name: yamlFile
        description: The path of the yaml file to update
      - name: yamlParamPath
        description: A tree path for some param attribute in yaml file
      - name: yamlParamValue
        description: param attribute value       
  steps:
    - name: replace-image
      image: mikefarah/yq
      command: ["yq"]
      args:
        - "w"
        - "-i"
        - "/workspace/source-repo/${inputs.params.yamlFile}"
        - "${inputs.params.yamlParamPath}"
        - "${inputs.param.yamlParamValue}"
  1. sed
apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: Update-param
spec:
  inputs:
    resources:
      - name: source-repo
        type: git       
    params:
      - name: yamlFile
        description: The path of the yaml file to update
      - name: ParamKey
        description: param attribute key
      - name: yamlParamValue
        description: param attribute value
  steps:
    - name: update-yaml
      image: alpine
      command: ["sed"]
      args:
        - "-i"
        - "-e"
        - "s;<old-key>:<old-value>;${inputs.params.ParamKey}:${inputs.params.yamlParamValue};g"
        - "/workspace/source-repo/${inputs.params.yamlFile}"
    - name: run-oc
      image: quay.io/openshift-pipeline/openshift-cli:0.5.0
      command: ["/usr/local/bin/oc"]
      args:
        - "apply"
        - "-f"
        - "/workspace/source-repo/${inputs.params.yamlFile}"
  1. shell script update to multiple values
  steps:
    - name: update-yaml
      image: docker.io/submod/update-yaml
      command: ["/bin/update"]
      args:
        - "${inputs.params.yamlParamPath}=${inputs.param.yamlParamValue}"
        - "/workspace/source-repo/${inputs.params.yamlFile}"

Folder Locations

git clone happens in /workspace
every input resource name creates a folder in /workspace folder
    resources:
      inputs:
      - name: source
ex: ^ would create /workspace/source folder
every output resource name creates a folder in /workspace/output folder
      outputs:
      - name: image
ex: ^ would create /workspace/output/image folder     

custom built tasks

apiVersion: tekton.dev/v1alpha1
kind: Task
metadata:
  name: change-param-task
spec:
  inputs:
    resources:
      - name: source-repo
        type: git

    params:
      - name: yamlFile
        description: The yaml file to update

      - name: yamlPath
        description: A tree path for attribute in yaml file

      - name: yamlPathValue
        description: A tree path value for attribute in yaml file

  steps:
    - name: replace-param
      image: mikefarah/yq
      command: ["yq"]
      args:
        - "w"
        - "-i"
        - "/workspace/source-repo/${inputs.params.yamlFile}"
        - "${inputs.params.yamlPath}"
        - "${inputs.params.yamlPathValue}"

    - name: cat
      image: mikefarah/yq
      command: ["cat"]
      args:
        - "/workspace/source-repo/${inputs.params.yamlFile}"
apiVersion: tekton.dev/v1alpha1
kind: TaskRun
metadata:
  name: change-param-taskrun
spec:
  # Use service account with git and image repo credentials
  serviceAccount: tekton-demo
  # using the task from here https://github.com/openshift/pipelines-catalog/blob/master/s2i-python-3/s2i-python-3-task.yaml
  taskRef:
    name: change-param-task
  inputs:
    # git cloning is done into /workspace/source
    resources:
    - name: source-repo
      resourceSpec:
        type: git
        params:
        - name: url
          value: https://github.com/sub-mod/tf-mnist.git
    params:
    - name: yamlFile
      value: "job.yml"
    - name: yamlPath
      value: "spec.template.spec.containers[0].env[0].value"
    - name: yamlPathValue
      value: "0.964"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment