Ubuntu20.04+docker+jenkins+飞书实现自动化发布

itlao6 工具&方法 开发&源码 .NET来源:趣编程ACE评论895字数 4522阅读15分4秒阅读模式

一、从0-1一点一滴实现如何本地提交代码到gitlab然后实现前后端自动发布


1.更新apt包索引文章源自IT老刘-https://wp.itlao6.com/9542.html

sudo apt-get update


2.安装必备的软件包以允许apt通过https使用存储库文章源自IT老刘-https://wp.itlao6.com/9542.html

sudo apt-get install ca-certificates curl gnupg lsb-release

3.添加Docker官方版本的GPG密钥文章源自IT老刘-https://wp.itlao6.com/9542.html

sudo mkdir -p /etc/apt/keyringscurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

4.添加阿里云的GPG密钥文章源自IT老刘-https://wp.itlao6.com/9542.html

curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

5.设置存储库文章源自IT老刘-https://wp.itlao6.com/9542.html

sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"

6.安装最新版本的docker文章源自IT老刘-https://wp.itlao6.com/9542.html

sudo apt-get updatesudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin

7.验证docker是否安装成功文章源自IT老刘-https://wp.itlao6.com/9542.html

docker version

8.验证docker服务是否在运行文章源自IT老刘-https://wp.itlao6.com/9542.html

systemctl status docker

9.设置docker服务每次开机自启文章源自IT老刘-https://wp.itlao6.com/9542.html

sudo systemctl enable docker

二、安装完docker之后我们按照jenkins,其中要注意的是我们需要构建出Jenkins的镜像要包含.net core/.net6 和node.js的环境。文章源自IT老刘-https://wp.itlao6.com/9542.html

docker中安装jenkins

  1. 编写带上述环境的dockerfile

