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

相关推荐
Karoku0661 小时前
【CI/CD】CI/CD环境搭建流程和持续集成环境配置
运维·ci/cd·docker·容器·kubernetes·prometheus
勤奋的凯尔森同学4 小时前
webmin配置终端显示样式,模仿UbuntuDesktop终端
linux·运维·服务器·ubuntu·webmin
技术小齐8 小时前
网络运维学习笔记 016网工初级(HCIA-Datacom与CCNA-EI)PPP点对点协议和PPPoE以太网上的点对点协议(此处只讲华为)
运维·网络·学习
ITPUB-微风8 小时前
Service Mesh在爱奇艺的落地实践:架构、运维与扩展
运维·架构·service_mesh
落幕8 小时前
C语言-进程
linux·运维·服务器
chenbin5209 小时前
Jenkins 自动构建Job
运维·jenkins
java 凯9 小时前
Jenkins插件管理切换国内源地址
运维·jenkins
AI服务老曹9 小时前
运用先进的智能算法和优化模型,进行科学合理调度的智慧园区开源了
运维·人工智能·安全·开源·音视频
sszdzq10 小时前
Docker
运维·docker·容器
book012110 小时前
MySql数据库运维学习笔记
运维·数据库·mysql