What does `sh` vs `bat` vs `powershell` do, and what are common `sh` options?
BeginnerConceptJenkins
Answer
sh runs a shell script on Unix agents, bat on Windows cmd, and powershell/pwsh for PowerShell. sh defaults to -xe, so commands are echoed and the script stops at the first failing command. Useful options are returnStdout: true to capture output, returnStatus: true to get the exit code without failing the build, and label to name the step in the UI.
def sha = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
More on Jenkins
What is Jenkins and how is its architecture organised?BeginnerWhat are executors, nodes and labels in Jenkins?IntermediateWhy should you not run builds on the Jenkins controller?BeginnerHow do inbound (JNLP/WebSocket) and SSH agents connect to the controller?IntermediateWhat is a Jenkinsfile and where does it live?BeginnerDeclarative versus Scripted Pipeline: what are the differences?Intermediate