Dotnet build permission denied in Docker container running Jenkins

后端 未结 3 1084
栀梦
栀梦 2021-02-20 11:12

I am trying to build a .NET application using Jenkins. The Jenkins instance is running in a Docker container.

My Jenkinsfile is as follows:

pipeline {
          


        
相关标签:
3条回答
  • 2021-02-20 11:25

    The issue appeared to be linked to trying to write data to the top level of the Docker container ('/').

    Adding the following to the Jenkinsfile ensures that the home directory is set and the .dotnet folder can be created in a location with correct permissions.

    environment {
       HOME = '/tmp'
    } 
    
    0 讨论(0)
  • 2021-02-20 11:26

    Custom Docker Image, minimal changes to Jenkinsfile

    This option is good if you have the following conditions.

    1. You have multiple Jenkinsfile projects all using the same docker image.
    2. You have custom Nuget package source.

    If you are using a custom docker image, based off the dotnet sdk docker image. You can create a Docker file with the following.

    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
    

    Create your image in the same directory as your docker file.

    docker build -t jenkins-dotnet:latest .
    

    Set your tag for the server you want to push to.

    docker tag jenkins-dotnet:latest some.docker.registry/jenkins-dotnet
    

    Push your jenkins-dotnet image to

    docker push some.docker.registry/jenkins-dotnet
    

    Then your Jenkinsfile for all of the projects could be something as follows.

    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'
          }
        }
      }
    }
    
    0 讨论(0)
  • 2021-02-20 11:30

    You can set the HOME environment variable as @colmulhall suggested, but then you will set the docker container home directory to /tmp.

    To do it in "dotnet" way set the environment variable DOTNET_CLI_HOME:

    environment {
        DOTNET_CLI_HOME = "/tmp/DOTNET_CLI_HOME"
    }
    

    Or before calling dotnet run:

    export DOTNET_CLI_HOME="/tmp/DOTNET_CLI_HOME"
    


    Update

    A sample Jenkins pipeline code taken from https://www.jenkins.io/doc/pipeline/tour/environment/

    Look how DOTNET_CLI_HOME is defined in the environment section:

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

    There are many ways to achieve this. If you are using docker, maybe a better way is defining the environment variable DOTNET_CLI_HOME in the docker image.

    0 讨论(0)
提交回复
热议问题