Ruoyi DevOps 流水线方式搭建(KubeSphere)
2022-05-03 本文已影响0人
攻城老狮
3.1 创建 DevOps 工程
- 创建 DevOps 项目
- 创建流水线
3.2 构建后端项目-step1 拉取代码
- 添加凭证,并指定 GitHub 的项目 url 和分支
- 完成 step1 的流水线节点
3.2 构建后端项目-step2 项目构建
- 修改maven的镜像仓库,改为从阿里云镜像仓库拉取依赖
- 登陆 admin 账号,找到 maven 的配置文件
- 加入阿里云的镜像配置
<mirror>
<id>nexus-aliyun</id>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
image-20220502184608668.png
- 使用 maven 命令编译项目代码
3.3 构建后端项目-step3 构建镜像
- 执行 docker 的镜像构建命令,为每个微服务进行镜像打包
- 并行开展镜像构建
3.4 构建后端项目-step4 推送镜像
- 创建阿里云镜像仓库的访问凭证
- 修改 Jenkinsfile 的环境变量
- 推送镜像,需要指定阿里云凭证,登陆阿里云,执行打标签脚本以及执行推送脚本
- 其他微服务同样的方式,并行执行
3.5 构建后端项目-step5 部署镜像
- 创建 kubeconfig 的访问凭证,用于在 work 节点可以执行 kubectl 命令执行配置文件
- 在项目管理中配置阿里云镜像地址的访问凭证,用于向私有仓库拉取镜像使用
- 执行部署,通过命令指定运行部署的 deploy.yaml 文件
- 其他微服务同样的方式,并行执行
3.6 构建后端项目-step6 邮件通知
- 登陆 admin 账号,添加邮件配置
- 修改 DevOps,集成邮件功能。使用 admin 账号登陆,修改 devops-jenkins yaml文件
- 添加发送邮件的节点
3.7 构建前端项目
与后端类似
3.8 附录
- 后端 Jenkinsfile.yaml 文件
pipeline {
agent {
node {
label 'maven'
}
}
stages {
stage('代码克隆') {
agent none
steps {
container('maven') {
git(url: 'https://gitee.com/yaokuku123/RuoYi-Cloud.git', credentialsId: 'github-auth', branch: 'master', changelog: true, poll: false)
sh 'ls -l'
}
}
}
stage('项目编译') {
agent none
steps {
container('maven') {
sh 'mvn clean package -Dmaven.test.skip=true'
}
}
}
stage('default-2') {
parallel {
stage('构建镜像') {
agent none
steps {
container('maven') {
sh 'docker build -t ruoyi-auth:latest -f ruoyi-auth/Dockerfile ./ruoyi-auth/'
}
}
}
stage('构建镜像2') {
agent none
steps {
container('maven') {
sh 'docker build -t ruoyi-gateway:latest -f ruoyi-gateway/Dockerfile ./ruoyi-gateway/'
}
}
}
stage('构建镜像3') {
agent none
steps {
container('maven') {
sh 'docker build -t ruoyi-file:latest -f ruoyi-modules/ruoyi-file/Dockerfile ./ruoyi-modules/ruoyi-file/'
}
}
}
stage('构建镜像4') {
agent none
steps {
container('maven') {
sh 'docker build -t ruoyi-job:latest -f ruoyi-modules/ruoyi-job/Dockerfile ./ruoyi-modules/ruoyi-job/'
}
}
}
stage('构建镜像5') {
agent none
steps {
container('maven') {
sh 'docker build -t ruoyi-system:latest -f ruoyi-modules/ruoyi-system/Dockerfile ./ruoyi-modules/ruoyi-system/'
}
}
}
stage('构建镜像6') {
agent none
steps {
container('maven') {
sh 'docker build -t ruoyi-monitor:latest -f ruoyi-visual/ruoyi-monitor/Dockerfile ./ruoyi-visual/ruoyi-monitor/'
}
}
}
}
}
stage('default-3') {
parallel {
stage('镜像推送') {
agent none
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId : 'aliyun-auth' ,passwordVariable : 'DOCKER_PWD_VAR' ,usernameVariable : 'DOCKER_USER_VAR' ,)]) {
sh 'echo $DOCKER_PWD_VAR | docker login $REGISTRY --username=$DOCKER_USER_VAR --password-stdin'
sh 'docker tag ruoyi-auth:latest $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-auth:SNAPSHOT-$BUILD_NUMBER'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-auth:SNAPSHOT-$BUILD_NUMBER'
}
}
}
}
stage('镜像推送2') {
agent none
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId : 'aliyun-auth' ,passwordVariable : 'DOCKER_PWD_VAR' ,usernameVariable : 'DOCKER_USER_VAR' ,)]) {
sh 'echo $DOCKER_PWD_VAR | docker login $REGISTRY --username=$DOCKER_USER_VAR --password-stdin'
sh 'docker tag ruoyi-gateway:latest $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-gateway:SNAPSHOT-$BUILD_NUMBER'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-gateway:SNAPSHOT-$BUILD_NUMBER'
}
}
}
}
stage('镜像推送3') {
agent none
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId : 'aliyun-auth' ,passwordVariable : 'DOCKER_PWD_VAR' ,usernameVariable : 'DOCKER_USER_VAR' ,)]) {
sh 'echo $DOCKER_PWD_VAR | docker login $REGISTRY --username=$DOCKER_USER_VAR --password-stdin'
sh 'docker tag ruoyi-file:latest $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-file:SNAPSHOT-$BUILD_NUMBER'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-file:SNAPSHOT-$BUILD_NUMBER'
}
}
}
}
stage('镜像推送4') {
agent none
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId : 'aliyun-auth' ,passwordVariable : 'DOCKER_PWD_VAR' ,usernameVariable : 'DOCKER_USER_VAR' ,)]) {
sh 'echo $DOCKER_PWD_VAR | docker login $REGISTRY --username=$DOCKER_USER_VAR --password-stdin'
sh 'docker tag ruoyi-job:latest $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-job:SNAPSHOT-$BUILD_NUMBER'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-job:SNAPSHOT-$BUILD_NUMBER'
}
}
}
}
stage('镜像推送5') {
agent none
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId : 'aliyun-auth' ,passwordVariable : 'DOCKER_PWD_VAR' ,usernameVariable : 'DOCKER_USER_VAR' ,)]) {
sh 'echo $DOCKER_PWD_VAR | docker login $REGISTRY --username=$DOCKER_USER_VAR --password-stdin'
sh 'docker tag ruoyi-system:latest $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-system:SNAPSHOT-$BUILD_NUMBER'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-system:SNAPSHOT-$BUILD_NUMBER'
}
}
}
}
stage('镜像推送6') {
agent none
steps {
container('maven') {
withCredentials([usernamePassword(credentialsId : 'aliyun-auth' ,passwordVariable : 'DOCKER_PWD_VAR' ,usernameVariable : 'DOCKER_USER_VAR' ,)]) {
sh 'echo $DOCKER_PWD_VAR | docker login $REGISTRY --username=$DOCKER_USER_VAR --password-stdin'
sh 'docker tag ruoyi-monitor:latest $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-monitor:SNAPSHOT-$BUILD_NUMBER'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-monitor:SNAPSHOT-$BUILD_NUMBER'
}
}
}
}
}
}
stage('default-4') {
parallel {
stage('镜像部署') {
agent none
steps {
container('maven') {
withCredentials([kubeconfigFile(credentialsId : 'demo-kubeconfig' ,variable : 'KUBECONFIG' ,)]) {
sh 'envsubst < ruoyi-auth/deploy/deploy.yaml | kubectl apply -f -'
}
}
}
}
stage('镜像部署2') {
agent none
steps {
container('maven') {
withCredentials([kubeconfigFile(credentialsId : 'demo-kubeconfig' ,variable : 'KUBECONFIG' ,)]) {
sh 'envsubst < ruoyi-gateway/deploy/deploy.yaml | kubectl apply -f -'
}
}
}
}
stage('镜像部署3') {
agent none
steps {
container('maven') {
withCredentials([kubeconfigFile(credentialsId : 'demo-kubeconfig' ,variable : 'KUBECONFIG' ,)]) {
sh 'envsubst < ruoyi-modules/ruoyi-file/deploy/deploy.yaml | kubectl apply -f -'
}
}
}
}
stage('镜像部署4') {
agent none
steps {
container('maven') {
withCredentials([kubeconfigFile(credentialsId : 'demo-kubeconfig' ,variable : 'KUBECONFIG' ,)]) {
sh 'envsubst < ruoyi-modules/ruoyi-job/deploy/deploy.yaml | kubectl apply -f -'
}
}
}
}
stage('镜像部署5') {
agent none
steps {
container('maven') {
withCredentials([kubeconfigFile(credentialsId : 'demo-kubeconfig' ,variable : 'KUBECONFIG' ,)]) {
sh 'envsubst < ruoyi-modules/ruoyi-system/deploy/deploy.yaml | kubectl apply -f -'
}
}
}
}
stage('镜像部署6') {
agent none
steps {
container('maven') {
withCredentials([kubeconfigFile(credentialsId : 'demo-kubeconfig' ,variable : 'KUBECONFIG' ,)]) {
sh 'envsubst < ruoyi-visual/ruoyi-monitor/deploy/deploy.yaml | kubectl apply -f -'
}
}
}
}
}
}
stage('部署完成') {
agent none
steps {
mail(to: 'yorick_jun@163.com', subject: 'Deploy Success', body: 'KubeSphere Deploy Ruoyi $BUILD_NUMBER Success!!')
}
}
}
environment {
DOCKER_CREDENTIAL_ID = 'dockerhub-id'
GITHUB_CREDENTIAL_ID = 'github-id'
KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
REGISTRY = 'registry.cn-beijing.aliyuncs.com'
DOCKERHUB_NAMESPACE = 'yqj_ruoyi'
GITHUB_ACCOUNT = 'kubesphere'
APP_NAME = 'devops-java-sample'
}
parameters {
string(name: 'TAG_NAME', defaultValue: '', description: '')
}
}
- 后端 deploy.yaml 文件
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: ruoyi-auth
name: ruoyi-auth
namespace: ruoyi #一定要写名称空间
spec:
progressDeadlineSeconds: 600
replicas: 1
selector:
matchLabels:
app: ruoyi-auth
strategy:
rollingUpdate:
maxSurge: 50%
maxUnavailable: 50%
type: RollingUpdate
template:
metadata:
labels:
app: ruoyi-auth
spec:
imagePullSecrets:
- name: aliyun-docker-hub #提前在项目下配置访问阿里云的账号密码
containers:
- image: $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-auth:SNAPSHOT-$BUILD_NUMBER
imagePullPolicy: Always
name: app
ports:
- containerPort: 8080
protocol: TCP
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
labels:
app: ruoyi-auth
name: ruoyi-auth
namespace: ruoyi
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: 8080
selector:
app: ruoyi-auth
sessionAffinity: None
type: ClusterIP
- 前端 Jenkinsfile.yaml 文件
pipeline {
agent {
node {
label 'nodejs'
}
}
stages {
stage('拉取代码') {
steps {
container('nodejs') {
git(url: ' https://gitee.com/yaokuku123/RuoYi-Cloud.git', credentialsId: 'github-auth', branch: 'master', changelog: true, poll: false)
sh 'ls -l'
}
}
}
stage('项目编译') {
agent none
steps {
container('nodejs') {
sh 'cd ruoyi-ui && npm install --registry=https://registry.npm.taobao.org && npm run build:prod && ls -l && mkdir -p docker/html/dist && cp -r dist docker/html && ls -l docker/html/dist'
}
}
}
stage('构建镜像') {
agent none
steps {
container('nodejs') {
sh 'cd ruoyi-ui/docker && docker build -t ruoyi-ui:latest -f dockerfile .'
}
}
}
stage('推送镜像') {
agent none
steps {
container('nodejs') {
withCredentials([usernamePassword(credentialsId : 'aliyun-auth' ,passwordVariable : 'DOCKER_PWD_VAR' ,usernameVariable : 'DOCKER_USER_VAR' ,)]) {
sh 'echo $DOCKER_PWD_VAR | docker login $REGISTRY --username=$DOCKER_USER_VAR --password-stdin'
sh 'docker tag ruoyi-ui:latest $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-ui:SNAPSHOT-$BUILD_NUMBER'
sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-ui:SNAPSHOT-$BUILD_NUMBER'
}
}
}
}
stage('部署镜像') {
agent none
steps {
container('nodejs') {
withCredentials([kubeconfigFile(credentialsId : 'demo-kubeconfig' ,variable : 'KUBECONFIG' ,)]) {
sh 'envsubst < ruoyi-ui/deploy/deploy.yaml | kubectl apply -f -'
}
}
}
}
stage('部署完成') {
agent none
steps {
mail(to: 'yorick_jun@163.com', subject: 'Ruoyi Ui Success', body: "Ruoyi UI Success $BUILD_NUMBER")
}
}
}
environment {
DOCKER_CREDENTIAL_ID = 'dockerhub-id'
GITHUB_CREDENTIAL_ID = 'github-id'
KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
REGISTRY = 'registry.cn-beijing.aliyuncs.com'
DOCKERHUB_NAMESPACE = 'yqj_ruoyi'
GITHUB_ACCOUNT = 'kubesphere'
APP_NAME = 'devops-java-sample'
}
parameters {
string(name: 'TAG_NAME', defaultValue: '', description: '')
}
}
- 前端 deploy.yaml 文件
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: ruoyi-ui
name: ruoyi-ui
namespace: ruoyi #一定要写名称空间
spec:
progressDeadlineSeconds: 600
replicas: 1
selector:
matchLabels:
app: ruoyi-ui
strategy:
rollingUpdate:
maxSurge: 50%
maxUnavailable: 50%
type: RollingUpdate
template:
metadata:
labels:
app: ruoyi-ui
spec:
imagePullSecrets:
- name: aliyun-docker-hub #提前在项目下配置访问阿里云的账号密码
containers:
- image: $REGISTRY/$DOCKERHUB_NAMESPACE/ruoyi-ui:SNAPSHOT-$BUILD_NUMBER
imagePullPolicy: Always
name: app
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: 300m
memory: 600Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
dnsPolicy: ClusterFirst
restartPolicy: Always
terminationGracePeriodSeconds: 30
---
apiVersion: v1
kind: Service
metadata:
labels:
app: ruoyi-ui
name: ruoyi-ui
namespace: ruoyi
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
nodePort: 32248
selector:
app: ruoyi-ui
sessionAffinity: None
type: NodePort
- 前端 nginx 配置文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name _;
location / {
root /home/ruoyi/projects/ruoyi-ui;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /prod-api/{
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://ruoyi-gateway.ruoyi:8080/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}# requirepass 123456