pipeline + node +jenkins+kubernetes部署yarn前端项目
1、编写Dockerfile文件
# Set the base image
FROM node:16.10.0
# WORKDIR /usr/src/app/
WORKDIR /home/option
# Copy files
COPY ./ /home/option/
# Build arguments
LABEL branch=${BRANCH}
LABEL commit=${COMMIT}
LABEL date=${BUILD_DATE}
ARG ENV
# Set ENV variables
ENV COMMIT_BRANCH=${BRANCH}
ENV COMMIT_SHA=${COMMIT}
ENV BUILD_DATE=${DATE}
ENV INSTALLATION_TYPE=docker
ENV MY_ENV=${ENV}
# Install dependencies and compile
RUN yarn install --frozen-lockfile
RUN yarn
RUN yarn build:${MY_ENV}
# Expose port 15888 - note that docs port is 3000
EXPOSE 3000
# Set the default command to run when starting the container
CMD yarn run start
这个Dockerfile需要传环境变量参数
2、编写pipeline文件
// 参数构建
pipeline {
agent any
parameters {
gitParameter(name: 'BRANCH_TAG', type: 'PT_BRANCH_TAG', branchFilter: 'origin/(.*)', defaultValue: 'main', selectedValue: 'DEFAULT', sortMode: 'DESCENDING_SMART', description: '请选择需要部署的代码:')
choice(name: 'mode', choices: ['deploy','rollback'], description: '请选择发布或者回滚?')
choice(name: 'ENVMENT', choices: ['sit','exp','gray','prod'], description: '环境参数')
string(name: 'iname', defaultValue: 'option-front', description: '服务名称')
}
environment {
dest_path = "/var/jenkins_home/workspace/${JOB_NAME}"
job_path = "/data/docker-compose/jenkins/jenkins_home/workspace/${JOB_NAME}"
mod_path = "/data/docker-compose/jenkins/jenkins_home/mod"
}
stages {
stage('clean'){
steps {
cleanWs(
cleanWhenAborted: true,
cleanWhenFailure: true,
cleanWhenNotBuilt: true,
cleanWhenSuccess: true,
cleanWhenUnstable: true,
cleanupMatrixParent: true,
disableDeferredWipeout: true,
deleteDirs: true
)
}
}
stage('从 gitlab 中拉取代码') {
when {
environment name: 'mode',value: 'deploy'
}
steps {
deleteDir()
checkout([$class: 'GitSCM',
branches: [[name: "${params.BRANCH_TAG}"]],
gitTool: 'Default',
userRemoteConfigs: [[url: 'https://gitlab.yunson.com/test/option/option_front.git', credentialsId: 'gitlab-deploy',]]
])
}
}
stage('Node Install And Build docker image'){
steps{
script{
sh """
docker build --build-arg ENV=${ENVMENT} -t harbor.yunson.com/test/${iname}:$ENVMENT .
"""
}
}
}
stage('Push image to hub'){
steps{
script{
withCredentials([usernamePassword(credentialsId: 'harbor-secret-dev',, passwordVariable: 'password', usernameVariable: 'username')]) {
sh 'docker login -u ${username} -p ${password} harbor.yunson.com'
}
sh 'docker push harbor.yunson.com/test/${iname}:$ENVMENT'
}
}
}
stage('deploy Server'){
steps{
script{
sh """
curl -X PUT \
-H "content-type: application/json" \
-H "Cookie: KuboardUsername=admin; KuboardAccessKey=ccpyiaxei7i8.disiejnk4dg5pfjlobgmflkuefkufdwf" \
-d '{"kind":"deployments","namespace":"test","name":"${iname}","images":{"harbor.yunson.com/test/${iname}":"harbor.yunson.com/test/${iname}:${ENVMENT}"}}' \
"http://69.36.89.2:18085/kuboard-api/cluster/Test/kind/CICDApi/admin/resource/updateImageTag"
curl -X PUT \
-H "Content-Type: application/yaml" \
-H "Cookie: KuboardUsername=admin; KuboardAccessKey=ccpyiaxei7i8.disiejnk4dg5pfjlobgmflkuefkufdwf" \
-d '{"kind":"deployments","namespace":"test","name":"${iname}"}' \
"http://69.36.89.2:18085/kuboard-api/cluster/Test/kind/CICDApi/admin/resource/restartWorkload"
"""
}
}
}
}
}
构建jenkins项目
构建成功如下
3、编写部署pods的yaml文件
---
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
k8s.kuboard.cn/displayName: option-front
labels:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: option-front
name: option-front
namespace: test
resourceVersion: '38871139'
spec:
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
k8s.kuboard.cn/name: option-front
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
annotations:
kubectl.kubernetes.io/restartedAt: '2023-11-15T19:51:15+08:00'
creationTimestamp: null
labels:
k8s.kuboard.cn/name: option-front
pod-template-hash: 645b77b9c
spec:
containers:
- env:
- name: TZ
value: Asia/Shanghai
image: 'harbor.yunson.com/test/option-front:sit'
imagePullPolicy: Always
name: option-front
ports:
- containerPort: 3000
name: msag7
protocol: TCP
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
imagePullSecrets:
- name: acr-secret
- name: harbor-secret
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
annotations: {}
labels:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: option-front
name: option-front
namespace: biking
resourceVersion: '31418671'
spec:
internalTrafficPolicy: Cluster
ipFamilies:
- IPv4
ipFamilyPolicy: SingleStack
ports:
- name: ef5znm
port: 3000
protocol: TCP
targetPort: 3000
selector:
k8s.kuboard.cn/name: option-front
sessionAffinity: None
type: ClusterIP
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations: {}
labels:
k8s.kuboard.cn/layer: web
k8s.kuboard.cn/name: option-front
name: option-front
namespace: biking
resourceVersion: '38696977'
spec:
ingressClassName: biking-ingress
rules:
- host: option-test.cuiwjrpcvi.com
http:
paths:
- backend:
service:
name: option-front
port:
number: 3000
path: /
pathType: Prefix
- backend:
service:
name: hyperw-option
port:
number: 9024
path: /service-option-core
pathType: Prefix
- backend:
service:
name: hyperw-assets
port:
number: 9027
path: /hyperw-assets
pathType: Prefix
- backend:
service:
name: option-index
port:
number: 9029
path: /service-option-index
pathType: Prefix
- backend:
service:
name: option-ws
port:
number: 9090
path: /ws
pathType: Prefix
- backend:
service:
name: hyperw-system
port:
number: 9028
path: /hyperw-system
pathType: Prefix
- backend:
service:
name: legend-index
port:
number: 9026
path: /public/web/kline/history
pathType: Prefix
- backend:
service:
name: legend-index
port:
number: 9026
path: /public/web/timeline/history
pathType: Prefix
- backend:
service:
name: option-ws
port:
number: 9090
path: /service-option-ws/ws
pathType: Prefix
- backend:
service:
name: hyperw-user
port:
number: 9023
path: /hyperw-user
pathType: Prefix
- backend:
service:
name: hyperw-agent
port:
number: 9033
path: /hyperw-agent
pathType: Prefix
- backend:
service:
name: hyperw-binary-option
port:
number: 9044
path: /binary-option
pathType: Prefix
tls:
- hosts:
- test.yunson.com
secretName: yunson.com-ssl