jenkinsgroovyjenkins-pipelinejenkins-groovy

Jenkins: Using external scripts vs coding the job in Groovy


In a company I'm currently working on, most of our Jenkins jobs are scripted and all they do is call an external script in python or bash, that actually does the heavy work. Something like this:

timestamps {
  stage('deploy-site') {
    sh('python deploy-script.py')
  }
}

I was wondering if this is actually a good practice and if it would be better to code the job steps using Groovy and the Jenkins API.

Any thoughts? What could be the pros and cons of each approach?


Solution

  • I think it depends a lot on what you're using Jenkins for. Jenkins can be used for automating all kinds of things, not just Continuous Integration. I use it for patching servers, applications, and databases, automating Disaster Recovery steps, a full replacement for Cron company-wide, and much more. So in my case there are not a lot of API's within Groovy that handle such things, so my approach is very similar to what you have there. However, I do have a lot of logic built around these processes that is written in Groovy, like knowing where to run things, which server is currently production, which is DR. And I use Groovy functionality for parameters, emailing, moving files etc. And one of the best things is logging. All of my scripts go to STDOUT so that Jenkins captures that and can parse for errors using its log parsing functionality. So even when doing the heavy lifting with shell scripts, Jenkins brings a heck of a lot to enhance that.