Running a command as root in JenkinsFile











up vote
0
down vote

favorite












I have a command which requires root in JenkinsFile.But when i run it on my localhost it gives error that "sudo not found".The JenkinsFile is in the source code.



stage('Install packages') {
sh("docker run --rm -v `pwd`:/app -w /app node yarn install")
sh("sudo chown -R jenkins: ./node_modules")
}


I have already tried this. How to run a script as root in Jenkins? The temp sh script is created on each build.
And the error is



/var/jenkins_home/workspace/boilerplate_master-2ESKZDF4PBIXGKODFFKIVAND5RN5VWF6QLEGRZ44LZGESCULVC4Q@tmp/durable-14d87eaa/script.sh: line 1: sudo: not found










share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a command which requires root in JenkinsFile.But when i run it on my localhost it gives error that "sudo not found".The JenkinsFile is in the source code.



    stage('Install packages') {
    sh("docker run --rm -v `pwd`:/app -w /app node yarn install")
    sh("sudo chown -R jenkins: ./node_modules")
    }


    I have already tried this. How to run a script as root in Jenkins? The temp sh script is created on each build.
    And the error is



    /var/jenkins_home/workspace/boilerplate_master-2ESKZDF4PBIXGKODFFKIVAND5RN5VWF6QLEGRZ44LZGESCULVC4Q@tmp/durable-14d87eaa/script.sh: line 1: sudo: not found










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a command which requires root in JenkinsFile.But when i run it on my localhost it gives error that "sudo not found".The JenkinsFile is in the source code.



      stage('Install packages') {
      sh("docker run --rm -v `pwd`:/app -w /app node yarn install")
      sh("sudo chown -R jenkins: ./node_modules")
      }


      I have already tried this. How to run a script as root in Jenkins? The temp sh script is created on each build.
      And the error is



      /var/jenkins_home/workspace/boilerplate_master-2ESKZDF4PBIXGKODFFKIVAND5RN5VWF6QLEGRZ44LZGESCULVC4Q@tmp/durable-14d87eaa/script.sh: line 1: sudo: not found










      share|improve this question













      I have a command which requires root in JenkinsFile.But when i run it on my localhost it gives error that "sudo not found".The JenkinsFile is in the source code.



      stage('Install packages') {
      sh("docker run --rm -v `pwd`:/app -w /app node yarn install")
      sh("sudo chown -R jenkins: ./node_modules")
      }


      I have already tried this. How to run a script as root in Jenkins? The temp sh script is created on each build.
      And the error is



      /var/jenkins_home/workspace/boilerplate_master-2ESKZDF4PBIXGKODFFKIVAND5RN5VWF6QLEGRZ44LZGESCULVC4Q@tmp/durable-14d87eaa/script.sh: line 1: sudo: not found







      jenkins jenkins-pipeline






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 8 hours ago









      Ricky Sterling

      648




      648
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          ‘sudo’ is not available in every docker image.
          Try checking the ID of a default user. For example on maven image it’s root user( ID 0).



          Also, the user mapping is usually based on the user ID not by name. Example Jenkins user on host may have ID 1000. In the node image, it’s user node.



          sh “chown -R 1000 /mydir”


          Should work, provided Jenkins user on the host also has user ID 1000. Else you will end up with files that has unknown owner.



          I wrote an example Jenkins file with the Docker image that you mentioned. Please try this.



          pipeline{
          agent any
          stages{
          stage('test'){
          steps{
          script{
          def image = docker.image('mhart/alpine-node:8.11.3')
          image.pull()
          image.inside() {
          sh 'id'
          sh 'ls -lrt'
          sh 'node yarn install'
          }
          }
          }
          }
          }
          }





          share|improve this answer























          • Whats the point if writing a JenkinsFile if it doesnt run everywhere? I will have to map user ID to hostID for every system.Is there any workaround?
            – Ricky Sterling
            8 hours ago










          • We use extensively docker in our Jenkins. Only workaround I found was either use docker images that runs with root user( not highly recommended by docker community) or use those images which has a matching user that of host.
            – Ram Kamath
            8 hours ago










          • It returned invalid spec 1001 when i did sh “chown -R 1000 /mydir”.
            – Ricky Sterling
            8 hours ago










          • Which docker image are you using? What’s the user ID of your Jenkins user ?
            – Ram Kamath
            8 hours ago










          • The docker image is mhart/alpine-node:8.11.3 and the userID of the jenkins user is 1001.
            – Ricky Sterling
            8 hours ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203314%2frunning-a-command-as-root-in-jenkinsfile%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote













          ‘sudo’ is not available in every docker image.
          Try checking the ID of a default user. For example on maven image it’s root user( ID 0).



          Also, the user mapping is usually based on the user ID not by name. Example Jenkins user on host may have ID 1000. In the node image, it’s user node.



          sh “chown -R 1000 /mydir”


          Should work, provided Jenkins user on the host also has user ID 1000. Else you will end up with files that has unknown owner.



          I wrote an example Jenkins file with the Docker image that you mentioned. Please try this.



          pipeline{
          agent any
          stages{
          stage('test'){
          steps{
          script{
          def image = docker.image('mhart/alpine-node:8.11.3')
          image.pull()
          image.inside() {
          sh 'id'
          sh 'ls -lrt'
          sh 'node yarn install'
          }
          }
          }
          }
          }
          }





          share|improve this answer























          • Whats the point if writing a JenkinsFile if it doesnt run everywhere? I will have to map user ID to hostID for every system.Is there any workaround?
            – Ricky Sterling
            8 hours ago










          • We use extensively docker in our Jenkins. Only workaround I found was either use docker images that runs with root user( not highly recommended by docker community) or use those images which has a matching user that of host.
            – Ram Kamath
            8 hours ago










          • It returned invalid spec 1001 when i did sh “chown -R 1000 /mydir”.
            – Ricky Sterling
            8 hours ago










          • Which docker image are you using? What’s the user ID of your Jenkins user ?
            – Ram Kamath
            8 hours ago










          • The docker image is mhart/alpine-node:8.11.3 and the userID of the jenkins user is 1001.
            – Ricky Sterling
            8 hours ago















          up vote
          0
          down vote













          ‘sudo’ is not available in every docker image.
          Try checking the ID of a default user. For example on maven image it’s root user( ID 0).



          Also, the user mapping is usually based on the user ID not by name. Example Jenkins user on host may have ID 1000. In the node image, it’s user node.



          sh “chown -R 1000 /mydir”


          Should work, provided Jenkins user on the host also has user ID 1000. Else you will end up with files that has unknown owner.



          I wrote an example Jenkins file with the Docker image that you mentioned. Please try this.



          pipeline{
          agent any
          stages{
          stage('test'){
          steps{
          script{
          def image = docker.image('mhart/alpine-node:8.11.3')
          image.pull()
          image.inside() {
          sh 'id'
          sh 'ls -lrt'
          sh 'node yarn install'
          }
          }
          }
          }
          }
          }





          share|improve this answer























          • Whats the point if writing a JenkinsFile if it doesnt run everywhere? I will have to map user ID to hostID for every system.Is there any workaround?
            – Ricky Sterling
            8 hours ago










          • We use extensively docker in our Jenkins. Only workaround I found was either use docker images that runs with root user( not highly recommended by docker community) or use those images which has a matching user that of host.
            – Ram Kamath
            8 hours ago










          • It returned invalid spec 1001 when i did sh “chown -R 1000 /mydir”.
            – Ricky Sterling
            8 hours ago










          • Which docker image are you using? What’s the user ID of your Jenkins user ?
            – Ram Kamath
            8 hours ago










          • The docker image is mhart/alpine-node:8.11.3 and the userID of the jenkins user is 1001.
            – Ricky Sterling
            8 hours ago













          up vote
          0
          down vote










          up vote
          0
          down vote









          ‘sudo’ is not available in every docker image.
          Try checking the ID of a default user. For example on maven image it’s root user( ID 0).



          Also, the user mapping is usually based on the user ID not by name. Example Jenkins user on host may have ID 1000. In the node image, it’s user node.



          sh “chown -R 1000 /mydir”


          Should work, provided Jenkins user on the host also has user ID 1000. Else you will end up with files that has unknown owner.



          I wrote an example Jenkins file with the Docker image that you mentioned. Please try this.



          pipeline{
          agent any
          stages{
          stage('test'){
          steps{
          script{
          def image = docker.image('mhart/alpine-node:8.11.3')
          image.pull()
          image.inside() {
          sh 'id'
          sh 'ls -lrt'
          sh 'node yarn install'
          }
          }
          }
          }
          }
          }





          share|improve this answer














          ‘sudo’ is not available in every docker image.
          Try checking the ID of a default user. For example on maven image it’s root user( ID 0).



          Also, the user mapping is usually based on the user ID not by name. Example Jenkins user on host may have ID 1000. In the node image, it’s user node.



          sh “chown -R 1000 /mydir”


          Should work, provided Jenkins user on the host also has user ID 1000. Else you will end up with files that has unknown owner.



          I wrote an example Jenkins file with the Docker image that you mentioned. Please try this.



          pipeline{
          agent any
          stages{
          stage('test'){
          steps{
          script{
          def image = docker.image('mhart/alpine-node:8.11.3')
          image.pull()
          image.inside() {
          sh 'id'
          sh 'ls -lrt'
          sh 'node yarn install'
          }
          }
          }
          }
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 1 hour ago

























          answered 8 hours ago









          Ram Kamath

          437311




          437311












          • Whats the point if writing a JenkinsFile if it doesnt run everywhere? I will have to map user ID to hostID for every system.Is there any workaround?
            – Ricky Sterling
            8 hours ago










          • We use extensively docker in our Jenkins. Only workaround I found was either use docker images that runs with root user( not highly recommended by docker community) or use those images which has a matching user that of host.
            – Ram Kamath
            8 hours ago










          • It returned invalid spec 1001 when i did sh “chown -R 1000 /mydir”.
            – Ricky Sterling
            8 hours ago










          • Which docker image are you using? What’s the user ID of your Jenkins user ?
            – Ram Kamath
            8 hours ago










          • The docker image is mhart/alpine-node:8.11.3 and the userID of the jenkins user is 1001.
            – Ricky Sterling
            8 hours ago


















          • Whats the point if writing a JenkinsFile if it doesnt run everywhere? I will have to map user ID to hostID for every system.Is there any workaround?
            – Ricky Sterling
            8 hours ago










          • We use extensively docker in our Jenkins. Only workaround I found was either use docker images that runs with root user( not highly recommended by docker community) or use those images which has a matching user that of host.
            – Ram Kamath
            8 hours ago










          • It returned invalid spec 1001 when i did sh “chown -R 1000 /mydir”.
            – Ricky Sterling
            8 hours ago










          • Which docker image are you using? What’s the user ID of your Jenkins user ?
            – Ram Kamath
            8 hours ago










          • The docker image is mhart/alpine-node:8.11.3 and the userID of the jenkins user is 1001.
            – Ricky Sterling
            8 hours ago
















          Whats the point if writing a JenkinsFile if it doesnt run everywhere? I will have to map user ID to hostID for every system.Is there any workaround?
          – Ricky Sterling
          8 hours ago




          Whats the point if writing a JenkinsFile if it doesnt run everywhere? I will have to map user ID to hostID for every system.Is there any workaround?
          – Ricky Sterling
          8 hours ago












          We use extensively docker in our Jenkins. Only workaround I found was either use docker images that runs with root user( not highly recommended by docker community) or use those images which has a matching user that of host.
          – Ram Kamath
          8 hours ago




          We use extensively docker in our Jenkins. Only workaround I found was either use docker images that runs with root user( not highly recommended by docker community) or use those images which has a matching user that of host.
          – Ram Kamath
          8 hours ago












          It returned invalid spec 1001 when i did sh “chown -R 1000 /mydir”.
          – Ricky Sterling
          8 hours ago




          It returned invalid spec 1001 when i did sh “chown -R 1000 /mydir”.
          – Ricky Sterling
          8 hours ago












          Which docker image are you using? What’s the user ID of your Jenkins user ?
          – Ram Kamath
          8 hours ago




          Which docker image are you using? What’s the user ID of your Jenkins user ?
          – Ram Kamath
          8 hours ago












          The docker image is mhart/alpine-node:8.11.3 and the userID of the jenkins user is 1001.
          – Ricky Sterling
          8 hours ago




          The docker image is mhart/alpine-node:8.11.3 and the userID of the jenkins user is 1001.
          – Ricky Sterling
          8 hours ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203314%2frunning-a-command-as-root-in-jenkinsfile%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Schultheiß

          Liste der Kulturdenkmale in Wilsdruff

          Android Play Services Check