一、从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/keyrings
curl -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 update
sudo 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
- 编写带上述环境的dockerfile
FROM jenkins/jenkins
USER root
RUN uname -a && cat /etc/*release
RUN apt-get update
RUN apt-get install -y curl libunwind8 gettext apt-transport-https
RUN curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
RUN mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg
RUN 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 framework
RUN apt-get update
RUN 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 cnpm
RUN npm install cnpm -g
RUN npm -v
RUN 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
# 安装nginx
docker pull nginx
docker 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
- 登录
2.构建项目
构建执行脚本
shell脚本编写
#!/bin/bash
pwd
cd /var/jenkins_home/workspace/mes_bac_dev/
set -eux
if [[ "$(docker inspect bac_dev 2> /dev/null | grep '"Name": "/bac_dev"')" != "" ]];
then
echo 'exists container'
docker stop bac_dev
docker rm bac_dev
fi
if [[ "$(docker images -q bac_dev 2> /dev/null)" != "" ]];
then
echo 'exists images'
docker rmi bac_dev
else
echo 'not exists'
fi
docker build -t bac_dev .
docker run --name=bac_dev -v /etc/localtime:/etc/localtime -it -p 9995:80 -d bac_dev
前端脚本配置也如上:
构建成功后飞书消息通知
- 首先在飞书群配置一个机器人
保存好webhook,以供下文python脚本使用
2.Jenkins里面配置pyhon3环境
# 进入docker jenkins 容器
docker exec -it 容器id /bin/bash
# 使用apt-get 代替yum 安装
apt-get
apt-get update
apt-get install wget
# 设置个文件夹 解压缩python包
cd /var/jenkins_home/
mkdir python3
cd python3/
wget https://www.python.org/ftp/python/3.6.8/Python-3.6.8.tgz
tar -xvf Python-3.6.8.tgz
# 安装gcc 依赖包
apt-get -y install gcc automake autoconf libtool make
apt-get -y install make*
apt-get -y install zlib*
apt-get -y install openssl libssl-dev
apt-get install sudo
# 指定安装目录
./configure --prefix=/var/jenkins_home/python3
make
make install
3.编写python飞书脚本并放置项目上传到gitlab上
import sys
import requests
import 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
评论