.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,验证应用是否运行正常。

相关推荐
不念霉运2 分钟前
Gitee DevOps:中国企业数字化转型的“本土化加速器“
运维·gitee·团队开发·代码规范·devops·代码复审
安迪小宝20 分钟前
6 任务路由与负载均衡
运维·python·celery
遇见火星31 分钟前
jenkins流水线常规配置教程!
运维·docker·jenkins
LunarCod2 小时前
Ubuntu使用Docker搭建SonarQube企业版(含破解方法)
linux·运维·服务器·ubuntu·docker·开源·sonarqube
什么半岛铁盒2 小时前
Linux信号的保存
linux·运维·网络
noravinsc2 小时前
国产化中间件 替换 nginx
运维·nginx·中间件
惜.己3 小时前
Linux常用命令(十四)
linux·运维·服务器
好吃的肘子3 小时前
Elasticsearch架构原理
开发语言·算法·elasticsearch·架构·jenkins
linkingvision3 小时前
H5S 视频监控AWS S3 对象存储
linux·运维·aws·视频监控s3对象存储
doupoa4 小时前
Fabric 服务端插件开发简述与聊天事件监听转发
运维·python·fabric