赞
踩
在github中设置第三方app配置。
需要记住id和密码,用于后面配置drone连接github。
生成drone的共享密钥,用于drone之前共享
[root@hecs-79411 drone]# openssl rand -hex 16
07182d77d40ff996d546c59f985c262a
c l i e n t I d 替换成之前 g i t h u b 创建第三方服务对应的 C l i e n t I d ; clientId替换成之前github创建第三方服务对应的Client Id; clientId替换成之前github创建第三方服务对应的ClientId;clientSecret替换成之前github创建第三方服务对应的Client Secrets;$ip替换成drone安装服务器ip;
version: '3' networks: drone: external: false services: # 容器名称 drone-server: container_name: drone # 构建所使用的镜像 image: drone/drone # 映射容器内80端口到宿主机的8611端口8611端口,若修改的话,那么上面Gitee上也需要进行修改 ports: - 8611:80 # 映射容器内/data目录到宿主机的目录 volumes: - /usr/local/soft/drone/data:/data # 容器随docker自动启动 restart: always privileged: true networks: - drone environment: # Gitee 服务器地址 - DRONE_GITHUB_SERVER=https://github.com # Gitee OAuth2客户端ID # - DRONE_GITEA_CLI(上面的Client ID值) - DRONE_GITHUB_CLIENT_ID=$clientId # Gitee OAuth2客户端密钥(上面的Client Secret值) - DRONE_GITHUB_CLIENT_SECRET=$clientSecret # drone的共享密钥(生成rpc密钥) - DRONE_RPC_SECRET=07182d77d40ff996d546c59f985c262a # drone的主机名(改成自己的域名获得ip+端口(注意是drome的)) - DRONE_SERVER_HOST=$ip:8611 # 外部协议方案根据你的域名判断是http还是https(ip加端口是http) - DRONE_SERVER_PROTO=http - DRONE_GIT_ALWAYS_AUTH=false # 创建管理员账户,这里对应为gitee的用户名(也就是登录的账号,不是昵称)(填错了回导致自动化部署失败) - DRONE_USER_CREATE=username:AlexhahahaDrag,admin:true docker-runner: container_name: drone-runner image: drone/drone-runner-docker restart: always privileged: true networks: - drone depends_on: - drone-server volumes: - /var/run/docker.sock:/var/run/docker.sock - /sync/drone/agent/drone.key:/root/drone.key environment: # 用于连接到Drone服务器的协议。该值必须是http或https。(同上) - DRONE_RPC_PROTO=http # 用于连接到Drone服务器的主机名(同上) - DRONE_RPC_HOST=$ip:8611 # Drone服务器进行身份验证的共享密钥,和上面设置一样(生成rpc密钥) - DRONE_RPC_SECRET=07182d77d40ff996d546c59f985c262a # 限制运行程序可以执行的并发管道数 - DRONE_RUNNER_CAPACITY=2 # docker runner 名称 - DRONE_RUNNER_NAME=docker-runner - DRONE_DEBUG=true # 调试相关,部署的时候建议先打开 - DRONE_LOGS_DEBUG=true # 调试相关,部署的时候建议先打开 - DRONE_LOGS_TRACE=true # 调试相关,部署的时候建议先打开 - TZ=Asia/Shanghai
docker-compose -f drone.yml up -d
然后登录 http:$ip:8611,授信项目信息。
可以在Secrets下设置一些隐秘的信息,如账号、密码等信息。然后再.drone.yaml文件中通过from_secret: ssh_email_username这种方式配置。
在微服务的路径下创建并配置Dockerfile文件
FROM openjdk:17
MAINTAINER alex 734663446@qq.com
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& echo "Asia/Shanghai" > /etc/timezone
ARG JAR_FILE=./*.jar
COPY ${JAR_FILE} alex_miaosha_monitor-1.0-SNAPSHOT.jar
ENTRYPOINT ["java", "-Xmx512m", "-jar", "alex_miaosha_monitor-1.0-SNAPSHOT.jar"]
CMD ["--spring.profiles.active=prod"]
EXPOSE 30006
在项目的根目录上创建.drone.yml,配置drone自动化部署需要的文件。$ip修改成对应的服务器地址。
kind: pipeline # 定义对象类型,还有secret和signature两种类型 type: docker # 定义流水线类型,还有kubernetes、exec、ssh等类型 name: drone-miaosha # 定义流水线名称 steps: # 定义流水线执行步骤,这些步骤将顺序执行 - name: build-package # 流水线名称 image: maven:3.8-openjdk-17 # 定义创建容器的Docker镜像 volumes: # 将容器内目录挂载到宿主机,仓库需要开启Trusted设置 - name: maven-build path: /usr/local/soft/drone/alex_miaosha # 将应用打包好的Jar和执行脚本挂载出来 commands: - mvn clean package -DskipTests=true -s settings.xml -B -U # 将打包后的jar包,拷贝到挂载目录 - cp /drone/src/alex_miaosha_monitor/target/alex_miaosha_monitor-1.0-SNAPSHOT.jar /usr/local/soft/drone/alex_miaosha/monitor/ - cp /drone/src/alex_miaosha_finance/target/alex_miaosha_finance-1.0-SNAPSHOT.jar /usr/local/soft/drone/alex_miaosha/finance/ - cp /drone/src/alex_miaosha_gateway/target/alex_miaosha_gateway-1.0-SNAPSHOT.jar /usr/local/soft/drone/alex_miaosha/gateway/ - cp /drone/src/alex_miaosha_mission/target/alex_miaosha_mission-1.0-SNAPSHOT.jar /usr/local/soft/drone/alex_miaosha/mission/ - cp /drone/src/alex_miaosha_web/target/alex_miaosha_web-1.0-SNAPSHOT.jar /usr/local/soft/drone/alex_miaosha/web/ - cp /drone/src/alex_generator/target/alex_generator-1.0-SNAPSHOT.jar /usr/local/soft/drone/alex_miaosha/generator/ # 将Dockerfile拷贝到挂载目录 - cp /drone/src/alex_miaosha_monitor/Dockerfile /usr/local/soft/drone/alex_miaosha/monitor/ - cp /drone/src/alex_miaosha_finance/Dockerfile /usr/local/soft/drone/alex_miaosha/finance/ - cp /drone/src/alex_miaosha_gateway/Dockerfile /usr/local/soft/drone/alex_miaosha/gateway/ - cp /drone/src/alex_miaosha_mission/Dockerfile /usr/local/soft/drone/alex_miaosha/mission/ - cp /drone/src/alex_miaosha_web/Dockerfile /usr/local/soft/drone/alex_miaosha/web/ - cp /drone/src/alex_generator/Dockerfile /usr/local/soft/drone/alex_miaosha/generator/ - name: ssh-monitor pull: if-not-exists image: appleboy/drone-ssh settings: # 你服务器ip地址 host: $ip # 服务器端口号 port: 22 # 服务器账号 username: root # 密码登入写法 password: from_secret: ssh_password script: - cd /usr/local/soft/drone/alex_miaosha/monitor - ls - docker build -t alex_miaosha_monitor:latest . - docker rm -f monitor - docker run -p 30099:30099 --name=monitor -v /usr/local/soft/drone/alex_miaosha/monitor/logs:/logs/alex-monitor -d alex_miaosha_monitor:latest - name: ssh-gateway pull: if-not-exists image: appleboy/drone-ssh settings: # 你服务器ip地址 host: $ip # 服务器端口号 port: 22 # 服务器账号 username: root # 密码登入写法 password: from_secret: ssh_password script: - cd /usr/local/soft/drone/alex_miaosha/gateway - ls - docker build -t alex_miaosha_gateway:latest . - docker rm -f gateway - docker run -p 30001:30001 --name=gateway -v /usr/local/soft/drone/alex_miaosha/gateway/logs:/logs/alex-gateway -d alex_miaosha_gateway:latest - name: ssh-finance pull: if-not-exists image: appleboy/drone-ssh settings: # 你服务器ip地址 host: $ip # 服务器端口号 port: 22 # 服务器账号 username: root # 密码登入写法 password: from_secret: ssh_password script: - cd /usr/local/soft/drone/alex_miaosha/finance - ls - docker build -t alex_miaosha_finance:latest . - docker rm -f finance - docker run -p 30008:30008 --name=finance -v /usr/local/soft/drone/alex_miaosha/finance/logs:/logs/alex-finance -d alex_miaosha_finance:latest - name: notify # 步骤4 部署完成,邮件通知 pull: if-not-exists # 如果镜像不存在则拉取,免去每次都要重新下载 image: drillster/drone-email settings: recipients_only: true # 只发送给指定邮件收件人,不默认发送给流水线创建人 host: smtp.qq.com #SMTP服务器 例如 smtp.qq.com port: 465 #SMTP服务端口 例如QQ邮箱端口465 subject: "Drone Build Complete!" username: from_secret: ssh_email_username password: from_secret: ssh_email_password from: from_secret: ssh_email_username recipients: 734663446@qq.com #收件人邮箱 when: #执行条件 status: - success - changed - failure volumes: # 定义流水线挂载目录,用于共享数据 - name: maven-build host: path: /usr/local/soft/drone/alex_miaosha/ #jar包目录可以修改从宿主机中挂载的目录 # 可限制哪些分支可以推送自动CICD trigger: branch: - master
在项目的根目录上创建settings.xml文件,加快打包速度。
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
<localRepository>/root/.m2/repository</localRepository>
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun</name>
<mirrorOf>central</mirrorOf>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
<pluginGroups>
<pluginGroup>org.apache.maven.plugins</pluginGroup>
<pluginGroup>org.codehaus.mojo</pluginGroup>
</pluginGroups>
</settings>
在前端项目的根目录上分别创建.drone.yml、Dockerfile和docker/nginx.conf文件。
将.drone.yml文件中的$ip修改成自己drone服务器对应的ip。
kind: pipeline type: docker name: alex_miaosha_front #定义drone中的前端项目名称 steps: # 定义流水线执行步骤,这些步骤将顺序执行 - name: yarn-package # 流水线名称 image: node:latest # 定义创建容器的Docker镜像 volumes: # 将容器内目录挂载到宿主机,仓库需要开启Trusted设置 - name: yarn-package path: /usr/local/soft/drone/alex_miaosha # 将应用打包好的Jar和执行脚本挂载出来 commands: - yarn config set registry https://registry.npm.taobao.org/ - yarn install - yarn build # 将打包后的dist包,拷贝到挂载目录 - cp /drone/src/Dockerfile /usr/local/soft/drone/alex_miaosha/front/ - cp /drone/src/docker/nginx.conf /usr/local/soft/drone/alex_miaosha/front/ - cp -r /drone/src/dist /usr/local/soft/drone/alex_miaosha/front/ - name: ssh-front pull: if-not-exists image: appleboy/drone-ssh settings: # 你服务器ip地址 host: $ip # 服务器端口号 port: 22 # 服务器账号 username: root # 密码登入写法 password: from_secret: ssh_password script: - cd /usr/local/soft/drone/alex_miaosha/front - docker build -t alex_miaosha_front:latest . - docker rm -f alex_front - docker run -p 3000:3000 --name=alex_front -d alex_miaosha_front:latest - name: notify # 步骤4 部署完成,邮件通知 pull: if-not-exists # 如果镜像不存在则拉取,免去每次都要重新下载 image: drillster/drone-email settings: recipients_only: true # 只发送给指定邮件收件人,不默认发送给流水线创建人 host: smtp.qq.com #SMTP服务器 例如 smtp.qq.com port: 465 #SMTP服务端口 例如QQ邮箱端口465 subject: "Drone Build Complete!" username: from_secret: ssh_email_username password: from_secret: ssh_email_password from: from_secret: ssh_email_username recipients: 734663446@qq.com #收件人邮箱 when: #执行条件 status: - success - changed - failure volumes: # 定义流水线挂载目录,用于共享数据 - name: yarn-package host: path: /usr/local/soft/drone/alex_miaosha #jar包目录可以修改从宿主机中挂载的目录 # 可限制哪些分支可以推送自动CICD trigger: branch: - master
# nginx镜像 FROM nginx:latest # 维护者信息 MAINTAINER majf "734663446@qq.com" # 移除nginx容器的default.conf文件、nginx配置文件 RUN rm /etc/nginx/conf.d/default.conf RUN rm /etc/nginx/nginx.conf # 把主机的nginx.conf文件复制到nginx容器的/etc/nginx文件夹下 COPY ./nginx.conf /etc/nginx/ # 拷贝前端vue项目打包后生成的文件到nginx下运行 COPY ./dist /usr/share/nginx/html # 暴露3000端口 EXPOSE 3000 # 注:CMD不同于RUN,CMD用于指定在容器启动时所要执行的命令,而RUN用于指定镜像构建时所要执行的命令。 # RUN指令创建的中间镜像会被缓存,并会在下次构建中使用。如果不想使用这些缓存镜像,可以在构建时指定--no-cache参数,如:docker build --no-cache # 使用daemon off的方式将nginx运行在前台保证镜像不至于退出 CMD ["nginx", "-g", "daemon off;"]
$ip修改成后端项目对应的服务器地址,30001修改成后端服务对应的端口号。
user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; # include /etc/nginx/conf.d/*.conf; upstream backupUrl { server $ip:30001 weight=1 max_fails=2 fail_timeout=10s; } server { listen 3000; charset utf-8; server_name $ip;# 服务器地址或绑定域名 # start --------------------------------------------------------------------------------------------- location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html; } location /api/ { proxy_pass http://$ip:30001/; #proxy_set_header Host $host; #proxy_set_header X-Real-IP $remote_addr; #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_set_header REMOTE-HOST $remote_addr; #add_header X-Cache $upstream_cache_status; #add_header Cache-Control no-cache; } # end --------------------------------------------------------------------------------------------- error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } }
其他配置drone同之前配置的后端项目。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。