运行Jenkins的Docker容器中的Dotnet构建许可被拒绝

col*_*all 6 .net build jenkins docker asp.net-core

我正在尝试使用Jenkins构建.NET应用程序。Jenkins实例在Docker容器中运行。

我的Jenkinsfile如下:

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'
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试构建时,在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)
Run Code Online (Sandbox Code Playgroud)

看来'.dotnet'文件夹在Docker容器中受到保护。有没有办法获得对此的读写权限,或者改变它的位置?当我猛扑到容器中时,似乎找不到该文件夹​​。

谢谢你的帮助。

hpa*_*nia 12

您可以HOME按照@colmulhall 的建议设置环境变量,但随后您会将 docker 容器主目录设置为/tmp.

要做到这一点,请"dotnet"设置环境变量DOTNET_CLI_HOME:

environment {
    DOTNET_CLI_HOME = "/tmp/DOTNET_CLI_HOME"
}
Run Code Online (Sandbox Code Playgroud)

或者在调用dotnet运行之前:

export DOTNET_CLI_HOME="/tmp/DOTNET_CLI_HOME"
Run Code Online (Sandbox Code Playgroud)


更新

取自https://www.jenkins.io/doc/pipeline/tour/environment/ 的示例 Jenkins 管道代码

看看如何DOTNET_CLI_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'
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

有很多方法可以实现这一目标。如果您使用的是 docker,也许更好的方法是DOTNET_CLI_HOME在 docker 镜像中定义环境变量。


col*_*all 5

该问题似乎与尝试将数据写入 Docker 容器 ('/') 的顶层有关。

将以下内容添加到 Jenkinsfile 可确保设置主目录,并且可以在具有正确权限的位置创建 .dotnet 文件夹。

environment {
   HOME = '/tmp'
} 
Run Code Online (Sandbox Code Playgroud)

  • 这解决了问题,但如果其他脚本依赖 home 位于其他地方,或者如果存在引导问题,在 Dockerfile 中设置了“USER”,则在“~/.blah”中设置了某些内容,这不是一个好主意` 并且突然 `HOME` 设置为不同的位置 `/tmp`。 (2认同)