惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
月光博客
月光博客
P
Palo Alto Networks Blog
大猫的无限游戏
大猫的无限游戏
量子位
S
Secure Thoughts
博客园 - 【当耐特】
V
Visual Studio Blog
腾讯CDC
爱范儿
爱范儿
Webroot Blog
Webroot Blog
The Register - Security
The Register - Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Latest news
Latest news
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Cloudbric
Cloudbric
T
Troy Hunt's Blog
S
Security @ Cisco Blogs
B
Blog RSS Feed
I
Intezer
S
SegmentFault 最新的问题
N
News and Events Feed by Topic
云风的 BLOG
云风的 BLOG
C
CXSECURITY Database RSS Feed - CXSecurity.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
TaoSecurity Blog
TaoSecurity Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Recorded Future
Recorded Future
Google DeepMind News
Google DeepMind News
Forbes - Security
Forbes - Security
雷峰网
雷峰网
博客园 - 司徒正美
C
Cisco Blogs
S
Securelist
L
LINUX DO - 最新话题
P
Proofpoint News Feed
Blog — PlanetScale
Blog — PlanetScale
J
Java Code Geeks
N
News | PayPal Newsroom
N
News and Events Feed by Topic

博客园 - TanSea

VMware虚拟机下Linux系统(Ubuntu桌面版)微服务环境搭建 - Gogs篇 VMware虚拟机下Linux系统(Ubuntu桌面版)微服务环境搭建 - Docker篇 VMware虚拟机下Linux系统(Ubuntu桌面版)微服务环境搭建 - Jenkins篇 VMware虚拟机下Linux系统(Ubuntu桌面版)微服务环境搭建 - 准备篇 Sql Server 分批复制数据 Sql Server 查询数据库表结构 MAUI Blazor+MASA开发安卓应用学习笔记 - 设置图标和初始屏幕 MAUI Blazor+MASA开发安卓应用学习笔记 - 设置APP格式、名称、版本信息 MAUI Blazor+MASA开发安卓应用学习笔记 - 新建项目和发布 一道SQL面试题的解题思路 Windows10设置默认简体美式键盘输入法 C# 历史版本特性变更(更新到C# 11) SQL Server Report Builder RDLC按记录数分页 一次Exchange邮箱接口的开发经历 SQL Server分页查询进化史 一次.NET项目反编译的实战经验(WinForm) 点石成金-访客至上的网站设计秘笈 读书笔记 Windows7使用无线网卡建立WiFi热点 程序员的职业素养 读书笔记 - 第14章 辅导、学徒期与技艺
VMware虚拟机下Linux系统(Ubuntu桌面版)微服务环境搭建 - 构建篇
TanSea · 2025-08-28 · via 博客园 - TanSea

配置Gogs

1.创建新的仓库

右上角加号 => 创建新的仓库,如图

487799a5-ece8-4d1f-a21a-b40acf3d3ece

 仓库名称:设置一个仓库名称
.gitignore:项目是什么类型,这里就选择什么类型

 2.测试拉取和推送

image

测试通过,能正常的拉取和推送

3.修改gogs配置信息

因为在添加Web钩子时,推送地址默认禁用了本地网络地址,这里需要修改一下gogs的配置

sudo nano /home/git/gogs/custom/conf/app.ini

找到security节点,在下面添加配置信息LOCAL_NETWORK_ALLOWLIST = 192.168.86.130

image

重启使配置生效

sudo systemctl daemon-reload
sudo systemctl restart gogs

4.新建Web钩子

选择要新建Web钩子的仓库 => 仓库设置 => 管理Web钩子 => 添加新的 Web 钩子 => Gogs

image

推送地址:http://192.168.86.130:8080/generic-webhook-trigger/invoke?token=gogs-webhook-token-2025,token部分自定义,要和后面保持一致

选择只推送push事件

配置Jenkins

1.创建项

右上角New Item => Enter an item name => Pipeline

image

3.配置Triggers

Configure => Triggers

勾选 Generic Webhook Trigger
Token: gogs-webhook-token-2025  一定要和上面的推送地址里的token保持一致
勾选 Print post content: 调试时打印请求内容
勾选 Print contributed variables: 查看解析出的变量

4.配置Pipeline

Configure  => Definition => Pipeline script

将下面代码复制到Script内

pipeline {
    agent any

    environment {
        PROJECT_NAME = "mywebapi"
        DOCKER_IMAGE = "mywebapi"
        IMAGE_NAME = "mywebapi:latest"
        CONTAINER_NAME = "mywebapi"
    }

    stages {
        stage('拉取代码') {
            steps {
                echo "🔄 正在拉取代码..."
                git url: 'http://192.168.86.130:3000/tansea/MyWebApi.git', branch: 'master'
            }
        }

        stage('构建 Docker 镜像') {
            steps {
                echo "🐋 构建本地镜像..."
                sh "docker build --pull=false -t ${IMAGE_NAME} ."
            }
        }

        stage('停止并删除旧容器') {
            steps {
                script {
                    try {
                        echo "🧹 检查是否存在旧容器..."
                        def containerExists = sh(
                            script: "docker inspect ${CONTAINER_NAME} > /dev/null 2>&1 && echo true || echo false",
                            returnStdout: true
                        ).trim()

                        if (containerExists == "true") {
                            echo "⏹️ 正在停止旧容器..."
                            sh "docker stop ${CONTAINER_NAME}"
                            echo "🗑️ 正在删除旧容器..."
                            sh "docker rm ${CONTAINER_NAME}"
                        } else {
                            echo "ℹ️ 未找到旧容器,跳过停止和删除步骤"
                        }
                    } catch (Exception e) {
                        echo "⚠️ 检查容器状态时发生异常:${e}"
                    }
                }
            }
        }

        stage('启动新容器') {
            steps {
                echo "🚀 启动容器..."
                sh """
                    docker run -d \
                        --name ${CONTAINER_NAME} \
                        -p 5000:80 \
                        ${IMAGE_NAME}
                """
            }
        }
    }

    post {
        always {
            script {
                // 清理悬空镜像
                sh 'docker image prune -f'
            }
        }
        success {
            echo "✅ 构建和部署成功!"
        }
        failure {
            echo "❌ 构建失败!"
        }
    }
}

生成 Dockerfile

1.在VS项目的根目录下添加Dockerfile文件

image

 将下面的内容复制到Dockerfile

# 使用 .NET SDK 镜像作为构建环境
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build-env
WORKDIR /app

# 恢复依赖并构建发布
COPY . ./
WORKDIR /app/MyWebApi
RUN dotnet restore && \
    dotnet build -c Release && \
    dotnet publish -c Release -o /app/publish

# 构建最终运行镜像
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
WORKDIR /app
COPY --from=build-env /app/publish .
ENTRYPOINT ["dotnet", "MyWebApi.dll"]

2.拉取dotnet_sdk:8.0和dotnet_aspnet:8.0

第一次生成会比较慢,要拉取dotnet_sdk:8.0和dotnet_aspnet:8.0,可以先拉取,当然也可以跳过这一步

docker pull mcr.microsoft.com/dotnet/sdk:8.0
docker pull mcr.microsoft.com/dotnet/aspnet:8.0

查看是否拉取成功

如果能查到结果,表示拉取成功了

3.测试测试推送代码并生成Docker

推送代码到Git,查看Jenkins是否会响应Webhook构建项目以及项目是否成功构建。如果成功构建就可以通过http://192.168.86.130:5000/swagger/index.html访问到服务。如果没有构建,在日志里查看原因。

image