Dot-Net

執行 Jenkins 的 Docker 容器中的 Dotnet 建構權限被拒絕

  • May 27, 2020

我正在嘗試使用 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'
     }
   }
 }
}

當我嘗試建構時,我在 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 容器中受到保護。有沒有辦法獲得讀/寫權限,或者改變它的位置?當我 bash 進入容器時,我似乎找不到該文件夾。

謝謝你的幫助。

該問題似乎與嘗試將數據寫入 Docker 容器 (’/’) 的頂層有關。

將以下內容添加到 Jenkinsfile 可確保設置主目錄,並且可以在具有正確權限的位置創建 .dotnet 文件夾。

environment {
  HOME = '/tmp'
} 

您可以HOME按照@colmulhall 的建議設置環境變數,但隨後您會將 docker 容器主目錄設置為/tmp.

要做到這一點,請"dotnet"設置環境變數DOTNET_CLI_HOME

environment {
   DOTNET_CLI_HOME = "/tmp/DOTNET_CLI_HOME"
}

或者在呼叫dotnet執行之前:

export DOTNET_CLI_HOME="/tmp/DOTNET_CLI_HOME"

更新

取自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'
           }
       }
   }
}

有很多方法可以實現這一目標。如果您使用的是 docker,也許更好的方法是DOTNET_CLI_HOME在 docker 映像中定義環境變數。

引用自:https://stackoverflow.com/questions/53556623