云实验室(22) - kubesphere的springboot流水线

2022-06-15 21:21:48 浏览数 (1)

1. 准备工作

1.1 开启流水线

见官方文档

https://kubesphere.com.cn/docs/devops-user-guide/how-to-use/create-a-pipeline-using-graphical-editing-panel/

1.2 仓库

docker仓库 : dockerhub / 私有镜像仓库 前端代码仓库 : git / gitlab / gitee ...

1.2.1 后端代码准备

k8s

deploy.yaml

代码语言:javascript复制
apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: $DEPLOY_NAMESPACE
  name: $APP_NAME
  labels:
    app: $APP_NAME
spec:
  replicas: 1
  selector:
    matchLabels:
      app: $APP_NAME
  template:
    metadata:
      labels:
        app: $APP_NAME
    spec:
      containers:
      - name: $APP_NAME
        image: $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$TAG
        ports:
        - containerPort: 80
          protocol: TCP
        imagePullPolicy: Always
      dnsPolicy: ClusterFirst
      restartPolicy: Always
---
apiVersion: v1
kind: Service
metadata:
  namespace: $DEPLOY_NAMESPACE
  name: $APP_NAME
spec:
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80
    protocol: TCP
  selector:
    app: $APP_NAME

Dockerfile

代码语言:javascript复制
#基础镜像,如果本地没有,会从远程仓库拉取。
FROM openjdk:8-jdk-alpine
#工作目录
WORKDIR /app/
EXPOSE 80
#拷贝本地文件到镜像中
COPY target/login-cube-web-1.0.0.jar .
#指定容器启动时要执行的命令,但如果存在CMD指令,CMD中的参数会被附加到ENTRYPOINT指令的后面
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","login-cube-web-1.0.0.jar","--spring.profiles.active=prod"]

2. 流水线

2.1 创建devops项目

2.2 创建流水线

注意,代码中不含jenkinsFile的话 ,可以先不填代码仓库这样可以在kubesphere中图形化编辑流水线;

2.3 编辑流水线

2.4 编辑环境变量

2.5 编辑jenkinsFile

样例:

代码语言:javascript复制
pipeline {
  agent {
    node {
      label 'maven'
    }

  }
  stages {
    stage('代码拉取') {
      agent none
      steps {
        git(url: '$GIT_URL', credentialsId: 'gitlab-infra-secret', changelog: true, poll: false, branch: 'main')
        sh 'echo 拉取成功:$GIT_URL $PROJECT_BRANCH'
      }
    }

    stage('编译') {
      agent none
      steps {
        container('maven') {
          sh 'mvn -Dmaven.test.skip=true clean package -gs settings.xml'
        }

      }
    }

    stage('构建和推送镜像') {
      agent none
      steps {
        container('maven') {
          sh 'echo 镜像名:$REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME'
          sh '''docker build --no-cache -f Dockerfile -t $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME .
docker build --no-cache -f Dockerfile -t $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$TAG .'''
          withCredentials([usernamePassword(credentialsId : 'aliyun-harbor-secret' ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
            sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
            sh '''docker push  $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME
docker push  $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:$TAG'''
          }

        }

      }
    }

    stage('保存制品') {
      agent none
      steps {
        archiveArtifacts 'target/*.jar'
      }
    }

    stage('部署') {
      agent none
      steps {
        sh 'echo 即将部署至:本地集群,$DEPLOY_NAMESPACE项目,$APP_NAME'
        container('maven') {
          withCredentials([kubeconfigFile(credentialsId : 'infra-kubeconfig' ,variable : 'KUBECONFIG' ,)]) {
            container('maven') {
              sh '''
                  envsubst < deploy/k8s/deploy.yaml | kubectl apply -f -
                  '''
            }

          }

        }

      }
    }

  }
}

0 人点赞