|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "CCJPE: Jenkins Templates" |
| 4 | +date: 2017-04-22 12:00:00 -0500 |
| 5 | +categories: Jenkins |
| 6 | +permalink: lessons/jenkins-templates |
| 7 | +excerpt: "Use templates to simplify job creation!" |
| 8 | +weight: 15 |
| 9 | +image: 'jenkinscourse.png' |
| 10 | +difficulty: easy |
| 11 | + |
| 12 | +--- |
| 13 | +{% include youtube.html id="26th8achUYw" %} |
| 14 | +{% include hired2.html %} |
| 15 | + |
| 16 | +1. Table of Contents |
| 17 | +{:title="Table of Contents"} |
| 18 | +{:toc} |
| 19 | + |
| 20 | +Cloud Config File Downloads |
| 21 | +--------------------------- |
| 22 | +* [Jenkins Master01](https://www.devopslibrary.com/scripts/master01.yaml) |
| 23 | + |
| 24 | +Introduction |
| 25 | +------------ |
| 26 | +Welcome to the DevOps Library! This is Samantha, and in today's episode we're |
| 27 | +going to cover how to use Jenkins templates. Unfortunately, the template plugin |
| 28 | +itself IS only available to Jenkins enterprise users, but if that's an option |
| 29 | +for you it does make it much easier to simplify Jenkins job creation. |
| 30 | + |
| 31 | +One common use case is when you have a bunch of nearly identical jobs with a |
| 32 | +couple of minor differences. Let's say one day you need to modify something on |
| 33 | +all of them. Normally you'd have to edit each job individually, but if they |
| 34 | +were built using the same template, you could simply modify it and every job, |
| 35 | +builder, or folder based on the template would update instantly! |
| 36 | + |
| 37 | +In some cases Jenkins Job builder is a better option, but the template plugin |
| 38 | +excels if your goal is to make it easier to create jobs. You can also |
| 39 | +look at it as a way to implement new functionality that would typically require |
| 40 | +writing a plugin, but without the need to do any actual coding. |
| 41 | + |
| 42 | +Don't worry, by the end of this lesson you'll know how to do that and more, but |
| 43 | +first we'd like to give a quick shout out to [Hired.com](hired.com/devopslibrary) |
| 44 | +for being kind enough to sponsor our Jenkins course. |
| 45 | + |
| 46 | +Creating a Template |
| 47 | +---------------------- |
| 48 | +Ok, back to our tutorial. First, let's create a new build step template for |
| 49 | +downloading files. Click "New Item", type "Download" for the name, then select |
| 50 | +"Builder Template" for the item type. Now go ahead and click "ok" to bring up |
| 51 | +the template configuration page. |
| 52 | + |
| 53 | +Before we do anything, let's think about what we want our template to do. |
| 54 | +Since this is a "builder template", it's going to show up on the list of |
| 55 | +builders of any freestyle jobs we set up, right along with "Execute shell script" |
| 56 | +and all the other standard Jenkins builders. For this template, let's make a |
| 57 | +build step that downloads a file to our workspace when given a URL. |
| 58 | + |
| 59 | +It's actually really easy to configure. First, let's add an attribute with an |
| 60 | +id of "url". |
| 61 | + |
| 62 | +An easy way to think of an attribute is as a template parameter. |
| 63 | + |
| 64 | +We'll stick with "Text-field" for the type, then feel free to add an inline help |
| 65 | +description if you'd like. |
| 66 | + |
| 67 | +Next, we need to configure the transformer, which is used to tell Jenkins how |
| 68 | +our builder works. If you're comfortable with groovy, you can do just |
| 69 | +about anything you'd like here, but for our example we'll go with a generated |
| 70 | +shell script. |
| 71 | + |
| 72 | +Type: wget, followed by a dollar sign and the name of our attribute, URL. |
| 73 | +Since the script section is actually a groovy template, Jenkins will populate |
| 74 | +the URL in our transformer with the URL provided by the user in the attribute. |
| 75 | +Pretty cool huh? Alright, go ahead and save the template, it's time to try it out! |
| 76 | + |
| 77 | +Create a new item again, but this time name it whatever you'd like, and |
| 78 | +select "Freestyle project" for the type. Click ok, then scroll down to the |
| 79 | +bottom until you see "Add Build Step". Look at the dropdown, there's our new |
| 80 | +build step we just created! Feel free to type any URL that you'd like then run the |
| 81 | +job. Pretty cool huh? |
| 82 | + |
| 83 | +Summary |
| 84 | +------- |
| 85 | +While I know our example was pretty simple, keep in mind you're not limited to |
| 86 | +templating build steps. You can create auxiliary templates for creating nested |
| 87 | +attribute structures, publisher templates, or even an entire job template. |
| 88 | + |
| 89 | +As we mentioned earlier, one of the best things about using templates is that any |
| 90 | +time you modify one, it'll automatically update anything else built off of it. |
| 91 | +For example, let's say you create a job template for testing... a Chef cookbook. |
| 92 | +A few months later the team wants to add a new linter to every cookbook testing job. |
| 93 | +In this example, we only have to modify a single template and we're done, making it |
| 94 | +super easy to maintain large Jenkins environments as well as being able to share |
| 95 | +best practices between jobs. |
| 96 | + |
| 97 | +Hopefully this lesson has helped you understand when and why the template plugin |
| 98 | +can be helpful, thank you so much for watching! |
| 99 | + |
| 100 | +-------------------- |
| 101 | +[Subscribe to our YouTube channel](https://www.youtube.com/channel/UCOnioSzUZS-ZqsRnf38V2nA?sub_confirmation=1) or follow [DevOpsLibrary on Twitter](https://twitter.com/intent/user?screen_name=devopslibrary). |
| 102 | + |
| 103 | +{% include subscribe.html %} |
0 commit comments