devops(前端)

news2024/7/6 19:15:36

1.前言

前端的打包流程和后端的流程是一样的,只是打包的环境和制作的镜像有所不同,前端需要使用nodejs环境打包,镜像也是使用nginx镜像,因为用的是k8s的pod运行镜像,还需要使用configmap挂载nginx的配置,一套流程还是gitlab+jenkins+harbor+k8s的架构

2.基础环境配置

我们这里就引用之前后端的环境配置,在额外部署一些环境即可

参考:devops(后端)_Apex Predator的博客-CSDN博客

jenkins主机配置

安装nodejs环境

参考:nodejs环境部署_Apex Predator的博客-CSDN博客

gitlab配置

创建前端项目仓库上传代码,我这边是创建了一个nodejs的项目仓库

 并且上传了前端的代码,需要做实验的可以到以下连接下载项目代码

项目代码:Release v4.9.2 · Meedu/frontend-v3 · GitHub

harbor配置 

创建项目镜像仓库

我这里是创建了一个名为nginx的仓库存放前端的镜像,基础镜像依然是存放在之前创建的base_image仓库中 

3.配置项目发布

jenkins配置

 创建前端pipeline项目

 

配置构建流程

这里我们就不配置参数化选择,在执行pipeline的时候让它自动生成,不过这样就会导致在第一次执行的时候会出现报错

 

 

 

 构建脚本如下

pipeline {          #该pipeline和后端发布的pipeline并没有太大区别,只是打包命令和项目的参数改变了
  agent any
  environment {        #配置环境变量参数
    registry = "harbor.apex.com"
    harbor_auth = "a1e2c627-dc62-4599-a035-8e98d74665ab"
    project = "nginx"
    app_name = "k8s-qd"
    namespace = "k8s-qd"
    k8s_auth = "k8s-kubeconfig"
  }
  parameters {         #配置手动执行时选取的Parameter
    gitParameter(branch: '', branchFilter: '.*', defaultValue: '', description: 'Branch for build and deploy', name: 'branch', quickFilterEnabled: false, selectedValue: 'NONE', sortMode: 'NONE', tagFilter: '*', type: 'PT_BRANCH')
    choice(choices: ['deploy','rollback'], description: '''deploy ---部署  rollback  ---回滚''', name: 'status')
  }
  stages {
    stage ('checkout code') {
      parallel {
        stage ('webhook tigger') {
          when {
            expression { params.status == 'deploy' && env.gitlabBranch != null }
          }
          steps {
            checkout([$class: 'GitSCM', branches: [[name: '${env.gitlabBranch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitlab_auth', url: 'git@10.1.60.114:gitlab-instance-c484dcfc/nodejs.git']]])
            sh "git branch"
            echo "Current branch: ${env.gitlabBranch}"
            script {
              commit_id = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
              tag = BUILD_TAG + '-' + commit_id
            }
          }
        }
        stage ('jenkins scm') {
          when {
            expression { params.status == 'deploy' && env.gitlabBranch == null }
          }
          steps {
            checkout([$class: 'GitSCM', branches: [[name: '${branch}']], extensions: [], userRemoteConfigs: [[credentialsId: 'gitlab_auth', url: 'git@10.1.60.114:gitlab-instance-c484dcfc/nodejs.git']]])
            sh "git branch"
            echo "Current branch: ${branch}"
            script {
              commit_id = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim()
              tag = BUILD_TAG + '-' + commit_id
            }
          }
        }
      }
    }
    stage ('build dist') {
      when {
        expression { params.status == 'deploy' }
      }
      steps {         #使用nodejs的打包命令编译前端代码
        sh """
           npm install --registry=https://registry.npm.taobao.org   #拉取依赖,并指定源
           npm run build        #编译构建
           ls dist          #编译后的前端静态文件都在dist目录下
        """
      }
    }
    stage ('docker image build and push') {
      when {
        expression { params.status == 'deploy' }
      }
      steps {
        withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
          sh """
              docker build -t ${registry}/${project}/${app_name}:${tag} .
              docker push ${registry}/${project}/${app_name}:${tag}
          """
        }
      }
    }
    stage ('k8s update image version') {
      parallel {
        stage ('to master') {
          when {
            expression { params.status == 'deploy' && (env.gitlabBranch == 'master' || params.branch == 'master') }
          }
          steps {
            withCredentials([file(credentialsId: "${k8s_auth}", variable: 'KUBECONFIG')]) {
              sh "kubectl --kubeconfig ${KUBECONFIG} set image deployment ${app_name} ${app_name}=${registry}/${project}/${app_name}:${tag} -n ${namespace} --record"
            }
          }
        }  
        stage ('to qd_apex') {
          when {
            expression { params.status == 'deploy' && (env.gitlabBranch == 'qd_apex' || params.branch == 'qd_apex')}
          }
          steps {
            withCredentials([file(credentialsId: "${k8s_auth}", variable: 'KUBECONFIG')]) {
              sh "kubectl --kubeconfig ${KUBECONFIG} set image deployment ${app_name} ${app_name}=${registry}/${project}/${app_name}:${tag} -n ${namespace} --record"
            }
          }
        }
      }
    }
    stage ('rollback version') {
      parallel {
        stage ('to  master') {
          when {
            expression { params.status == 'rollback' && params.branch == 'master' }
          }
          steps {
            withCredentials([file(credentialsId: "${k8s_auth}", variable: 'KUBECONFIG')]) {
              sh "kubectl --kubeconfig ${KUBECONFIG} rollout undo deployment ${app_name} -n ${namespace}" 
            }
          }
        }
        stage ('to  qd_apex') {
          when {
            expression { params.status == 'rollback' && params.branch == 'qd_apex' }
          }
          steps {
            withCredentials([file(credentialsId: "${k8s_auth}", variable: 'KUBECONFIG')]) {
              sh "kubectl --kubeconfig ${KUBECONFIG} rollout undo deployment ${app_name} -n ${namespace}" 
            }
          }
        }
      }    
    }
  }
}

 制作基础镜像

