04-s2i-auto-deploy

 

root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 01-task-git-clone.yaml 
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: git-clone
spec:
  description: Clone the code repository to the workspace. 
  params:
    - name: url
      type: string
      description: git url to clone
  workspaces:
    - name: source
      description: The git repo will be cloned onto the volume backing this workspace
  steps:
    - name: git-clone
      image: alpine/git:v2.32.0
      script: git clone -v $(params.url) $(workspaces.source.path)/source
root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 02-task-source-build.yaml 
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: build-to-package
spec:
  description: build application and package the files to image
  workspaces:
    - name: source
      description: The git repo that cloned onto the volume backing this workspace
  steps:
    - name: build
      image: maven:3.8-openjdk-11-slim
      workingDir: $(workspaces.source.path)/source
      volumeMounts:
        - name: m2
          mountPath: /root/.m2
      script: mvn clean install
  volumes:
    - name: m2
      persistentVolumeClaim:
        claimName: maven-cache
root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 03-generate-build-id.yaml 
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: generate-build-id
spec:
  params:
    - name: version
      description: The version of the application
      type: string
  results:
    - name: datetime
      description: The current date and time
    - name: buildId
      description: The build ID
  steps:
    - name: generate-datetime
      image: ikubernetes/admin-box:v1.2
      script: |
        #!/usr/bin/env bash
        datetime=`date +%Y%m%d-%H%M%S`
        echo -n ${datetime} | tee $(results.datetime.path)
    - name: generate-buildid
      image: ikubernetes/admin-box:v1.2
      script: |
        #!/usr/bin/env bash
        buildDatetime=`cat $(results.datetime.path)`
        buildId=$(params.version)-${buildDatetime}
        echo -n ${buildId} | tee $(results.buildId.path)
root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 04-task-build-image.yaml 
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: image-build-and-push
spec:
  description: package the application files to image
  params:
    - name: dockerfile
      description: The path to the dockerfile to build (relative to the context)
      default: Dockerfile
    - name: image-url
      description: Url of image repository
    - name: image-tag
      description: Tag to apply to the built image
  workspaces:
    - name: source
    - name: dockerconfig
      mountPath: /kaniko/.docker
  steps:
    - name: image-build-and-push
      image: gexuchuan123/kaniko-project-executor:debug
      securityContext:
        runAsUser: 0
      env:
        - name: DOCKER_CONFIG
          value: /kaniko/.docker
      command:
        - /kaniko/executor
      args:
        - --dockerfile=$(params.dockerfile)
        - --context=$(workspaces.source.path)/source
        - --destination=$(params.image-url):$(params.image-tag)
root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 05-task-deploy.yaml 
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
  name: deploy-using-kubectl
spec:
  workspaces:
    - name: source
      description: The git repo
  params:
    - name: deploy-config-file
      description: The path to the yaml file to deploy within the git source
    - name: image-url
      description: Image name including repository
    - name: image-tag
      description: Image tag
  steps:
    - name: update-yaml
      image: alpine:3.15
      command: ["sed"]
      args:
        - "-i"
        - "-e"
        - "s@__IMAGE__@$(params.image-url):$(params.image-tag)@g"
        - "$(workspaces.source.path)/source/deploy/$(params.deploy-config-file)"
    - name: run-kubectl
      image: lachlanevenson/k8s-kubectl
      command: ["kubectl"]
      args:
        - "apply"
        - "-f"
        - "$(workspaces.source.path)/source/deploy/$(params.deploy-config-file)"
root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 06-pipeline-source-to-image.yaml 
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
  name: source-to-image
