learning_record_doc/devops/cicd/drone/drone-template.md

210 lines
4.5 KiB
Markdown
Raw Normal View History

2023-11-29 23:14:29 +08:00
**官方地址**
https://docs.drone.io
**官方触发方式文档**
https://docs.drone.io/pipeline/triggers/
- CICD .drone.yml 模板示例
```yml
kind: pipeline
type: docker
name: ds-cloud
#分支触发 https://docs.drone.io/pipeline/triggers/
trigger:
branch:
- dev
steps:
- name: build-jar # 流水线名称
image: maven:3.8.5-openjdk-17 # 定义创建容器的Docker镜像
volumes: # 将容器内目录挂载到宿主机仓库需要开启Trusted设置
- name: maven-cache
path: /root/.m2 # 将maven下载依赖的目录挂载出来防止重复下载
- name: maven-build
path: /mnt/app/build # 将应用打包好的Jar和执行脚本挂载出来
commands: # 定义在Docker容器中执行的shell命令
- mvn clean package -Ptest -DskipTests=true # 应用打包命
- cd docker
- sh copy.sh #复制文件到指定地址
- cd ../
- cp -r docker /mnt/app/build/
- name: build-docker
image: plugins/docker
volumes: # 将容器内目录挂载到宿主机仓库需要开启Trusted设置
- name: maven-build
path: /mnt/app/build # 将应用打包好的Jar和执行脚本挂载出来
- name: docker
path: /var/run/docker.sock # 挂载宿主机的docker
- name: dockerc #挂载docker-compose 如果没有用到可以不挂载
path: /usr/local/bin/docker-compose
#settings:
# dockerfile: /mnt/app/build/Dockerfile
commands: # 通过定义好的脚本文件 停止当前容易并重新构建
- cd /mnt/app/build/docker
- chmod +x deploy.sh
- sh deploy.sh stop
- sh deploy.sh rm
- sh deploy.sh build
- sh deploy.sh modules
- docker ps
volumes: # 定义流水线挂载目录,用于共享数据
- name: maven-build
host:
path: /www/mnt/maven/build # 从宿主机中挂载的目录
- name: maven-cache
host:
path: /www/mnt/maven/cache
- name: docker
host:
path: /var/run/docker.sock
- name: dockerc
host:
path: /usr/local/bin/docker-compose
```
- copy 脚本文件
```sh
#!/bin/sh
# 复制项目的文件到对应docker路径便于一键生成镜像。
usage() {
echo "Usage: sh copy.sh"
exit 1
}
# copy jar
echo "begin copy ds-gateway "
cp ../ds-gateway/target/ds-gateway.jar ./ds/gateway/jar
echo "begin copy ds-miniapps "
cp ../ds-modules/ds-miniapps/ds-miniapps-service/target/ds-miniapps-service.jar ./ds/modules/miniapps/jar
```
- deploy 脚本文件
```shell
#!/bin/sh
# 使用说明,用来提示输入参数
usage() {
echo "Usage: sh 执行脚本.sh [port|base|modules|stop|rm|stopbase|rmbase]"
exit 1
}
# 开启所需端口
port(){
# firewall-cmd --add-port=80/tcp --permanent
# firewall-cmd --add-port=8080/tcp --permanent
# service firewalld restart
echo "开启所需端口(未实现)"
}
# 启动基础环境(必须)
base(){
docker-compose up -d ds-nacos
}
# 停止基础环境
stopbase(){
docker-compose stop ds-nacos
}
# 删除基础环境
rmbase(){
docker-compose rm ds-nacos -f
}
# 启动程序模块(必须)
modules(){
docker-compose up -d ds-gateway ds-miniapps
}
# 关闭module环境/模块
stop(){
docker-compose stop ds-gateway ds-miniapps
}
# 删除module环境/模块
rm(){
docker-compose rm ds-gateway ds-miniapps -f
docker rmi docker-ds-gateway docker-ds-miniapps
}
build(){
docker-compose build ds-gateway ds-miniapps
}
# 根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"port")
port
;;
"base")
base
;;
"modules")
modules
;;
"stop")
stop
;;
"rm")
rm
;;
"stopbase")
stopbase
;;
"rmbase")
rmbase
;;
"build")
build
;;
*)
usage
;;
esac
```
- docker-compose.yml
```yml
version: '3'
services:
ds-gateway:
container_name: ds-gateway
build:
context: ./ds/gateway
dockerfile: Dockerfile #指定Dockerfile文件 通过dockerfile文件进行构建
ports:
- "8080:8080"
networks:
- ds-network
ds-miniapps:
container_name: ds-miniapps
build:
context: ./ds/modules/miniapps
dockerfile: Dockerfile
ports:
- "15101:15101"
networks:
- ds-network
networks:
ds-network:
external: true
```