vi dockerfile

FROM alpine:latest   #使用alpine系统作为基础镜像
ENV TZ="Asia/Shanghai"    #配置变量
RUN sed -i 's/dl-cdn.alpinelinux.org/repo.huaweicloud.com/g' /etc/apk/repositories \
&& apk add --upgrade --no-cache nginx tzdata ttf-dejavu fontconfig \  #安装nginx和一些软件
&& cp /usr/share/zoneinfo/${TZ} /etc/localtime \
&& echo ${TZ} > /etc/timezone

使用dockefile生成镜像

docker build -t harbor.apex.com/base_image/nginx:latest .

推送镜像到harbor仓库

docker push harbor.apex.com/base_image/nginx:latest

gitlab配置

在创建的前端代码仓库中配置webhook

参考:gitlab配置webhook_Apex Predator的博客-CSDN博客

在项目中新建dockerfile文件用于制作每次前端发版的新版本镜像

 

dockerfile内容如下

FROM harbor.apex.com/base_image/nginx:latest  #从harbor仓库获取nginx基础镜像
RUN mkdir -p /opt/web    #新建静态文件目录
WORKDIR /opt/web     #设置该目录为工作目录,即相当于cd进入该目录下
COPY ./dist/ ./      #讲编译构建好的静态文件拷贝到该工作目录下
EXPOSE 80           #暴露端口为80
ENTRYPOINT /usr/sbin/nginx -g "daemon off;"   
#启动nginx服务,nginx默认是后台启动的,但是容器是必须要在前台运行服务,不然就视为没有服务在容器中运行,就会导致容器自动关闭,所以使用-g参数使nginx服务在前台运行

 k8s集群配置

在master节点上执行

新建命名空间

kubectl create namespace k8s-qd

拷贝nginx配置文件过来,也可以在我这个配置中修改,用于创建configmap服务

