Jenkins FAQ Collection (Part 3)

Question 11: How do I trigger other Jenkins jobs from a Pipeline script?

Reference: there is a build step you can look into — it supports both synchronous and asynchronous triggering.

Question 12: Is there a convenient way to see the list of plugins currently installed on Jenkins and their versions? Plugin Manager - Installed shows them, but copying it out gives you extra information that’s awkward to process. For example, each plugin’s summary is included, and when you paste it into a spreadsheet you have to delete them one by one by hand.

Reference: Manage Jenkins - System Information. There’s a plugins section at the very bottom where you can see it, with only three columns: name, version and enabled state.

Question 13: Different branches need different handling actions — how do I write that in a Pipeline?

Reference:

when {
    branch 'xxx'
}
steps {
    echo 'do something'
}

Question 14: How do I use a Pipeline shared library?

Reference: once you’ve written a lot of Pipeline scripts, although every job differs, a great many parts are common or similar, so writing them over and over is pointless. For one thing it’s duplicated labor; for another, because you write it by hand every time, you may write it incorrectly and then have to debug to find out why. A shared library is exactly a way to extract these common methods for each Pipeline job to use. These days many people doing Jenkins-related work don’t come from a development background and have no development experience. This form is in fact similar to the concept of a library in a programming language: you extract some general-purpose methods and turn them into parameterized calls — it belongs to the realm of code reuse. Currently there are two main ways to use a shared library. One is to have the entire contents of the Pipeline generated by the shared library, so that the Jenkinsfile only contains one line calling it; the other is to keep the main framework of the Pipeline written in the Jenkinsfile, with the shared library implementing only some custom individual steps. One more thing: right now most people are just starting out with Pipeline, writing the Jenkinsfile themselves and extracting common methods into a shared library of their own to save trouble, which produces a preconception that “shared libraries always have to be written by yourself.” That’s not the case either. As long as there’s something suitable, other people’s libraries can absolutely be used as well. It’s the same idea as third-party libraries in a programming language. Writing a Pipeline shared library requires knowledge of the Groovy programming language (Pipeline isn’t pure Groovy either — space is limited, so I won’t go into detail). Study hard. Young one, you’re a developer now too!

Question 15: How do I use the post method in a scripted Pipeline?

Reference: post can only be used in a declarative Pipeline. In a scripted Pipeline you use the most primitive try...catch... to catch exceptions. Generally, unless you have special needs, I’d suggest using a scripted Pipeline — the barrier to entry is a bit lower.

This article was first published on my WeChat official account “左手编程右手文化” (Coding Left, Culture Right), account ID: devopscd. Welcome to follow.

Author: Tang Moying

Source: /en/post/jenkins-faq-three

License: this article is shared under "CC BY 4.0". Please credit the author and link back to the original when republishing.