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
jenkins jenkins-pipeline
add a comment |
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
jenkins jenkins-pipeline
add a comment |
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
jenkins jenkins-pipeline
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
jenkins jenkins-pipeline
asked 8 hours ago
Ricky Sterling
648
648
add a comment |
add a comment |
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'
}
}
}
}
}
}
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 spec1001
when i didsh “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 ismhart/alpine-node:8.11.3
and the userID of the jenkins user is 1001.
– Ricky Sterling
8 hours ago
|
show 1 more comment
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'
}
}
}
}
}
}
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 spec1001
when i didsh “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 ismhart/alpine-node:8.11.3
and the userID of the jenkins user is 1001.
– Ricky Sterling
8 hours ago
|
show 1 more comment
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'
}
}
}
}
}
}
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 spec1001
when i didsh “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 ismhart/alpine-node:8.11.3
and the userID of the jenkins user is 1001.
– Ricky Sterling
8 hours ago
|
show 1 more comment
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'
}
}
}
}
}
}
‘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'
}
}
}
}
}
}
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 spec1001
when i didsh “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 ismhart/alpine-node:8.11.3
and the userID of the jenkins user is 1001.
– Ricky Sterling
8 hours ago
|
show 1 more comment
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 spec1001
when i didsh “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 ismhart/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
|
show 1 more comment
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password