.net 运行Jenkins的Docker容器中的Dotnet生成权限被拒绝
我正在尝试使用Jenkins构建一个.NET应用程序。Jenkins实例正在Docker容器中运行 我的文件如下:.net 运行Jenkins的Docker容器中的Dotnet生成权限被拒绝,.net,docker,jenkins,asp.net-core,build,.net,Docker,Jenkins,Asp.net Core,Build,我正在尝试使用Jenkins构建一个.NET应用程序。Jenkins实例正在Docker容器中运行 我的文件如下: pipeline { agent { docker { image 'microsoft/dotnet:2.1-sdk' registryUrl 'https://index.docker.io/v1/' } } stages { stage('Build') { steps { sh 'dot
pipeline {
agent {
docker {
image 'microsoft/dotnet:2.1-sdk'
registryUrl 'https://index.docker.io/v1/'
}
}
stages {
stage('Build') {
steps {
sh 'dotnet build MyApplication/Application.csproj -c Release -o /app'
}
}
stage('Test') {
steps {
sh 'dotnet test MyApplication/Application.csproj -c Release -r /results'
}
}
}
}
当我尝试构建时,我在Jenkins构建输出中看到以下错误:
System.UnauthorizedAccessException: Access to the path '/.dotnet' is denied. ---> System.IO.IOException: Permission denied
--- End of inner exception stack trace ---
at System.IO.FileSystem.CreateDirectory(String fullPath)
at System.IO.Directory.CreateDirectory(String path)
at Microsoft.Extensions.EnvironmentAbstractions.DirectoryWrapper.CreateDirectory(String path)
at Microsoft.DotNet.Configurer.FileSentinel.Create()
at Microsoft.DotNet.Configurer.DotnetFirstTimeUseConfigurer.Configure()
at Microsoft.DotNet.Cli.Program.ConfigureDotNetForFirstTimeUse(INuGetCacheSentinel nugetCacheSentinel, IFirstTimeUseNoticeSentinel firstTimeUseNoticeSentinel, IAspNetCertificateSentinel aspNetCertificateSentinel, IFileSentinel toolPathSentinel, Boolean hasSuperUserAccess, DotnetFirstRunConfiguration dotnetFirstRunConfiguration, IEnvironmentProvider environmentProvider)
at Microsoft.DotNet.Cli.Program.ProcessArgs(String[] args, ITelemetry telemetryClient)
at Microsoft.DotNet.Cli.Program.Main(String[] args)
.dotnet文件夹似乎在Docker容器中受到保护。有没有办法获取此文件的读/写权限,或者改变其位置?当我猛击容器时,似乎找不到文件夹
感谢您的帮助。问题似乎与试图将数据写入Docker容器的顶层(“/”)有关 将以下内容添加到Jenkins文件可确保设置了主目录,并且可以在具有正确权限的位置创建.dotnet文件夹
environment {
HOME = '/tmp'
}
您可以按照@colmulhall的建议设置
HOME
环境变量,但随后将docker容器主目录设置为/tmp
要以“dotnet”
方式执行此操作,请设置环境变量dotnet\u CLI\u HOME
:
environment {
DOTNET_CLI_HOME = "/tmp/DOTNET_CLI_HOME"
}
或在调用dotnet
run之前:
export DOTNET_CLI_HOME="/tmp/DOTNET_CLI_HOME"
更新 Jenkins管道代码示例取自 查看
DOTNET\u CLI\u HOME
是如何在environment
部分中定义的:
pipeline {
agent {
label '!windows'
}
environment {
DISABLE_AUTH = 'true'
DB_ENGINE = 'sqlite'
DOTNET_CLI_HOME = "/tmp/DOTNET_CLI_HOME"
}
stages {
stage('Build') {
steps {
echo "Database engine is ${DB_ENGINE}"
echo "DISABLE_AUTH is ${DISABLE_AUTH}"
sh 'printenv'
}
}
}
}
实现这一点的方法有很多。如果您正在使用docker,可能更好的方法是在docker映像中定义环境变量DOTNET\u CLI\u HOME
。自定义docker映像,对文件进行最小更改
如果您具有以下条件,则此选项很好
FROM mcr.microsoft.com/dotnet/core/sdk:2.2
WORKDIR /
# Setup default nuget.config, useful for custom nuget servers/sources
# Set Project-specific NuGet.Config files located in any folder from the solution folder up to the drive root. These allow control over settings as they apply to a project or a group of projects.
COPY nuget.config .
# Set the Environment Variable for the DOTNET CLI HOME PATH
ARG dotnet_cli_home_arg=/tmp/
ENV DOTNET_CLI_HOME=$dotnet_cli_home_arg
在与docker文件相同的目录中创建图像
docker build -t jenkins-dotnet:latest .
为要推送到的服务器设置标记
docker tag jenkins-dotnet:latest some.docker.registry/jenkins-dotnet
将您的jenkins dotnet图像推送到
docker push some.docker.registry/jenkins-dotnet
然后,所有项目的Jenkinsfile可能如下所示
pipeline {
agent {
docker {
image 'some.docker.registry/jenkins-dotnet'
registryUrl 'https://some.docker.registry'
}
}
stages {
stage('Build') {
steps {
sh 'dotnet build MyApplication/Application.csproj -c Release -o /app'
}
}
stage('Test') {
steps {
sh 'dotnet test MyApplication/Application.csproj -c Release -r /results'
}
}
}
}
我需要在哪里编写此代码
环境{..
@ArvindChourasiya如果您正在使用Jenkins,请查看以下内容以确定如何在Jenkins管道阶段定义环境变量:这可以解决问题,但如果其他脚本依赖home在其他地方,或者如果在Dockerfile中设置了用户
,则这不是一个好主意,因此方法设置在~/.blah
中,突然主页
被设置到另一个位置/tmp
。