[英]How to run shell script in jenkins pipeline
I want to run shell script from jenkins pipeline, instead of calling shell script can I run sh
commands one by one like below我想从 jenkins 管道运行 shell 脚本,而不是调用 shell 脚本,我可以像下面一样一一运行
sh
命令
top -bn1 | grep load | awk '{printf "%.2f%%\t\t\n", $(NF-2)}'
free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }'
iostat -c 3 3 |awk '/^ /{print $4}'|awk '{ printf( "%s ", $1 ); } END { printf( "\n" ); }'
I am new to jenkins but when I try to use it with sh
like below its giving error我是 jenkins 的新手,但是当我尝试将它与
sh
一起使用时,如下所示其给出错误
pipeline {
agent any
stages {
stage('Check System Usage') {
steps {
script {
def CPUUSAGE = sh (script: "top -bn1 | grep load | awk '{printf "%.2f%%\t\t\n", $(NF-2)}'", returnStdout: true).trim() as Integer
def MEMUSAGE = sh (script:free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }'", returnStdout: true).trim() as Integer
def IOWAIT = sh (script:"iostat -c 3 3 |awk '/^ /{print $4}'|awk '{ printf( "%s ", $1 ); } END { printf( "\n" ); }'", returnStdout: true).trim() as Integer
println("CPUUSAGE = ${CPUUSAGE}","MEMUSAGE = ${MEMUSAGE}","IOWAIT = {IOWAIT}")
}
}
}
}
}
Can you guide what is the exact format to run shell command in jenkins file, or can you share sample code for reference.您能否指导在 jenkins 文件中运行 shell 命令的确切格式是什么,或者您可以共享示例代码以供参考。
您正在使用 "" 和 '' 不正确。
Reading https://www.jenkins.io/doc/book/pipeline/ and https://code-examples.net/en/q/237ec14 , I would try:阅读https://www.jenkins.io/doc/book/pipeline/和https://code-examples.net/en/q/237ec14 ,我会尝试:
pipeline {
agent any
stages {
stage('Check System Usage') {
steps {
script {
bash'''#!/bin/bash
CPUUSAGE=$(top -bn1 | grep load | awk '{printf "%.2f%%\t\t\n", $(NF-2)}')
MEMUSAGE=$(free -m | awk 'NR==2{printf "%.2f%%\t\t", $3*100/$2 }')
IOWAIT=$(iostat -c 3 3 |awk '/^ /{print $4}'|awk '{ printf( "%s ", $1 ); } END { printf( "\n" ); }')
echo("CPUUSAGE = ${CPUUSAGE}","MEMUSAGE = ${MEMUSAGE}","IOWAIT = {IOWAIT}")
'''
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.