vi nginx.conf

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    log_format  log_json '{ "time_local": "$time_local", '
                           '"remote_addr": "$remote_addr", '
                           '"referer": "$http_referer", '
                           '"request": "$request", '
                           '"status": $status, '
                           '"bytes": $body_bytes_sent, '
                           '"agent": "$http_user_agent", '
                           '"x_forwarded": "$http_x_forwarded_for", '
                           '"up_addr": "$upstream_addr", '
                           '"up_host": "$upstream_http_host", '
                           '"up_resp_time": "$upstream_response_time", '
                           '"request_time": "$request_time" }';

    access_log  /var/log/nginx/access.log  log_json;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /opt/web/;    #配置为存放静态文件的目录

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
        
        location / {       #配置访问规则,访问到html页面,因为html中应用的文件路径都是写死的,所以只需要配置了root指定静态目录即可
          root  /opt/web/;
          index index.html;
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

根据以上的nginx配置创建configmap

kubectl create configmap  qd-nginx --from-file=/root/nginx.conf -n k8s-qd

kubectl describe configmap qd-nginx -n k8s-qd

可以看到configmap是挂载了nginx配置的 

创建secret,用于拉取harbor仓库镜像验证

kubectl create secret docker-registry qd-harbor-secret --namespace=k8s-qd --docker-server=harbor.apex.com --docker-username=admin --docker-password=Harbor12345

新建前端服务的yaml文件

vi k8s-qd.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: k8s-qd
  name: k8s-qd
  namespace: k8s-qd
spec:
  replicas: 4
  progressDeadlineSeconds: 600
  minReadySeconds: 10
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  selector:
    matchLabels:
      app: k8s-qd
  template:
    metadata:
      labels:
        app: k8s-qd
    spec:
      containers:
      - name: k8s-qd
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        readinessProbe:
          httpGet:
            path: /
            port: 80
            scheme: HTTP
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 2
          successThreshold: 1
          failureThreshold: 2
        livenessProbe:
          tcpSocket:
            port: 80
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 2
          failureThreshold: 2
        volumeMounts:       #配置挂载configmap
        - name: nginx-configmap
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
      volumes:       #引用configmap挂载
      - name: nginx-configmap
        configMap:
          name: qd-nginx     #填入刚刚配置的configmap名称
          items:
          - key: nginx.conf
            path: nginx.conf
      imagePullSecrets:      #使用配置的secret
      - name: qd-harbor-secret
      restartPolicy: Always

---
apiVersion: v1
kind: Service
metadata:
  name: k8s-qd
  namespace: k8s-qd
spec:
  selector:
    app: k8s-qd
  type: NodePort
  clusterIP:
  ports:
  - port: 80
    targetPort: 80
    nodePort: 30001
    protocol: TCP

启动该yaml服务

kubectl apply -f k8s-qd.yaml

4.发布展示

手动构建 

可以看到手动构建的时候,git parameter选框会自动去拉取gitlab项目中的所有分支,默认是没有选择的,需要手动选择,status是默认选择deploy为发布

 

 

也可以看到第一次构建是失败的,这是因为没有手动配置parameter的原因,在第一次执行时会根据pipeline脚本中的配置自动生成parameter

再次进入配置也可以看到,配置构建流程的时候是没有配置这些参数的,这些都配置在pipeline脚本中,第一次构建的时候会自动生成,但是相应的,第一次构建也会执行失败

自动化构建 

我在gitlab配置了过滤只有推分支合并master的时候才触发构建,现在来合并一下master触发构建

点击批准合并后查看jenkins是否触发构建

 

 可以看到通过自动触发的构建也是成功完成了发布的

也可以通过blue ocean功能更直观的看到构建流程,blue ocean需要安装插件

 

 

至此配置完成 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/828191.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

04|Oracle学习(外键约束)

备注:下文中出现的 add constraints应改为add constraint,不需要加s 1.外键约束介绍 1.1 什么是外键约束: 如果按下面的设计,直接在原表中添加“班级编号”、“班级名称”、“班主任”、“班级描述”这些列名,会出现…

# Windows 环境下载 Android 12源码

前言 Android 官网(该方式不适合 Windows 平台):https://source.android.com/source/downloading.html (备注自 2021 年 6 月 22 日起,安卓操作系统不再支持在 Windows 或 MacOS 上进行构建,如果要编译源码推荐先安装…

JavaScript Object所有方法

下面将会一一介绍上面的方法。 Object.create Object.create() 方法创建一个拥有指定原型和若干个指定属性的对象。 Object.create(proto [, propertiesObject ])proto 为新创建对象的原型对象,设置为null可创建没有原型的空对象。propertiesObject 包涵若干个属性…

Kubernetes系列-Ingress

1 Ingress 概述 Kubernetes 对外暴露服务(Service)主要有两种方式:NodePort,LoadBalance,此外 externalIps 也可以使各类 service 对外提供服务,但是当集群服务很多的时候,NodePort方式最大的缺…

静态资源导入探究

静态资源可以在哪里找呢?我们看看源码 从这个类进去 里面有个静态类 WebMvcAutoConfigurationAdapter 有个配置类,将这个类的对象创建并导入IOC容器里 这个静态类下有个方法 addResourceHandlers(ResourceHandlerRegistry registry)静态资源处理器 若自…

【测试联调】如何在前后端测试联调时优雅的构造异常场景

目录 背景 使用iptables实现 利用iptables丢弃某ip数据包 使用 -L 列出所有规则 IP 连通性 通信 测试 插入一条规则,丢弃此ip 的所有协议请求 列出所有规则 测试 丢弃规则内的IP 连通性 清除 规则列表的 限制 模拟ip进行丢包50%的处理。 mysql proxy 代理…

面试总结-Redis篇章(十一)——分片集群、数据读写规则

分片集群、数据读写规则 主从(解决高并发)和哨兵(解决高可用)分别解决了高并发读、高可用的问题。但是依然有两个问题没有解决:解决办法:使用分片集群可以解决上述问题。 特征:客户端请求可以访…

【uni-app】【Android studio】手把手教你运行uniapp项目到Android App

最开始想写一个自定义背景的弹窗,因为要用到项目的好几个地方,不希望每个地方都需要引入。而且只需要放张图片,加个关闭按钮和功能按钮就行,类似这种效果: 开始写的时候找了一篇博客,写的很详细&#xff0…

vue element el-upload附件上传、在线预览、下载当前预览文件

上传 在线预览&#xff08;iframe&#xff09;&#xff1a; payload&#xff1a; response&#xff1a; 全部代码&#xff1a; <template><div><el-table :data"tableData" border style"width: 100%"><el-table-column prop"d…

论文阅读 - Social bot detection in the age of ChatGPT: Challenges and opportunities

论文链接&#xff1a;https://www.researchgate.net/publication/371661341_Social_bot_detection_in_the_age_of_ChatGPT_Challenges_and_opportunities 目录 摘要&#xff1a; 引言 1.1. Background on social bots and their role in society 1.2. The rise of AI-gene…

细扒电驱电控整合趋势

电机和电控的配套量方面&#xff0c;领先都是比亚迪的弗迪动力。这与比亚迪整车销量显著领先分不开。在电机和电控排名靠前的企业中&#xff0c;有很多相同的企业。根据科瑞三电系统数据的统计&#xff0c;72%的电机电控来自同一企业&#xff0c;这部分业务量在2022年的同比增速…

剑指大厂,手撕 Java 八股文

tip: 此贴为目录贴&#xff0c;定期更新 toNew: 时间是最好的答案&#xff0c;它能解决所有问题。坚持&#xff01;&#xff01;&#xff01; ✌本文章旨在总结 Java 的知识生态以及帮助需要学习者和求职者&#xff0c;本人从事应用安全和大数据领域&#xff0c;有8年开发经验&…

mse.backward()作用及原理

作用&#xff1a;自动求导。计算那些有关图中叶子节点的tensors的梯度(这里的叶子节点指的是那些require_gardtrue的叶子节点) 计算叶子节点的梯度&#xff0c;自动附加在每个tensor的成员变量上&#xff0c;之后通过变量.grad&#xff0c;比如w.grad,b.grad 来调用。 另外补…

浅谈React中的ref和useRef

目录 什么是useRef&#xff1f; 使用 ref 访问 DOM 元素 Ref和useRef之间的区别 Ref和useRef的使用案例 善用工具 结论 在各种 JavaScript 库和框架中&#xff0c;React 因其开发人员友好性和支持性而得到认可。 大多数开发人员发现 React 非常舒适且可扩展&#xff0c;…

从0到1开发go-tcp框架【3-读写协程分离、引入消息队列、进入连接管理器、引入连接属性】【基础篇完结】

从0到1开发go-tcp框架【3-读写协程分离、引入消息队列、进入连接管理器、引入连接属性】 1 读写协程分离[v0.7] 添加一个Reader和Writer之间通信的channel添加一个Writer goroutineReader由之前直接发送给客户端改为发送给通信channel启动Reader和Writer一起工作 zinx/znet/co…

TPlink DDNS 内网穿透?外网访问设置方法

有很多小伙伴都想知道&#xff1a;TPlink路由器怎么设置DDNS内网穿透&#xff1f;今天&#xff0c;小编就给大家分享一下TPlink DDNS 外网访问设置方法&#xff0c;下面是图文教程&#xff0c;帮助新手快速入门DDNS设置。 本文介绍的是云路由器TP-LINK DDNS的设置方法。TP-LIN…

【算法提高:动态规划】1.6 区间DP

文章目录 前言例题列表1068. 环形石子合并&#xff08;前缀和 区间DP 环形转换成线性⭐&#xff09;如何把环转换成区间&#xff1f;⭐实现代码补充&#xff1a;相关题目——282. 石子合并 320. 能量项链&#xff08;另一种计算价值的石子合并&#xff09;479. 加分二叉树&am…

企业电子招标采购系统源码Spring Boot + Mybatis + Redis + Layui + 前后端分离 构建企业电子招采平台之立项流程图 tbms

&#xfeff; 项目说明 随着公司的快速发展&#xff0c;企业人员和经营规模不断壮大&#xff0c;公司对内部招采管理的提升提出了更高的要求。在企业里建立一个公平、公开、公正的采购环境&#xff0c;最大限度控制采购成本至关重要。符合国家电子招投标法律法规及相关规范&am…

VBA技术资料MF38:VBA_在Excel中隐藏公式

【分享成果&#xff0c;随喜正能量】佛祖也无能为力的四件事&#xff1a;第一&#xff0c;因果不可改&#xff0c;自因自果&#xff0c;别人是代替不了的&#xff1b;第二&#xff0c;智慧不可赐&#xff0c;任何人要开智慧&#xff0c;离不开自身的磨练&#xff1b;第三&#…

Stable Diffusion - SDXL 1.0 全部样式设计与艺术家风格的配置与提示词

欢迎关注我的CSDN&#xff1a;https://spike.blog.csdn.net/ 本文地址&#xff1a;https://spike.blog.csdn.net/article/details/132072482 来源于 Anna Dittmann 安娜迪特曼&#xff0c;艺术家风格的图像&#xff0c;融合幻想、数字艺术、纹理等样式。 SDXL 是 Stable Diffus…