#

This paper introduces in detail how Jenkins interacts with Tekton Pipeline on Kubernetes through Tekton-client-plugin. Charge: Jenkins; Tekton;

For more information on how to implement CICD using Tekton Pipeline, see this article

The project used to build this article and all manifest YAML are available for download here.

TL; DR

Convention, let me summarize. The Tekton-client-plugin is still in its early stages, but its value is clear, especially for users who have started with Jenkins as a CICD implementation. When moving from Jenkins to cloud-native Tekton, the cost of user interface development can be saved and user habits can be changed as little as possible, relying on version management to control the pace of migration.

Tekton-client-plugin was released in version 1.0.0 on May 7 this year and is currently at 1.0.02. It is still in the early stage. I personally feel that it is not good enough to just open the way of interaction between Jenkins and Tekton.

For example, only the following parameters can be injected into PipelineRun, which is difficult to support complex process control.

  • BUILD_ID – the build id/number of the Jenkins job
  • JOB_NAME – the name of the jenkins job that triggered this pipeline
  • PULL_BASE_REF – name of the base branch
  • PULL_PULL_SHA – the commit sha of the pull request or branch
  • REPO_NAME – name of the repository
  • REPO_OWNER – owner of the repository
  • REPO_URL – the URL of the repository

Hopefully, custom parameters will be supported later, such as registering more project metadata information into the Pipeline.

It is worth mentioning that the Tekton-client-plugin provides support for THE Job DSL, which is used as FreeStyle Project later in this article.

pipeline {
  agent any
  stages {
    stage('Stage') {
      steps {
        checkout scm
        tektonCreateRaw(inputType: 'FILE', input: '.tekton/pipeline.yaml')
      }
    }
  }
}
Copy the code

precondition

The environment

  • Kubernetes: Minikube is recommended
  • Jenkins: I recommend installing it on Kubernetes
  • Tekton
  • Projects for construction

tool

  • kubectl
  • tektoncd-cli
  • Kubectx, kubens
  • helm

Install Jenkins on Kubernetes (Helm)

Jenkins here uses Helm to install on Kubernetes.

Initialize namespaces, persistent volumes, and ServiceAccount.

kubectl create namespace jenkins
kubens jenkins
#For persistent storage, the author changed the capacity to 5Ghttp https://raw.githubusercontent.com/jenkins-infra/jenkins.io/master/content/doc/tutorials/kubernetes/installing-jenkins-on -kubernetes/jenkins-volume.yaml --body > jenkins-volume.yaml#Create the PV
kubectl apply -f jenkins-volume.yaml
#Create a service accountkubectl apply -f https://raw.githubusercontent.com/jenkins-infra/jenkins.io/master/content/doc/tutorials/kubernetes/installing-jenkins-on -kubernetes/jenkins-sa.yamlCopy the code

Prepare the HELM environment and add Jenkins ChartRepo

#Installed homebrew helm
brew install helm
#Add Jenkins Chart repo
helm repo add jenkinsci https://charts.jenkins.io
helm repo update
Copy the code

Jenkins configuration Chart

  1. Download the official Values YAMl for modification:http https://raw.githubusercontent.com/jenkinsci/helm-charts/main/charts/jenkins/values.yaml > jenkins-values.yaml
  2. Modify theserviceTypeNodePortAnd increasenodePort: 32000. Used to access Jenkins from outside minikube
  3. Modify thestorageClassjenkins-pv. We used it when we created PVjenkins-pvAs aDynamic Volume Provisioning 的 storageClass
  4. Modify theserviceAccountPart,createSet tofalse(It has been created aboveserviceAccount) at the same timenameSpecifies the preceding SA namejenkins
  5. installPluginsUnder the increasingTekton - client: 1.0.2
  6. Modify theadminPasswordadmin. Specify the initial password (you can obtain the initial password from the instructions in the installation output if you do not specify it)
  7. Modify thepersistencesize5Gi(My minikube virtual machine is only 20Gi in size)

The modified file is here jenkins-values.yaml.

Perform the installation

chart=jenkinsci/jenkins
helm install jenkins -n jenkins -f jenkins-values.yaml $chart
Copy the code

Output result:

NAME: jenkins
LAST DEPLOYED: Sun Jun 20 22:05:53 2021
NAMESPACE: jenkins
STATUS: deployed
REVISION: 1
Copy the code

Echo $(minikube IP):$(kubectl get SVC Jenkins -o jsonPath =”{.spec.ports[0].nodeport}”);

Tekton installation

kubectl create ns tekton-pipelines
kubens tekton-pipelines
kubectl apply --filename https://storage.googleapis.com/tekton-releases/pipeline/latest/release.yaml
Copy the code

Install the CLI

brew install tektoncd-cli

RBAC

After the Tekton Pipeline installation is complete, you need to add the operation rights of the Tekon resource to ServiceAccount Jenkins created earlier.

//tekton-role.yaml
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: tekton-role
  namespace: tekton-pipelines
rules:
  - apiGroups:
      - ""
    resources:
      - pods
      - pods/log
    verbs:
      - get
      - list
      - watch
  - apiGroups:
      - tekton.dev
    resources:
      - tasks
      - taskruns
      - pipelines
      - pipelineruns
    verbs:
      - create
      - delete
      - deletecollection
      - get
      - list
      - patch
      - update
      - watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: tekton-role-binding
  namespace: tekton-pipelines
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: tekton-role
subjects:
  - kind: ServiceAccount
    name: jenkins
    namespace: jenkins
Copy the code

Jenkins interacts with Tekton

Much of the previous work was preparatory work, but we had already added the Tekton-client-plugin when Jenkins was installed.

Add a FreeStyle project named Tekton-client-sample.

SCM this is where the project repository addresses and branches are used to build.

Build module select Tekton: Create Resource (RAW)

The FILE type is selected here because I already put PipelineRun’s YAML into the repository.

Perform a build

Check the application

$ kubectl get pod | grep tekton-testtekton-test-75975dcc88-xkzb6 1/1 Running 0 3m13s tekton-test-c26lw-deploy-to-k8s-28trp-pod-w8tgc 0/1 Completed 0 3m18s tekton-test-c26lw-fetch-from-git-pv66g-pod-nm5qh 0/1 Completed 0 6m30s tekton-test-c26lw-source-to-image-k8mpg-pod-qh7g4  0/2 Completed 0 6m15s
$ curl $(minikube ip):$(kubectl get svc tekton-test -o jsonpath="{.spec.ports[0].nodePort}")/hi
hello world
Copy the code

reference

  • Jenkins on Kubernetes
  • Tekton Client Plugin Tutorial
  • Easily reuse Tekton and Jenkins X from Jenkins

The article is uniformly published in the public number cloud native refers to north