簡體   English   中英

Do.net 構建權限在運行 Jenkins 的 Docker 容器中被拒絕

[英]Dotnet build permission denied in Docker container running Jenkins

我正在嘗試使用 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 構建 output 中看到以下錯誤:

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)

看起來“.do.net”文件夾在 Docker 容器中受到保護。 有沒有辦法獲得對此的讀/寫權限,或者改變它的位置的方法? 當我 bash 進入容器時,我似乎找不到文件夾。

謝謝你的幫助。

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

要以"dotnet"方式進行設置,請設置環境變量DOTNET_CLI_HOME

environment {
    DOTNET_CLI_HOME = "/tmp/DOTNET_CLI_HOME"
}

或者在調用dotnet run 之前:

export DOTNET_CLI_HOME="/tmp/DOTNET_CLI_HOME"


更新

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

看看DOTNET_CLI_HOMEenvironment部分是如何定義的:

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

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

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

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

environment {
   HOME = '/tmp'
} 

自定義 Docker 映像,對 Jenkinsfile 的更改最少

如果您有以下條件,則此選項很好。

  1. 您有多個 Jenkinsfile 項目都使用相同的 docker 鏡像。
  2. 您有自定義 Nuget 包源。

如果您使用自定義 docker 鏡像,則基於 dotnet sdk docker 鏡像。 您可以使用以下內容創建 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'
      }
    }
  }
}

添加用戶。 例如:

RUN useradd -m jenkinsbuild

在撥打任何do.net電話之前先撥打您的 Dockerfile,即可解決問題。

現在$HOME已設置並且do.net不使用根文件夾/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM