Jenkins流水线:执行shell脚本

5
我创建了以下管道,请注意,我在源代码仓库中有名为“backup_grafana.sh”和“gitPush.sh”的脚本文件,其中包含Jenkinsfile。但是由于以下错误,我无法执行脚本:
/home/jenkins/workspace/grafana-backup@tmp/durable-52495dad/script.sh: 
line 1: backup_grafana.sh: not found

请注意,我正在一个pod中在kubernetes上运行jenkins主服务器。因此,按照错误提示建议的复制脚本文件是不可能的,因为pod可能会被动态地销毁和重建(在这种情况下,使用新的pod时,我的脚本将不再在jenkins主服务器上可用)。
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'
        }
    }
 }
}

您好,您能否建议我如何在Jenkinsfile中运行这些脚本?我还想提一下,我希望在我已经创建好的Python从机上运行这些脚本。

1个回答

4
如果命令“sh backup_grafana.sh”应该成功执行,但实际上未能执行,这里提供两个可能的解决方案。
1)也许你需要在那些可执行命令前加上点斜杠,这样可以告诉你的shell它们的位置。如果它们不在你的 $PATH 中,你需要告诉你的shell它们可以在当前目录中找到。下面是已修复的Jenkinsfile,增加了四个非空格字符:
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) 检查您是否已将文件声明为bash或sh脚本,方法是在脚本的第一行中声明以下之一:

#!/bin/bash 

或者

#!/bin/sh

1
如何在Jenkins中查看Shell脚本内运行命令的输出 - Vineet Sajwan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接