3

I have build step in my jenkins-pipeline:

        stage('Build') {
        agent {
            docker {
                image 'mymvnbasedimage:latest'
                args '-u root:root'
                reuseNode true
            }
        }
        steps {
                sh "ant"
            }
        }

It works, everything is fine. But, then the workspace will contains directory and files owned by root which can not be deleted by the user jenkins at the next run when cleaning the workpace.

ls -lt ~/workspace/myjenkinsjob/dist/
total 185764
-rw-r--r-- 1 root root 190218240 Aug  3 19:49 buildresult-21.tar

O f course i can add:

        stage('Chown to user Jenkins'){
            steps {
            echo 'Chown to user Jenkins'
            sh "sudo chown -R jenkins:jenkins ${WORKSPACE}"
            }
        }

But it is not ok, because i have to add in the /etc/sudoers rights to make chown for user Jenkins. Also, i can't make inside container operation of chown, because there user jenkins does not exists. Which variant, you can advice ? Please help.

daspilker
  • 8,154
  • 1
  • 35
  • 49
Piduna
  • 609
  • 12
  • 28
  • Don’t chown by the username inside the container but the PID instead. – Mike Doe Aug 03 '19 at 20:09
  • @emix sorry ? can You explain more please ? – Piduna Aug 03 '19 at 20:15
  • UID not PID sorry :) check it with `id -u your_username` then in the container if the result was `1000`: `chown 1000:1000 -R ${WORKSPACE}`. You could also pass the UID as an argument. – Mike Doe Aug 04 '19 at 05:19

2 Answers2

0

I may suggest to Set ${WORKSPACE} as a volume with a folder on the host and set appropriate rights for jenkins on the host folder using jenkins user uid. uid user on host should be equals to uid in docker container.

  • can you explain how to do this technically ? i am already using workspace `reuseNode true` – Piduna Aug 03 '19 at 20:32
  • I misunderstood your question. can you create a user in you image mymvnbasedimage:latest ? If yes, create a jenkins user in your image (https://stackoverflow.com/questions/39855304/how-to-add-user-with-dockerfile/39855387) with the same uid as the real jenkins user (grep jenkins /etc/passwd) Then chown with your jenkins uid – user8794331 Aug 03 '19 at 21:37
0

I have used a next Kludge

script {
    def jUserID = sh returnStdout: true, script: "id -u"
    jUserID = jUserID.trim()
    def jGroupID = sh returnStdout: true, script: "id -g"
    jGroupID = jGroupID.trim()
    sh "docker run --rm -v ./src:/src bash chown -R ${jUserID}:${jGroupID} /src/FOLDER_TO_CHOWN"
}

Also, I have been looking into involving docker via the plugin as a standalone stage, something like

agent {
    docker {
        image 'bash'
        reuseNode true
    }
}
steps {
    sh 'chown -R ${jUserID}:${jGroupID} /code/FOLDER_TO_CHOWN'
}

But I have no idea how to define a variable in this case and even more, how to substitute it in the command. At least while writing this answer )

DronKram
  • 1
  • 1