美文网首页
jenkins 部署发布 gin-vue-admin

jenkins 部署发布 gin-vue-admin

作者: 码农工号9527 | 来源:发表于2023-08-26 20:37 被阅读0次

    前置环境 部署相关可以参考这篇文章 https://www2.jianshu.com/p/7be08071f2ca

    1.harbor创建项目相关

    新建项目



    新增项目管理员




    公开项目

    git 仓库服务器(192.168.137.10)新增 diy_gin_vue_admin 仓库

    [root@localhost ~]# cd /data/git_repo/
    [root@localhost git_repo]# ll
    总用量 0
    drwxr-xr-x. 7 git git 119 8月  27 00:12 php_test.git
    [root@localhost git_repo]# git init --bare diy_gin_vue_admin.git
    提示:使用 'master' 作为初始分支的名称。这个默认分支名称可能会更改。要在新仓库中
    提示:配置使用初始分支名,并消除这条警告,请执行:
    提示:
    提示: git config --global init.defaultBranch <名称>
    提示:
    提示:除了 'master' 之外,通常选定的名字有 'main'、'trunk' 和 'development'。
    提示:可以通过以下命令重命名刚创建的分支:
    提示:
    提示: git branch -m <name>
    已初始化空的 Git 仓库于 /data/git_repo/diy_gin_vue_admin.git/
    [root@localhost git_repo]# chown -R git:git diy_gin_vue_admin.git/
    

    然后把拉取到的 gin_vue_admin 代码提交到 diy_gin_vue_admin 仓库
    ./web是前端项目,目录下框架包含Dockerfile,可以直接使用
    .server/是后端项目,目录下也自带包含Dockerfile,直接使用
    具体项目结构可以查看 https://www.gin-vue-admin.com/guide/introduce/project.html ,官方介绍

    2.jenkins新建diy_gin_vue前端项目




    Execute shell 脚本内容

    # Jenkins机器:编译完成后,build生成一个新版本的镜像,push到远程docker仓库
    
    # Variables
    DOCKERFILE_HOME='/var/lib/jenkins/workspace/diy_gin_vue/web'
    HARBOR_IP='192.168.137.11'
    REPOSITORIES='diy_gin_vue_admin/vue-docker'
    HARBOR_USER='test'
    HARBOR_USER_PASSWD='Harbor123456'
    HARBOR_USER_EMAIL='test@qq.com'
    
    # Delete image early version.
    sudo docker login -u ${HARBOR_USER} -p ${HARBOR_USER_PASSWD} ${HARBOR_IP}:80 
    IMAGE_ID=`sudo docker images | grep ${REPOSITORIES} | awk '{print $3}'`
    if [ -n "${IMAGE_ID}" ];then
        sudo docker rmi ${IMAGE_ID}
    fi
    
    # Build image.
    cd ${DOCKERFILE_HOME}
    TAG=`date +%Y%m%d-%H%M%S`
    sudo docker build -t ${HARBOR_IP}:80/${REPOSITORIES}:${TAG} . &>/dev/null
    
    # Push to the harbor registry.
    sudo docker push ${HARBOR_IP}:80/${REPOSITORIES}:${TAG} &>/dev/null
    

    测试构建


    harbor镜像仓库查看

    新增发布docker容器操作


    Command内容

    # 拉取镜像,发布
    HARBOR_IP='192.168.137.11'
    REPOSITORIES='jenkins/maven-docker'
    HARBOR_USER='test'
    HARBOR_USER_PASSWD='Harbor123456'
    
    # 登录harbor
    docker login -u ${HARBOR_USER} -p ${HARBOR_USER_PASSWD} ${HARBOR_IP}:80
    
    # Stop container, and delete the container.
    CONTAINER_ID=`docker ps | grep "maven-docker" | awk '{print $1}'`
    if [ -n "$CONTAINER_ID" ]; then
        docker stop $CONTAINER_ID
        docker rm $CONTAINER_ID
    else #如果容器启动时失败了,就需要docker ps -a才能找到那个容器
        CONTAINER_ID=`docker ps -a | grep "maven-docker" | awk '{print $1}'`
        if [ -n "$CONTAINER_ID" ]; then  # 如果是第一次在这台机器上拉取运行容器,那么docker ps -a也是找不到这个容器的
            docker rm $CONTAINER_ID
        fi
    fi
    
    # Delet eeasy-springmvc-maven image early version.
    IMAGE_ID=`sudo docker images | grep ${REPOSITORIES} | awk '{print $3}'`
    if [ -n "${IMAGE_ID}" ];then
        docker rmi ${IMAGE_ID}
    fi
    
    # Pull image.
    # TAG=`curl -s http://${HARBOR_IP}/api/repositories/${REPOSITORIES}/tags | jq '.[-1]' | sed 's/\"//g'` 
    # TAG=`curl -s http://192.168.137.11/api/repositories/jenkins/maven-docker/tags | jq '.[-1]| {name:.name}' | awk -F '"' '/name/{print $4}'`
    TAG=`curl -s http://192.168.137.11/api/v2.0/projects/jenkins/repositories/maven-docker/artifacts |awk -F'name":"' '{print $2}'|cut -d '"' -f1`
    docker pull ${HARBOR_IP}:80/${REPOSITORIES}:${TAG} &>/dev/null
    
    # Run.
    docker run -d --name maven-docker -p 8080:8080 ${HARBOR_IP}:80/${REPOSITORIES}:${TAG}
    

    重新构建



    应用服务器(192.168.137.13)上查看容器

    [root@localhost project]# docker ps -a
    CONTAINER ID   IMAGE                                                            COMMAND                   CREATED         STATUS                      PORTS                                               NAMES
    0796a7cace61   192.168.137.11:80/diy_gin_vue_admin/vue-docker:20230827-191415   "/docker-entrypoint.…"   8 seconds ago   Up 7 seconds                80/tcp, 0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   vue-docker
    b235bd6b3fb3   192.168.137.11:80/jenkins/maven-docker:20230826-023949           "catalina.sh run"         41 hours ago    Exited (143) 40 hours ago                                                       maven-docker
    

    页面访问http://192.168.137.13:8089/查看

    3. 新建jenkins新建diy_gin_vue后端项目



    Build Steps -> Execute shell
    写入shell命令,用于jenkins服务器上执行:
    # Jenkins机器:编译完成后,build生成一个新版本的镜像,push到远程docker仓库
    
    # Variables
    DOCKERFILE_HOME='/var/lib/jenkins/workspace/diy_gin_vue/server'
    HARBOR_IP='192.168.137.11'
    REPOSITORIES='diy_gin_vue_admin/gin-server-docker'
    HARBOR_USER='test'
    HARBOR_USER_PASSWD='Harbor123456'
    HARBOR_USER_EMAIL='test@qq.com'
    
    # Delete image early version.
    sudo docker login -u ${HARBOR_USER} -p ${HARBOR_USER_PASSWD} ${HARBOR_IP}:80 
    IMAGE_ID=`sudo docker images | grep ${REPOSITORIES} | awk '{print $3}'`
    if [ -n "${IMAGE_ID}" ];then
        sudo docker rmi ${IMAGE_ID}
    fi
    
    # Build image.
    cd ${DOCKERFILE_HOME}
    TAG=`date +%Y%m%d-%H%M%S`
    sudo docker build -t ${HARBOR_IP}:80/${REPOSITORIES}:${TAG} . &>/dev/null
    
    # Push to the harbor registry.
    sudo docker push ${HARBOR_IP}:80/${REPOSITORIES}:${TAG} &>/dev/null
    

    新增构建步骤 -> Execute shell script on remote host using ssh
    写入Command命令,用于docker应用服务器上执行:

    # 拉取镜像,发布
    HARBOR_IP='192.168.137.11'
    REPOSITORIES='diy_gin_vue_admin/vue-docker'
    HARBOR_USER='test'
    HARBOR_USER_PASSWD='Harbor123456'
    
    # 登录harbor
    # 拉取镜像,发布
    HARBOR_IP='192.168.137.11'
    REPOSITORIES='diy_gin_vue_admin/gin-server-docker'
    HARBOR_USER='test'
    HARBOR_USER_PASSWD='Harbor123456'
    
    # 登录harbor
    docker login -u ${HARBOR_USER} -p ${HARBOR_USER_PASSWD} ${HARBOR_IP}:80
    
    # Stop container, and delete the container.
    CONTAINER_ID=`docker ps | grep "gin-server-docker" | awk '{print $1}'`
    if [ -n "$CONTAINER_ID" ]; then
        docker stop $CONTAINER_ID
        docker rm $CONTAINER_ID
    else #如果容器启动时失败了,就需要docker ps -a才能找到那个容器
        CONTAINER_ID=`docker ps -a | grep "gin-server-docker" | awk '{print $1}'`
        if [ -n "$CONTAINER_ID" ]; then  # 如果是第一次在这台机器上拉取运行容器,那么docker ps -a也是找不到这个容器的
            docker rm $CONTAINER_ID
        fi
    fi
    
    # Delet eeasy-springmvc-maven image early version.
    IMAGE_ID=`sudo docker images | grep ${REPOSITORIES} | awk '{print $3}'`
    if [ -n "${IMAGE_ID}" ];then
        docker rmi ${IMAGE_ID}
    fi
    
    # Pull image.
    # TAG=`curl -s http://${HARBOR_IP}/api/repositories/${REPOSITORIES}/tags | jq '.[-1]' | sed 's/\"//g'` 
    # TAG=`curl -s http://192.168.137.11/api/repositories/jenkins/maven-docker/tags | jq '.[-1]| {name:.name}' | awk -F '"' '/name/{print $4}'`
    TAG=`curl -s http://192.168.137.11/api/v2.0/projects/diy_gin_vue_admin/repositories/gin-server-docker/artifacts |awk -F'name":"' '{print $2}'|cut -d '"' -f1`
    docker pull ${HARBOR_IP}:80/${REPOSITORIES}:${TAG} &>/dev/null
    
    # Run.
    docker run -d --name gin-server-docker -p 8888:8888 ${HARBOR_IP}:80/${REPOSITORIES}:${TAG}
    

    并存并执行构建


    harbor查看多了新的镜像了

    docker应用服务器上查看
    [root@localhost project]# docker ps -a
    CONTAINER ID   IMAGE                                                                   COMMAND                   CREATED          STATUS                      PORTS                                               NAMES
    17e47a9bde95   192.168.137.11:80/diy_gin_vue_admin/gin-server-docker:20230827-193840   "/bin/sh -c './serve…"   2 minutes ago    Up 2 minutes                0.0.0.0:8888->8888/tcp, :::8888->8888/tcp           gin-server-docker
    0796a7cace61   192.168.137.11:80/diy_gin_vue_admin/vue-docker:20230827-191415          "/docker-entrypoint.…"   29 minutes ago   Up 29 minutes               80/tcp, 0.0.0.0:8089->8080/tcp, :::8089->8080/tcp   vue-docker
    b235bd6b3fb3   192.168.137.11:80/jenkins/maven-docker:20230826-023949                  "catalina.sh run"         41 hours ago     Exited (143) 41 hours ago                                                       maven-docker
    

    再次查看gin_vue_admin万张登录页初始化数据



    访问存在验证码图片超时问题,需要修改vue项目以下选项





    因为vue端口和服务端ip地址改了,所以相关vue前端配置也得一起改掉才行,改完之后提交代码重新构建,再次访问:


    操作前往初始化:




    大功告成,登陆成功

    相关文章

      网友评论

          本文标题:jenkins 部署发布 gin-vue-admin

          本文链接:https://www.haomeiwen.com/subject/lsfimdtx.html