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

查看:196
本文介绍了如何在 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 文件大小.

it will extract 4096 - the jenkins_home file size.

值得记住的事情:

  • 使用简单的 bash 脚本,例如 ls -la/var |grep jenkins_home |剪切 -d " " -f5.您上面显示的示例不适用于我的本地 bash 以及 Jenkins 服务器
  • returnStdout: true 参数添加到 sh 步骤,以将您的命令打印到控制台的内容返回.
  • 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天全站免登陆