How to store Jenkins jobs in your code — Part 1

Jose Manuel Cardona
3 min readApr 22, 2017

--

When we start using Jenkins it offers us a lot of possible jobs to create, that have a concrete purpose.

All jobs available in Jenkins

Usually at the beginning we always start using the “Freestyle Project” that offers us an easy way to build our jobs.

These jobs can be configured easily using the web interface and have all the features that we usually need in any project. Furthermore, you can increase the amount of utilities that they provide you installing some additional plugins.

But this approach has a few drawbacks:

  • It doesn’t allow you to share complete jobs or job parts easily.
  • It doesn’t allow you to version the job.
  • It is bound to a specific Jenkins and move it from one to another it is not so trivial as expected.
  • When it gets more complex, they get a mess and are not readable because the web interface.
  • Anyone with permissions can modify your job without traces.

If you just have a single project or you can live with this drawbacks, you can remain using this approach. By other hand if you want to have your jobs stored safely in your code and have it versioned, lets see how to do it.

Storing Jenkins jobs in your SCM

If we want to store our jobs, we need to tell Jenkins where we are storing them, to do that we need a Pipeline Job.

The Pipeline Job has a Pipeline section where you can tell Jenkins if you want to configure the job using the GUI or using a file from your SCM.

Pipeline configuration using Web GUI
Pipeline Job configuration using SCM

To start the configuration I recommend writing your Pipeline script using the Web GUI to avoid a big amount of commits and pushes. But, How to write this scripts?

The pipeline scripts are based in groovy, so if you know groovy you have a big advantage but, if you don’t know about groovy there is no problem because in every pipeline configuration you have a link to the pipeline syntax where you can build your snippets using GUI and use it later in your script.

Pipeline script generated using the pipeline syntax GUI

In the pipeline syntax you will have a drop down with all the options that you have in that Jenkins. You can have different options depending on the Jenkins if you install some additional plug-in with pipeline support. Play a bit with the different sample steps to know all the possibilities that they offer you.

Conclusions

With this new approach we have the possibility to store our Jenkins jobs in our SCM, doing it more portable to different Jenkins and being versioned. Furthermore, you can isolate parts in libraries to be used in different jobs and simplify the maintenance due to fix the library involves fix all the jobs.

The main drawback in this type of jobs is that you need to learn groovy and can take you more time to create your firsts jobs, but Jenkins help us with the pipeline syntax generator.

So I highly recommend the pipeline job usage if you want to have more reliable jobs.

You can see in the next article part an example about how to use Jenkinsfile to deploy a project using Docker.

--

--