spec:
  params:
    - name: git-url
    - name: pathToContext
      description: The path to the build context, used by Kaniko - within the workspace
      default: .
    - name: image-url
      description: Url of image repository
    - name: deploy-config-file
      description: The path to the yaml file to deploy within the git source
      default: all-in-one.yaml
    - name: version
      description: The version of the application
      type: string
      default: "v0.9" 
  results:
    - name: datetime
      description: The current date and time
    - name: buildId
      description: The build ID
  workspaces:
    - name: codebase
    - name: docker-config
  tasks:
    - name: git-clone
      taskRef:
        name: git-clone
      params:
        - name: url
          value: "$(params.git-url)"
      workspaces:
        - name: source
          workspace: codebase
    - name: build-to-package
      taskRef:
        name: build-to-package
      workspaces:
        - name: source
          workspace: codebase
      runAfter:
        - git-clone
    - name: generate-build-id
      taskRef:
        name: generate-build-id
      params:
        - name: version
          value: "$(params.version)"
      runAfter:
        - git-clone
    - name: image-build-and-push
      taskRef:
        name: image-build-and-push
      params:
        - name: image-url
          value: "$(params.image-url)"
        - name: image-tag
          value: "$(tasks.generate-build-id.results.buildId)"
      workspaces:
        - name: source
          workspace: codebase
        - name: dockerconfig
          workspace: docker-config
      runAfter:
        - generate-build-id
        - build-to-package
    - name: deploy-to-cluster
      taskRef:
        name: deploy-using-kubectl
      workspaces:
        - name: source
          workspace: codebase
      params:
        - name: deploy-config-file
          value: $(params.deploy-config-file)
        - name: image-url
          value: $(params.image-url)
        - name: image-tag
          value: "$(tasks.generate-build-id.results.buildId)"
      runAfter:
        - image-build-and-push
root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 07-rbac.yaml 
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: helloworld-admin
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: helloworld-admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: helloworld-admin
  namespace: default
root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# cat 08-pipelinerun-source-to-image.yaml 
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: s2i-buildid-run-00002
spec:
  serviceAccountNames:
    - taskName: deploy-to-cluster
      serviceAccountName: helloworld-admin
  pipelineRef:
    name: source-to-image
  params:
    - name: git-url
      value: https://gitee.com/mageedu/spring-boot-helloWorld.git
    - name: image-url
      value: gexuchuan123/spring-boot-helloworld
    - name: version
      value: v0.9.2
  workspaces:
    - name: codebase
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
          storageClassName: nfs-csi
    - name: docker-config
      secret:
        secretName: docker-config

 

验证

root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# kubectl get po -nhello
NAME                                      READY   STATUS    RESTARTS   AGE
spring-boot-helloworld-6c4f65bc79-hg4nb   1/1     Running   0          100s

 

root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# kubectl get svc -nhello
NAME                     TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
spring-boot-helloworld   NodePort   10.100.137.77   <none>        80:34162/TCP   2m31s
root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# curl 10.100.137.77
Hello Spring Boot 2.0!

 

root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy# curl 10.100.137.77/version
version 0.9.2

 版本变更 使用本地gitlab

New project > Import project Repo > Git repository URL ( https://gitee.com/mageedu/spring-boot-helloWorld.git )  Public

git clone http://192.168.80.251/root/spring-boot-helloWorld.git

cd spring-boot-helloWorld

vi src/main/java/com/neo/controller/HelloWorldController.java

vi pom.xml

version 0.9.3

git config --global user.name root
git config --global user.email mage@magedu.com
git add .
git status
git commit -m "v0.9.3"
git push origin
root magedu.com

 

cat /etc/hosts
192.168.80.251 code.gitlab.svc.cluster.local

 

root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy/09# cat 09-pipelinerun-source-to-image-local-repo.yaml 
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
  name: s2i-buildid-run-00004
spec:
  serviceAccountNames:
    - taskName: deploy-to-cluster
      serviceAccountName: helloworld-admin
  pipelineRef:
    name: source-to-image
  params:
    - name: git-url
      value: http://192.168.80.251/root/spring-boot-helloWorld.git  #http://code.gitlab.svc.cluster.local/root/spring-boot-helloWorld.git
    - name: image-url
      value: gexuchuan123/spring-boot-helloworld
    - name: version
      value: v0.9.3
  workspaces:
    - name: codebase
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
          storageClassName: nfs-csi
    - name: docker-config
      secret:
        secretName: docker-config
root@master01:~/tekton-and-argocd-in-practise/04-tekton-pipeline-in-practise/04-s2i-auto-deploy/09# kubectl get po -nhello
NAME                                     READY   STATUS    RESTARTS   AGE
spring-boot-helloworld-5d586f4d4-2bm9n   1/1     Running   0          83s

 

posted @ 2022-03-23 20:46  gg888666  阅读(42)  评论(0编辑  收藏  举报