如何在Jenkinsfile中获取Shell脚本的输出? [英] How to get the output of a shell script in a Jenkinsfile?

查看:1363
本文介绍了如何在Jenkinsfile中获取Shell脚本的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Jenkinsfile Groovy脚本阶段,说我想发出一个Linux命令,该命令输出字符串的行和列,并希望在特定行的输出中获得第n列.这样的命令的一个示例是"ls -al".所以我做对了吗?

In a Jenkinsfile Groovy script stage, say I want to issue a linux command that outputs rows and columns of strings, and want to get the nth column in the output of a certain row. An example of such command is "ls -al." So I do this correct?

stage("Get dir size") {
  sh returnStatus: true, script: '''
    LINE=`ls -al | grep some_dir`
    IFS=" " read -ra COLS <<< $LINE
    echo ${COLS[4]}
  '''
  /* I want to use the value of ${COL[4]} after above block */
}

但是如何获取本质上为$ {COL [4]}的值,该值是"ls -al"命令的第五列,即目录大小?

But how do I get the value of essentially ${COL[4]}, which is the fifth column in an "ls -al" command, which is the directory size?

谢谢!

推荐答案

在示例中显示的此bash脚本不会返回正确的目录大小.它将以递归方式返回文件的大小(通常为4096字节),而不是所有文件和子目录的总大小.如果要获取目录的总大小,可以尝试执行以下操作:

This bash script you have shown in your example won't return correct directory size. It will return a size of a file, usually 4096 bytes and not a total size of all files and subdirectories recursively. If you want to get the total directory size you can try something like this:

#!groovy

node('master') {

  stage("Get dir size") {
    script {
      DIR_SIZE = sh(returnStdout: true, script: 'du -sb /var/jenkins_home/war/jsbundles | cut -f1')
    }
    echo "dir size = ${DIR_SIZE}"
  }
}

关键部分是在启用returnStdout的情况下使用sh步骤,以便您可以在变量内捕获脚本将输出到控制台的内容.在此示例中,我计算了/var/jenkins_home/war/jsbundles文件夹的总大小,并且在运行此管道脚本时得到:

Crucial part is to use sh step with returnStdout enabled so you can capture inside a variable what the script prints out to the console. In this example I am calculating a total size of /var/jenkins_home/war/jsbundles folder and when I run this pipeline script I get:

dir size = 653136

然后,您可以在以后的流水线步骤中将DIR_SIZE变量用作输入.

You can then use DIR_SIZE variable as an input in later pipeline steps.

您可以考虑使用Groovy的内置方法File.directorySize(),而不是使用bash脚本,例如:

Instead of using a bash script you could consider using Groovy's built-in method File.directorySize(), something like:

#!groovy

node('master') {

  stage("Get dir size") {
    script {
      DIR_SIZE = new File('/var/jenkins_home/war/jsbundles').directorySize()
    }
    echo "dir size = ${DIR_SIZE}"
  }
}

但是,与使用bash命令的用例相比,这种方法将为您提供不同的结果:

However, this approach will give you a different result comparing to the use case with bash command:

dir size = 649040

这是因为Groovy的File.directorySize()方法以递归方式计算所有文件大小之和的结果,并且没有考虑目录文件的大小.在此示例中,差异为4096-目录文件/var/jenkins_home/war/jsbundles的大小(此路径不包含任何子文件夹,仅包含文件堆).

This is because Groovy's File.directorySize() method calculates the result as a sum of all file sizes recursively and it does not take into account size of directory file. In this example the difference is 4096 - a size of a directory file /var/jenkins_home/war/jsbundles (this path does not contain any subfolders, only bunch of files).

您可以通过将grepcut之类的管道命令组合在一起,从类似列的输出中提取任何信息.例如,您可以将以上示例替换为:

You can extract any information from column-like output by piping commands like grep and cut together. For example, you can replace above examples with:

#!groovy

node('master') {
  stage("Get dir size") {
    script {
      DIR_SIZE = sh(returnStdout: true, script: 'ls -la /var | grep jenkins_home | cut -d " " -f5')
    }
    echo "dir size = ${DIR_SIZE}"
  }
}

对于以下输出:

total 60
drwxr-xr-x  1 root    root    4096 Nov  4  2017 .
drwxr-xr-x  1 root    root    4096 May 31 03:27 ..
drwxr-xr-x  1 root    root    4096 Nov  4  2017 cache
dr-xr-xr-x  2 root    root    4096 May  9  2017 empty
drwxr-xr-x  2 root    root    4096 Nov  4  2017 git
drwxrwxr-x 20 jenkins jenkins 4096 May 31 12:26 jenkins_home
drwxr-xr-x  5 root    root    4096 May  9  2017 lib
drwxr-xr-x  2 root    root    4096 May  9  2017 local
drwxr-xr-x  3 root    root    4096 May  9  2017 lock
drwxr-xr-x  2 root    root    4096 May  9  2017 log
drwxr-xr-x  2 root    root    4096 May  9  2017 opt
drwxr-xr-x  2 root    root    4096 May  9  2017 run
drwxr-xr-x  3 root    root    4096 May  9  2017 spool
drwxrwxrwt  2 root    root    4096 May  9  2017 tmp

它将提取4096-jenkins_home文件大小.

值得记住的事情

  • 使用简单的bash脚本,例如ls -la /var | grep jenkins_home | cut -d " " -f5.您上面显示的示例在我的本地bash和Jenkins服务器中均不起作用
  • sh步骤中添加returnStdout: true参数,以将命令打印的内容返回到控制台.
  • use simple bash scripts like ls -la /var | grep jenkins_home | cut -d " " -f5. The example you have shown above does not work in my local bash as well as in the Jenkins server
  • add returnStdout: true parameter to sh step to return what your command prints to the console.

这篇关于如何在Jenkinsfile中获取Shell脚本的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