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

相关推荐
FJW02081413 分钟前
【Linux】web服务器的部署和优化
linux·运维·服务器·rhce
平生不喜凡桃李32 分钟前
Linux 进程控制
linux·运维·服务器
鱼与宇1 小时前
Linux常用命令
linux·运维·服务器
小南家的青蛙1 小时前
lspci的资料
linux·运维·服务器
敖云岚1 小时前
【那些年踩过的坑】Docker换源加速详细教程(截至2025年4月)
运维·docker·容器
TracyCoder1231 小时前
ElasticSearch深入解析(三):Elasticsearch 7的安装与配置、Kibana安装
大数据·elasticsearch·jenkins
柳如烟@2 小时前
Docker安装ES :确保 Kibana 正确连接 Elasticsearch
运维·数据库·elasticsearch·docker·容器·kibana
网络工程师_ling2 小时前
【WLAN】华为无线AC双机热备负载分担—双链路热备份
运维·网络
饭来_3 小时前
配置 RDP 远程桌面协议连接ubuntu服务器桌面
linux·运维·服务器
李菠菜3 小时前
Redis主从/哨兵/集群离线部署指南
linux·运维·redis