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

相关推荐
哇哈哈&3 小时前
gcc9.2的离线安装,支持gcc++19及以上版本
linux·运维·服务器
一条咸鱼¥¥¥3 小时前
【运维经验】使用QQ邮箱SMTP服务器设置ssms计划任务完成时邮件发送
运维·服务器·经验分享·sql·sqlserver
【上下求索】3 小时前
学习笔记095——Ubuntu 安装 lrzsz 服务?
运维·笔记·学习·ubuntu
Caster_Z5 小时前
WinServer安装VM虚拟机运行Linux-(失败,云服务器不支持虚拟化)
linux·运维·服务器
小小测试开发5 小时前
提升WebUI自动化效率与性能:从脚本到架构的全链路优化指南
运维·架构·自动化
GeminiJM6 小时前
Elasticsearch minimum_should_match 参数详解
大数据·elasticsearch·jenkins
The star"'6 小时前
mysql(1-3)
运维·mysql·云计算
model20057 小时前
Alibaba linux 3安装LAMP(5)
linux·运维·服务器
weixin_307779138 小时前
Jenkins中的Jakarta Activation API插件:功能、使用与最佳实践
运维·开发语言·ci/cd·自动化·jenkins
王 富贵10 小时前
【Linux】防火墙常用命令(iptables/firewalld/ufw)
linux·运维·服务器