0

I have a strange situation where the shell command "sudo su" is not working as expected in Jenkins Declarative pipeline. Below is my code:

 stages {

     stage('Ansible-dynamichost-generation') {



           steps {
                   ansiColor('xterm') {


                                        sh 'chmod -R 777 /home/jenkins-slave/'
                                        sh 'sudo su ansible'


                                        sh '''#!/bin/bash -l 
                                        #Execute commands
                                         echo "$WORKSPACE"
                                         whoami
                                         '''

                                         }
                                 }
                            }

  }

This pipeline is being run by a jenkins-slave user. However, I have a shell command in pipeline that is supposed to switch to a user called "ansible". But, when the "whoami" command is executed, it returns with an output of "jenkins-slave" itself. Can anyone help as to what is going wrong here ?

rb16
  • 129
  • 4
  • 11

1 Answers1

0

each sh commands are execute in sperate sheel. better write one sh and do the operation it will work

Shubham Gorlewar
  • 404
  • 4
  • 11
  • How does using multiple "sh" cause the above issue ? Can you please elaborate ? And, I also did try calling all shell commands under one "sh" block and the issue still persists. – rb16 Feb 27 '20 at 18:30