windows runner agent
·
我来详细介绍 Windows Runner/Agent 的配置和使用方法:
1. Jenkins Windows Agent 配置
1.1 安装 Windows Agent
# 1. 创建 Jenkins Agent 目录
New-Item -ItemType Directory -Path "C:\Jenkins-Agent"
# 2. 下载 agent.jar
Invoke-WebRequest -Uri "http://[JENKINS-SERVER]/jnlpJars/agent.jar" -OutFile "C:\Jenkins-Agent\agent.jar"
# 3. 创建服务运行脚本
@"
java -jar C:\Jenkins-Agent\agent.jar `
-jnlpUrl http://[JENKINS-SERVER]/computer/[AGENT-NAME]/slave-agent.jnlp `
-secret [YOUR-SECRET] `
-workDir "C:\Jenkins-Agent"
"@ | Out-File C:\Jenkins-Agent\start-agent.ps1
1.2 Jenkins Pipeline 示例
pipeline {
agent {
label 'windows' // 指定 Windows agent
}
environment {
// 环境变量设置
NODEJS_HOME = 'C:\\Program Files\\nodejs'
MAVEN_HOME = 'C:\\Program Files\\Apache\\maven'
PATH = "${env.NODEJS_HOME};${env.MAVEN_HOME}\\bin;${env.PATH}"
}
stages {
stage('Prepare') {
steps {
// 清理工作空间
cleanWs()
// 检出代码
checkout scm
}
}
stage('Build') {
steps {
// Windows 批处理命令使用 bat
bat '''
echo "Starting build process..."
npm install
npm run build
'''
}
}
stage('Test') {
steps {
bat 'npm test'
}
post {
always {
// 收集测试报告
junit '**/test-results/*.xml'
}
}
}
stage('Deploy') {
steps {
bat '''
echo "Deploying application..."
xcopy /s /y dist\\* \\\\server\\deploy\\
'''
}
}
}
post {
always {
// 清理工作
cleanWs()
}
}
}
2. GitHub Actions Windows Runner
2.1 自托管 Runner 安装
# 1. 创建 Runner 目录
mkdir C:\actions-runner
cd C:\actions-runner
# 2. 下载最新版本的 Runner
Invoke-WebRequest -Uri https://github.com/actions/runner/releases/download/v[VERSION]/actions-runner-win-x64-[VERSION].zip -OutFile actions-runner.zip
# 3. 解压
Expand-Archive -Path actions-runner.zip -DestinationPath .
# 4. 配置 Runner
./config.cmd --url https://github.com/[OWNER]/[REPO] --token [TOKEN]
# 5. 安装并启动服务
./svc.cmd install
./svc.cmd start
2.2 GitHub Actions 工作流示例
name: Windows Build
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: windows-latest # 或 self-hosted
env:
NODE_VERSION: '18.x'
DOTNET_VERSION: '6.0.x'
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Cache Dependencies
uses: actions/cache@v3
with:
path: |
~\AppData\Local\npm-cache
~\AppData\Roaming\npm-cache
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
- name: Install Dependencies
run: |
npm install
dotnet restore
- name: Build
run: |
npm run build
dotnet build --configuration Release
- name: Test
run: |
npm test
dotnet test
- name: Package
run: |
# 创建发布包
mkdir release
xcopy /s /y dist\* release\
Compress-Archive -Path release\* -DestinationPath artifact.zip
- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: build-artifact
path: artifact.zip
3. 常见问题和解决方案
3.1 路径问题
# 处理 Windows 路径
$workspacePath = $env:WORKSPACE -replace '/', '\'
# 使用 PowerShell Join-Path
$buildPath = Join-Path $workspacePath "build"
3.2 权限问题
# 检查和授予权限
$acl = Get-Acl "C:\Jenkins-Agent"
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("NT AUTHORITY\NetworkService","FullControl","Allow")
$acl.SetAccessRule($rule)
Set-Acl "C:\Jenkins-Agent" $acl
3.3 环境变量持久化
# 系统级环境变量设置
[Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Java\jdk-11", "Machine")
4. 最佳实践
- 性能优化:
# 使用缓存
- uses: actions/cache@v3
with:
path: |
node_modules
packages/*/node_modules
key: ${{ runner.os }}-modules-${{ hashFiles('**/package-lock.json') }}
- 错误处理:
# PowerShell 错误处理
try {
# 执行命令
& npm install
if ($LASTEXITCODE -ne 0) { throw "npm install failed" }
} catch {
Write-Error $_.Exception.Message
exit 1
}
- 日志管理:
# 创建日志文件
Start-Transcript -Path "C:\logs\build-$(Get-Date -Format 'yyyyMMdd-HHmmss').log"
- 清理策略:
# 定期清理旧文件
Get-ChildItem -Path "C:\Builds" -Recurse |
Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } |
Remove-Item -Force -Recurse
这些配置和示例可以根据具体项目需求进行调整和扩展。重要的是要确保 Windows Runner/Agent 的稳定性和安全性,同时优化构建和部署流程的效率。
更多推荐

所有评论(0)