Jenkins Pipeline: Executing a shell script

6/23/2017

I have create a pipeline like below and please note that I have the script files namely- "backup_grafana.sh" and "gitPush.sh" in source code repository where the Jenkinsfile is present. But I am unable to execute the script because of the following error:-

/home/jenkins/workspace/grafana-backup@tmp/durable-52495dad/script.sh: 
line 1: backup_grafana.sh: not found

Please note that I am running jenkins master on kubernetes in a pod. So copying scripts files as suggested by the error is not possible because the pod may be destroyed and recreated dynamically(in this case with a new pod, my scripts will no longer be available in the jenkins master)

pipeline {
agent {
    node {
        label 'jenkins-slave-python2.7'
    }
}
stages {
    stage('Take the grafana backup') {
        steps {
            sh 'backup_grafana.sh'
        }
    }
    stage('Push to the grafana-backup submodule repository') {
        steps {
            sh 'gitPush.sh'
        }
    }
 }
}

Can you please suggest how can I run these scripts in Jenkinsfile? I would like to also mention that I want to run these scripts on a python slave that I have already created finely.

-- Suhas Chikkanna
jenkins
jenkins-pipeline
jenkins-plugins
kubernetes

1 Answer

6/23/2017

If the command 'sh backup_grafana.sh' fails to execute when it actually should have successfully executed, here are two possible solutions.

1) Maybe you need a dot slash in front of those executable commands to tell your shell where they are. if they are not in your $PATH, you need to tell your shell that they can be found in the current directory. here's the fixed Jenkinsfile with four non-whitespace characters added:

pipeline {
  agent {
    node {
      label 'jenkins-slave-python2.7'
    }
  }
  stages {
    stage('Take the grafana backup') {
      steps {
        sh './backup_grafana.sh'
      }
    }
    stage('Push to the grafana-backup submodule repository') {
      steps {
        sh './gitPush.sh'
      }
    }
  }
}

2) Check whether you have declared your file as a bash or sh script by declaring one of the following as the first line in your script:

#!/bin/bash 

or

#!/bin/sh
-- burnettk
Source: StackOverflow