【问题标题】:How to change from ROOT ownership to jenkins after build of the Jenkins-job inside docker container在docker容器内构建Jenkins-job后如何从ROOT所有权更改为jenkins
【发布时间】:2019-08-03 20:07:14
【问题描述】:

我的詹金斯管道中有构建步骤:

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

它工作正常,一切都很好。但是,workspace 将包含root 拥有的目录和文件,用户jenkins 在下次清理workpace 时无法删除这些目录和文件。

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

当然可以添加:

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

但这不行,因为我必须添加/etc/sudoers 权限才能为用户Jenkins 制作chown。另外,我无法进行chown 的内部容器操作,因为用户jenkins 不存在。您可以建议哪种变体?请帮忙。

【问题讨论】:

  • 不要使用容器内的用户名,而是使用 PID。
  • @emix 对不起?你能解释一下吗?
  • UID 不是 PID 抱歉 :) 使用 id -u your_username 检查它,然后在容器中检查结果是否为 1000:chown 1000:1000 -R ${WORKSPACE}。您也可以将 UID 作为参数传递。

标签: docker jenkins jenkins-pipeline dockerfile


【解决方案1】:

我可能建议将 ${WORKSPACE} 设置为在主机上带有文件夹的卷,并使用 jenkins 用户 uid 在主机文件夹上为 jenkins 设置适当的权限。主机上的 uid 用户应该等于 docker 容器中的 uid。

【讨论】:

  • 您能从技术上解释一下如何做到这一点吗?我已经在使用工作区reuseNode true
  • 我误解了你的问题。您可以在图像 mymvnbasedimage:latest 中创建用户吗?如果是,请在您的图像中创建一个 jenkins 用户 (stackoverflow.com/questions/39855304/…),其 uid 与真正的 jenkins 用户 (grep jenkins /etc/passwd) 相同,然后使用您的 jenkins uid 进行 chown
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多