Some tips that may help you with Jenkins on Pipeline-as-Code

Bianca Gotaski
4 min readJan 31, 2022

--

I’ve been working with Jenkins since the beginning of 2020 and I've learned a lot about it. So I would like to share some tips — and tricks — that may help beginners with this tool.

A funny Jenkins logo

As you may already know, Jenkins is an open-source automation server. This means that it should be used to automate most of the processes of the life cycle's software. With that said, let’s get started with the tips.

1. Plugins

Plugins are the basics when you are starting with Jenkins. You will notice how much it helps most of the time when you need to connect your Jenkins server with other services or servers. On the other hand, unfortunately, there are a lot of plugins that are out of date because the community doesn’t look through them anymore. I faced this problem many times and I had to look for other alternatives to achieve what I needed. So the next topic is one of the tricks that helped me the most.

2. Formatted HTML

This is a really great alternative when you want dynamically update the Jenkins UI to execute some jobs. Let’s say you need to create parameters dynamically, so you would check out the Active Choices plugin, right? But it’s kind of limited when you need to add some parameters on the interface, interacting with the person who will trigger the build. For example, if a parameter button is hitten then it will create another field (another parameter) on the screen and you can use that information in your pipeline.

It would be like in the sample below:

Screenshot with html code on the parameters section on pipeline configuration
Source: jenkins.io

As you can see, Param1 is based on Param2. You can use both in your pipeline as you need.

3. Pipeline Syntax

Are you not familiar with how pipelines are written? Jenkins has an option where you can select the parameters for your code and it will generate a sample for you!

Screenshot exhibiting the snippet generation
Source: jenkins.io

Just select what action you need to perform in that piece of code for your pipeline, and Jenkins will provide it to you.

4. The creation of parameters

This can be definitely done by your pipeline or from the web console. The main difference between them is that once you generate the parameters from Pipeline, right after its creation you’ll need to execute the build twice. Because at first, pressing the button 'build’, will run your Jenkinsfile and bring all the parameters to the interface. Then, once it’s done, the second time you perform the build, you’ll see all the parameters there and the pipeline can be executed successfully.

However, if you choose to do it from the web console directly, just choose the option 'This project is parameterized' on the configuration window, then you can create all the parameters according to the types you need.

5. Executing shell commands directly from the pipeline

Keep in mind that you’ll be managing the software’s deployment flow, so you’ll need to execute shell commands very often to connect through some servers and take some action during your build/deployment, especially if it’s a distributed system.

peace of code using Visual Studio Code, written the command "ls -a"

Or you can also do it in this way if you are using the Freestyle project type:

Screenshot of Jenkins setup with the command "ls -a"

6. Known bug

You might need to perform a loop inside of a loop (for … for). If doing it by using the most common syntax doesn't work:

// prints 'a', 'b' and 'c'
list = ['a', 'b', 'c']
for (i in list) {
print(i)
}

Try using this instead:

// prints 'a', 'b' and 'c'
list = ['a', 'b', 'c']
list.each { item ->
print("${item}")
}

This is a known issue on Jenkins. Although it seems to be resolved here, I tried in 2020, and it didn't work. Notice that the bug was solved in 2017. So, just in case you need this alternative, it's very useful :).

I hope you liked this post and the content is relevant to you. Do you have any other tips you can share? Let me know about it in the comment section below!

See you next time!

--

--

Bianca Gotaski

Hi! I'm a software developer. I love to share knowledge, experiences, and everything related to computational things :)