- 已经安装kubesphere的devops组件
- 安装教程可参考官方文档:https://v3-1.docs.kubesphere.io/zh/docs/pluggable-components/devops/
1 我们编写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: '')
}
}
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
imagePullSecrets:
- name: aliyun-docker #提前在项目下配置访问阿里云的账号密码