Jenkins Pipeline 可以直接运行系统命令,这种操作通常通过 sh 或 bat 步骤完成,分别适用于 Linux/macOS 和 Windows 环境。接下来给你详细讲讲怎么用。
一、执行系统命令的基本方法
1. Linux/macOS 环境
• 使用 sh 步骤:
pipeline {
agent any
stages {
stage('Run Command') {
steps {
sh 'ls -la' // 执行 Linux 的 ls 命令
}
}
}
}
• 输出类似:
drwxr-xr-x 5 user staff 160 Dec 19 10:20 .
drwxr-xr-x 10 user staff 320 Dec 19 10:00 ..
-rw-r--r-- 1 user staff 123 Dec 19 10:15 README.md
2. Windows 环境
• 使用 bat 步骤:
pipeline {
agent any
stages {
stage('Run Command') {
steps {
bat 'dir' // 执行 Windows 的 dir 命令
}
}
}
}
• 输出类似:
Volume in drive C is OS
Volume Serial Number is XXXX-XXXX
Directory of C:\workspace
**

最低0.47元/天 解锁文章
597

被折叠的 条评论
为什么被折叠?



