

环境说明创建DevOps工程填写流水线信息创建流水线部署应用所需脚本JenkinsfileDockerfile

脚本一些参数如何设置说明deploy.yaml中的:imagePullSecrets:name属性jenkinsfile中的kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID"jenkinsfile中的git-code-auth属性

环境说明

已经安装kubesphere的devops组件安装教程可参考官方文档:https://v3-1.docs.kubesphere.io/zh/docs/pluggable-components/devops/

创建DevOps工程

填写流水线信息

创建流水线

1 我们编写JenkinsFile

流水线的部署流程如下图

#mermaid-svg-r1M8LGjWqRmUvOEG {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-r1M8LGjWqRmUvOEG .error-icon{fill:#552222;}#mermaid-svg-r1M8LGjWqRmUvOEG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-r1M8LGjWqRmUvOEG .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-r1M8LGjWqRmUvOEG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-r1M8LGjWqRmUvOEG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-r1M8LGjWqRmUvOEG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-r1M8LGjWqRmUvOEG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-r1M8LGjWqRmUvOEG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-r1M8LGjWqRmUvOEG .marker.cross{stroke:#333333;}#mermaid-svg-r1M8LGjWqRmUvOEG svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-r1M8LGjWqRmUvOEG .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-r1M8LGjWqRmUvOEG .cluster-label text{fill:#333;}#mermaid-svg-r1M8LGjWqRmUvOEG .cluster-label span{color:#333;}#mermaid-svg-r1M8LGjWqRmUvOEG .label text,#mermaid-svg-r1M8LGjWqRmUvOEG span{fill:#333;color:#333;}#mermaid-svg-r1M8LGjWqRmUvOEG .node rect,#mermaid-svg-r1M8LGjWqRmUvOEG .node circle,#mermaid-svg-r1M8LGjWqRmUvOEG .node ellipse,#mermaid-svg-r1M8LGjWqRmUvOEG .node polygon,#mermaid-svg-r1M8LGjWqRmUvOEG .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-r1M8LGjWqRmUvOEG .node .label{text-align:center;}#mermaid-svg-r1M8LGjWqRmUvOEG .node.clickable{cursor:pointer;}#mermaid-svg-r1M8LGjWqRmUvOEG .arrowheadPath{fill:#333333;}#mermaid-svg-r1M8LGjWqRmUvOEG .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-r1M8LGjWqRmUvOEG .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-r1M8LGjWqRmUvOEG .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-r1M8LGjWqRmUvOEG .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-r1M8LGjWqRmUvOEG .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-r1M8LGjWqRmUvOEG .cluster text{fill:#333;}#mermaid-svg-r1M8LGjWqRmUvOEG .cluster span{color:#333;}#mermaid-svg-r1M8LGjWqRmUvOEG div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-r1M8LGjWqRmUvOEG :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}

拉取代码

编译代码

打包镜像

推送镜像

部署应用

部署应用所需脚本

Jenkinsfile

pipeline {

agent {

node {

label 'nodejs'

}

}

stages {

stage('拉取代码') {

agent none

steps {

git(url: '代码地址', credentialsId: 'git-code-auth', branch: 'pro', changelog: true, poll: false)

}

}

stage('构建代码') {

agent none

steps {

container('nodejs') {

sh '''ls

npm install --force

npm run build:k8sprod'''

}

}

}

stage('构建镜像') {

agent none

steps {

container('nodejs') {

sh 'ls'

sh 'docker build -t tingyuan-cloud-service-web-admin:latest .'

}

}

}

stage('推送镜像') {

agent none

steps {

container('nodejs') {

withCredentials([usernamePassword(credentialsId: 'aliyun-docker', passwordVariable: 'DOCKER_PASSWORD_VAR', usernameVariable: 'DOCKER_USER_VAR',)]) {

sh 'echo "$DOCKER_PASSWORD_VAR" | docker login $REGISTRY -u "$DOCKER_USER_VAR" --password-stdin'

sh 'docker tag admin:latest 镜像仓库地址:SNAPSHOT-$BUILD_NUMBER'

sh 'docker push 镜像仓库地址:SNAPSHOT-$BUILD_NUMBER'

}

}

}

}

stage('发布应用') {

agent none

steps {

kubernetesDeploy(configs: 'deploy/**', enableConfigSubstitution: true, kubeconfigId: "$KUBECONFIG_CREDENTIAL_ID")

}

}

}

environment {

DOCKER_CREDENTIAL_ID = 'dockerhub-id'

GITHUB_CREDENTIAL_ID = 'github-id'

KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'

REGISTRY = '镜像仓库地址'

GITHUB_ACCOUNT = 'kubesphere'

DOCKERHUB_NAMESPACE = '镜像仓库命名空间'

}

parameters {

string(name: 'TAG_NAME', defaultValue: '', description: '')

}

}

Dockerfile

FROM nginx

LABEL maintainer=hrd

COPY dist /usr/share/nginx/html/

EXPOSE 80

apiVersion: apps/v1

kind: Deployment

metadata:

labels:

app: 我用的是前端项目,可以自行决定叫什么

name: 我用的是前端项目,可以自行决定叫什么

namespace: ty #一定要写名称空间

spec:

progressDeadlineSeconds: 600

replicas: 1

selector:

matchLabels:

app: 我用的是前端项目,可以自行决定叫什么

strategy:

rollingUpdate:

maxSurge: 50%

maxUnavailable: 50%

type: RollingUpdate

template:

metadata:

labels:

app: 我用的是前端项目,可以自行决定叫什么

spec:

imagePullSecrets:

- name: aliyun-docker #提前在项目下配置访问阿里云的账号密码

containers:

- image: 镜像仓库地址,我用阿里云镜像仓库

imagePullPolicy: Always

name: app

ports:

- name: http-80

containerPort: 80

protocol: TCP

resources: {}

terminationMessagePath: /dev/termination-log

terminationMessagePolicy: File

dnsPolicy: ClusterFirst

restartPolicy: Always

terminationGracePeriodSeconds: 30

---

apiVersion: v1

kind: Service

metadata:

labels:

app: 我用的是前端项目,可以自行决定叫什么

name: 我用的是前端项目,可以自行决定叫什么

namespace: ty

spec:

ports:

- name: http

port: 80

protocol: TCP

targetPort: 80

selector:

app: 我用的是前端项目,可以自行决定叫什么

sessionAffinity: None

type: ClusterIP

脚本一些参数如何设置说明

deploy.yaml中的:imagePullSecrets:name属性

imagePullSecrets:

- name: aliyun-docker #提前在项目下配置访问阿里云的账号密码

选择刚才添加的,添加凭证设置变量

jenkinsfile中的kubeconfigId: “$KUBECONFIG_CREDENTIAL_ID”

jenkinsfile中的git-code-auth属性

好文推荐

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。