.NET Core 项目配置到 Jenkins

  1. Jenkins 基础环境准备

安装必要插件

确保 Jenkins 安装了以下插件:

Pipeline(用于构建 Pipeline)

Docker Pipeline(如果使用 Docker 构建)

Git Plugin(用于拉取代码)

MSBuild Plugin(如果在 Windows 上构建 .NET 项目)

安装 .NET Core SDK

在 Jenkins 构建节点(Master 或 Agent)上安装所需版本的 .NET Core SDK(如 7.0)。

可以通过以下命令安装(基于 Linux 示例):

bash

wget https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnetinstall.sh

chmod +x dotnetinstall.sh

./dotnetinstall.sh channel 7.0

安装 Docker

如果使用 Docker 构建和运行应用,请确保 Jenkins 服务器已安装 Docker 并配置 Jenkins 用户对 Docker 的访问权限。

  1. 配置 Jenkins Job

以下是两种常见的配置方法:Pipeline 脚本和自由风格项目。

方法 1: 使用 Pipeline 脚本

  1. 创建 Jenkins Pipeline 项目:

打开 Jenkins,点击 New Item > Pipeline > 输入名称 > 点击 OK。

  1. 在 Pipeline 中配置脚本:

在 "Pipeline" 部分选择 Pipeline script 并添加以下脚本:

groovy

pipeline {

agent any

stages {

stage('Checkout') {

steps {

// 拉取代码

git branch: 'main', url: 'https://github.com/yourrepo/WebApplication2.git'

}

}

stage('Build') {

steps {

// 恢复依赖项

sh 'dotnet restore WebApplication2.csproj'

// 编译项目

sh 'dotnet build WebApplication2.csproj c Release'

}

}

stage('Publish') {

steps {

// 发布项目到指定目录

sh 'dotnet publish WebApplication2.csproj c Release o ./publish'

}

}

stage('Docker Build') {

steps {

// 使用 Dockerfile 构建镜像

sh 'docker build t webapplication2 ./'

}

}

stage('Run Docker Container') {

steps {

// 运行容器

sh 'docker run d p 5000:80 p 5001:443 webapplication2'

}

}

}

}

  1. 运行 Pipeline:

点击 "Build Now" 按钮启动 Pipeline。

方法 2: 自由风格项目

如果您不使用 Pipeline,可以通过以下步骤配置自由风格项目:

  1. 创建自由风格项目:

打开 Jenkins,点击 New Item > Freestyle project > 输入名称 > 点击 OK。

  1. 配置源码管理:

在 "Source Code Management" 中选择 Git。

输入仓库地址,如 https://github.com/yourrepo/WebApplication2.git。

  1. 配置构建步骤:

点击 Add Build Step > Execute Shell,输入以下内容:

bash

恢复依赖项

dotnet restore WebApplication2.csproj

编译项目

dotnet build WebApplication2.csproj c Release

发布项目到指定目录

dotnet publish WebApplication2.csproj c Release o ./publish

使用 Dockerfile 构建镜像

docker build t webapplication2 ./

运行容器

docker run d p 5000:80 p 5001:443 webapplication2

  1. 运行构建:

点击 "Build Now" 按钮启动构建。

  1. 配置自动化触发

Webhook 自动触发

在 GitHub 或其他代码管理工具中配置 Webhook,使代码提交自动触发 Jenkins 构建:

打开 Jenkins 项目配置。

在 Build Triggers 中勾选 GitHub hook trigger for GITScm polling。

在 GitHub 仓库中添加 Jenkins 的 Webhook(http://<JENKINS_URL>/githubwebhook/)。

定时构建

在 Build Triggers 中勾选 Build periodically,使用 Cron 表达式配置构建时间,例如:

H/15 每 15 分钟触发一次

  1. 验证结果

检查 Jenkins 控制台日志:

确保所有步骤执行成功,没有报错。

访问应用:

打开浏览器访问 http://<Jenkins_Host>:5000,验证应用是否运行正常。

相关推荐
小卒过河010414 小时前
使用apache nifi 从数据库文件表路径拉取远程文件至远程服务器目的地址
运维·服务器·数据库
Empty_77714 小时前
DevOps理念
运维·devops
叶之香14 小时前
CentOS/RHEL 7、8安装exfat和ntfs文件系统
linux·运维·centos
不过普通话一乙不改名14 小时前
Linux 内核开发入门:从环境配置到 Hello World 实战
linux·运维
Trouvaille ~14 小时前
【Linux】理解“一切皆文件“与缓冲区机制:Linux文件系统的设计哲学
linux·运维·服务器·操作系统·进程·文件·缓冲区
小五传输14 小时前
隔离网闸的作用是什么?新型网闸如何构筑“数字护城河”?
大数据·运维·安全
算力魔方AIPC15 小时前
使用 Docker 一键部署 PaddleOCR-VL: 新手保姆级教程
运维·docker·容器
Evan芙15 小时前
nginx核心配置总结,并实现nginx多虚拟主机
运维·数据库·nginx
FIT2CLOUD飞致云16 小时前
操作教程丨通过1Panel快速安装Zabbix,搭建企业级监控系统
运维·服务器·开源·zabbix·监控·1panel