下面只是使用docker compose 启动容器docker-compose.yml文件
- 创建drone容器
yaml
version: '3'
services:
drone-server:
restart: always
image: drone/drone:2
ports:
- "3000:80"
volumes:
- /home/dorne/lib/drone:/var/lib/drone/
- /home/dorne/data:/data/
environment:
- DRONE_GITEA_SERVER=http://域名:端口 # gitea的地址 支持http, https
- DRONE_GITEA_CLIENT_ID=gitea的密钥id
- DRONE_GITEA_CLIENT_SECRET=gitea的密钥
- DRONE_SERVER_HOST=域名:端口
- DRONE_SERVER_PROTO=http # 支持http, https
- DRONE_RPC_SECRET=随机生成的32位字符 # 和下的保持一致
- DRONE_GIT_ALWAYS_AUTH=true
- DRONE_GIT_USERNAME=root
- DRONE_GIT_PASSWORD=123456
- DRONE_USER_CREATE=username:账户名,admin:true # 开启管理员账户
drone-runner-docker:
restart: always
image: drone/drone-runner-docker:1
ports:
- "3002:3000"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- DRONE_RPC_PROTO=http # 支持http, https
- DRONE_RPC_HOST=drone-server
- DRONE_RPC_SECRET=随机生成的32位字符
- DRONE_RUNNER_NAME=drone-runner-docker
- DRONE_RUNNER_CAPACITY=2
- 创建gitea容器
yaml
version: "3"
networks:
gitea:
external: false
services:
server:
image: gitea/gitea:1.16.7
container_name: gitea
environment:
- USER_UID=1000
- USER_GID=1000
restart: always
networks:
- gitea
volumes:
- /home/data:/data # /home/data可以替换成你想要的挂载目录
- /etc/timezone:/etc/timezone:ro
- /etc/localtime:/etc/localtime:ro
ports:
- "8090:3000" # 8090可以替换成你想要的端口
- "8222:22" # 8222可以替换成22
启动流水线的.dron.yml文件
- .drone.yml文件
yaml
kind: pipeline
name: default
steps:
# - name: install-dependencies
# image: node:latest
# commands:
# - npm install
- name: deploy-to-server
image: appleboy/drone-ssh
settings:
host: "ip" # 替换为宿主机的IP地址或域名
username: "root" # 替换为宿主机的用户名
password: "宿主机密码" # 替换为宿主机的密码 (也可以使用密钥)
port: 22 # SSH连接的端口,默认为22
# private_key:
# from_secret: "VdLUwOUJF0sVWjbjbiAfcuQ8TskgY9kN" # 密钥名称,在Drone的设置中添加
script:
- rm -rf /home/vueProject/vue3_demo
- cd /home/vueProject
- git clone 项目地址 #gitea的项目地址
- echo "当前构建分支:${DRONE_BRANCH}"
- cd /home/vueProject/vue3_demo
# - git fetch --all
- git checkout ${DRONE_BRANCH} # 动态切换到正在构建的分支
- git pull origin ${DRONE_BRANCH}
- ../deploy.sh # 部署脚本
- echo "构建分支 ${DRONE_BRANCH} successful!"
- deploy.sh脚本
```yaml
#!/bin/bash
# 1. 定义变量
PROJECT_DIR="/home/vueProject/vue3_demo" # 项目目录
IMAGE_NAME="vue3_demo-application" # Docker 镜像名称
CONTAINER_NAME="vue3demo-application" # Docker 容器名称
DOCKERFILE_PATH="./Dockerfile" # Dockerfile 路径
SOURCE="dist/index.html" # npm生成的dist
# 2. 导航到项目目录
cd $PROJECT_DIR
# 4使用npm打包
echo "使用npm install 安装"
cnpm install
echo "在使用npm打包项目"
cnpm run build
echo "打包完成!"
if [ ! -f "$SOURCE" ]; then
echo "npm 构建失败"
exit 1
fi
# 5. 停止旧的 Docker 容器
echo "正在停止旧的 Docker 容器..."
docker stop $CONTAINER_NAME
# 6. 删除旧的 Docker 容器
echo "正在删除旧的 Docker 容器..."
docker rm $CONTAINER_NAME
# 7. 删除旧的 Docker 镜像
echo "正在删除旧的 Docker 镜像..."
docker rmi $IMAGE_NAME
# 8. 构建新的 Docker 镜像
echo "正在构建新的 Docker 镜像..."
docker build -t $IMAGE_NAME -f $DOCKERFILE_PATH .
# 9. 运行新的 Docker 容器
echo "正在启动新的 Docker 容器..."
docker run -d --name $CONTAINER_NAME --network=my-network -p 80:80 -v /home/vueProject/nginx.conf:/etc/nginx/nginx.con $IMAGE_NAME
# 10. 检查新容器是否运行成功
if [ "$(docker ps -q -f name=$CONTAINER_NAME)" ]; then
echo "新的 Docker 容器 $CONTAINER_NAME 启动成功!"
else
echo "启动新的 Docker 容器失败。"
exit 1
fi