Gogs+Drone+Docker CI/CD
Gogs+Drone+Docker CD/CD Demo
1.准备docker-compose.yml
本机IP:172.24.16.1
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
version: "2" services: gogs: container_name: gogs image: gogs/gogs:latest ports: - 3000:3000 volumes: - ./gogs/data:/data environment: - TZ=Asia/Shanghai restart: always drone-server: image: drone/drone:latest container_name: drone-server ports: - 801:80 volumes: - /var/run/docker.sock:/var/run/docker.sock - ./drone/:/var/lib/drone environment: - DRONE_OPEN=true - DRONE_SERVER_HOST=172.24.16.1 - DRONE_DEBUG=true - DRONE_GIT_ALWAYS_AUTH=false - DRONE_GOGS=true - DRONE_GOGS_SKIP_VERIFY=false - DRONE_GOGS_SERVER=http://172.24.16.1:3000 - DRONE_PROVIDER=gogs - DRONE_SERVER_PROTO=http - DRONE_RPC_SECRET=7b4eb5caee376cf81a2fcf7181e66175 - DRONE_USER_CREATE=username:Test,admin:true - DRONE_DATABASE_DATASOURCE=/var/lib/drone/drone.sqlite - DRONE_DATABASE_DRIVER=sqlite3 - TZ=Asia/Shanghai restart: always drone-runner-docker: image: drone/drone-runner-docker:latest container_name: drone-runner-docker depends_on: - drone-server volumes: - /var/run/docker.sock:/var/run/docker.sock environment: - DRONE_RPC_PROTO=http - DRONE_RPC_HOST=172.24.16.1:801 - DRONE_RPC_SECRET=7b4eb5caee376cf81a2fcf7181e66175 - DRONE_RUNNER_CAPACITY=10 - DRONE_DEBUG=true - TZ=Asia/Shanghai restart: always
2.进入文件目录直接docker compose up -d 进行容器编排启动
3.数据库选择的是Sqlite3.配置域名以及应用URL
设置一个管理员账号
4.建立一个Gogs 仓库
5.在Drone配置仓库流水线,对仓库信任并保存。
6.修改web hook改成drone的serever地址
7.创建一个.NET Core 项目 并添加Gogs的地址 推送触发web hook 进行自动化部署 在根目录需要.drone.yml
.drone.yml 测试代码
kind: pipeline type: docker name: default steps: - name: greeting image: alpine commands: - echo hello - echo world
8.提交到Gogs仓库 测试
在项目根目录 修改.drone.yml,进行编译部署。
#当我们向Git仓库Push代码时,会自动触发Web钩子,然后Drone就会从Git仓库Clone代码,再通过项目目录下的.drone.yml配置,执行相应的流水线 kind: pipeline type: docker #流水线类型 还有k8s等 name: deployment # 流水线名称 platform: os: linux arch: amd64 steps: #流水线执行步骤 - name: pack when: branch: - master image: docker:dind environment: IMAGE: management volumes: - name: dockersock path: /var/run/docker.sock commands: - docker build -f ./Dockerfile -t management . - name: push when: branch: - master image: docker:dind environment: IMAGE: management volumes: - name: dockersock path: /var/run/docker.sock commands: - docker stop management && docker rm management - docker run -d -p 8087:80 --name management management:latest volumes: - name: dockersock host: path: /var/run/docker.sock
dockerfile如下
FROM mcr.microsoft.com/dotnet/aspnet:3.1 AS base WORKDIR /app EXPOSE 80 FROM mcr.microsoft.com/dotnet/sdk:3.1 AS build WORKDIR /src COPY ["DroneWebApi.csproj", "."] RUN dotnet restore "./DroneWebApi.csproj" COPY . . WORKDIR "/src/." RUN dotnet build "DroneWebApi.csproj" -c Release -o /app/build FROM build AS publish RUN dotnet publish "DroneWebApi.csproj" -c Release -o /app/publish FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "DroneWebApi.dll"]
效果图 第一次构建比较慢 后面就非常快了。
![](https://img2022.cnblogs.com/blog/1761239/202205/1761239-20220502110941881-1646765086.png)