FROM jenkins/jenkins
USER root
RUN uname -a && cat /etc/*release
RUN apt-get updateRUN apt-get install -y curl libunwind8 gettext apt-transport-https
RUN curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpgRUN mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpgRUN sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-debian-stretch-prod stretch main" > /etc/apt/sources.list.d/dotnetdev.list'
# Install the .NET Core frameworkRUN apt-get updateRUN apt-get install -y dotnet-sdk-3.1 \ && apt-get install -y dotnet-sdk-5.0 \ && apt-get install -y dotnet-sdk-6.0
# Install the npm RUN apt-get install -y curl \ && curl -sL https://deb.nodesource.com/setup_16.x | bash - \ && apt-get install -y nodejs \ && curl -L https://www.npmjs.com/install.sh | sh # Install cnpmRUN npm install cnpm -g
RUN npm -vRUN cnpm -v
USER jenkins

2.根据dockerfile构建镜像文章源自IT老刘-https://wp.itlao6.com/9542.html

# 进入dockerfile所在目录docker build -t complex_jenkins .

3.根据镜像生成容器文章源自IT老刘-https://wp.itlao6.com/9542.html

# docker run  -d -p 8081:8080 --name complex_jenkins -v /etc/localtime:/etc/localtime -v /home/projects/jenkins_home:/var/jenkins_home complex_jenkins

三、.net core项目编写dockerfile文章源自IT老刘-https://wp.itlao6.com/9542.html

后端自动生成.net core dockerfile文章源自IT老刘-https://wp.itlao6.com/9542.html

打开VS2022,在启动项目上添加docker支持,这样VS2022就会帮我们生成dockerfile文章源自IT老刘-https://wp.itlao6.com/9542.html

这里有个坑,我们需要将生成的dockerfile移动到项目根目录。文章源自IT老刘-https://wp.itlao6.com/9542.html

将生成好的dockerfile提交到gitlab仓库中。文章源自IT老刘-https://wp.itlao6.com/9542.html

前端生成dockerfile文章源自IT老刘-https://wp.itlao6.com/9542.html

# 安装nginxdocker pull nginxdocker run --name=nginx -d -p 4030:80 nginx

生成dockerfile,并将dockerfile放置在与dist文件夹同级目录下文章源自IT老刘-https://wp.itlao6.com/9542.html

FROM nginx #指定基础镜像  MAINTAINER ps #说明信息 RUN rm /etc/nginx/conf.d/default.conf   ADD default.conf /etc/nginx/conf.d/  #删除基础镜像中的Nginx配置文件替换成自己写的 COPY dist/ /usr/share/nginx/html/ #将打包好的vue项目复制到基础镜像中Nginx配置文件的指定路径下

编写Nginx配置文件文章源自IT老刘-https://wp.itlao6.com/9542.html

生成一个nginx默认配置文件:default.conf 并将配置文件放置在与dockerfile同级目录下

server {    listen       8081;# 配置端口    server_name  宿主机IP; # 修改为docker服务宿主机的ip     location / {        root   /usr/share/nginx/html;        index  index.html index.htm;        try_files $uri $uri/ /index.html =404;    }     error_page   500 502 503 504  /50x.html;    location = /50x.html {        root   html;    }}

配置Jenkins

  1. 登录

    图片

2.构建项目

Ubuntu20.04+docker+jenkins+飞书实现自动化发布
Ubuntu20.04+docker+jenkins+飞书实现自动化发布

构建执行脚本

Ubuntu20.04+docker+jenkins+飞书实现自动化发布

shell脚本编写

#!/bin/bashpwdcd /var/jenkins_home/workspace/mes_bac_dev/set -euxif [[ "$(docker inspect bac_dev 2> /dev/null | grep '"Name": "/bac_dev"')" != "" ]];        then      echo 'exists container'      docker stop bac_dev      docker rm bac_devfiif [[ "$(docker images -q bac_dev 2> /dev/null)" != "" ]];        then      echo 'exists images'      docker rmi bac_develse        echo 'not exists'fidocker build -t bac_dev .docker run --name=bac_dev  -v /etc/localtime:/etc/localtime   -it -p 9995:80 -d bac_dev

前端脚本配置也如上:


构建成功后飞书消息通知

  1. 首先在飞书群配置一个机器人

    图片
Ubuntu20.04+docker+jenkins+飞书实现自动化发布

保存好webhook,以供下文python脚本使用

2.Jenkins里面配置pyhon3环境

# 进入docker jenkins 容器docker exec -it  容器id /bin/bash# 使用apt-get 代替yum 安装apt-getapt-get updateapt-get install wget
# 设置个文件夹 解压缩python包cd /var/jenkins_home/mkdir python3cd python3/wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgztar -xvf Python-3.6.8.tgz
# 安装gcc 依赖包apt-get -y install gcc automake autoconf libtool makeapt-get -y install make*apt-get -y install zlib*apt-get -y install openssl libssl-devapt-get install sudo
# 指定安装目录./configure --prefix=/var/jenkins_home/python3makemake install

3.编写python飞书脚本并放置项目上传到gitlab上

import sysimport requestsimport time #定义python系统变量JOB_URL = sys.argv[1]JOB_NAME = sys.argv[2]BUILD_NUMBER = sys.argv[3]GIT_BRANCH= sys.argv[4]
currenttime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 飞书机器人的webhook地址url = '此处为上文创建飞书机器人的webhook地址'method = 'post'headers = {'Content-Type':'application/json'} data = { "msg_type": "interactive", "card": { "config": { "wide_screen_mode": True, "enable_forward": True }, "elements": [{ "tag": "div", "text": { "content": "项目名称:" + JOB_NAME + "\n构建编号:第" + BUILD_NUMBER + "次构建\n运行时间:" + currenttime + "\n分支:" + GIT_BRANCH, "tag": "lark_md" } }], "header": { "title": { "content": JOB_NAME + "构建报告", # JOB_NAME 调用python定义的变量,这是卡片的标题 "tag": "plain_text" } } }}res= requests.request(method=method,url=url,headers=headers,json=data)print(res)print(res.json())

4.jenkins配置飞书

增加shell脚本构建

# 进入python脚本所在的目录 安装脚本的需要的requests包 并指定镜像源pip3 install requests -i http://pypi.douban.com/simple --trusted-host pypi.douban.com# 执行脚本 其中 $... 为Jenkins传递给脚本的变量 python3 autoResult.py $JOB_URL $JOB_NAME $BUILD_NUMBER $GIT_BRANCH

转自: 趣编程ACE

继续阅读
weinxin
我的微信公众号
微信扫一扫关注公众号,不定时更新
itlao6
  • 版权声明 本文源自 趣编程ACE 整理 发表于 2022年 10月 20日 18:43:00
  • 转载请务必保留本文链接:https://wp.itlao6.com/9542.html
评论  0  访客  0
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定