Skip to content

Labels for Jenkins Agents

Sam Gleske edited this page Dec 9, 2017 · 6 revisions

Jenkins allows pinning jobs to specific nodes using labels.

Labels are automatically generated using Jervis based on the .jervis.yml in the example Job DSL script by calling generator.getLabels(). This is documented in the Jervis API documentation.

Here's an example on the platforms.json, lifecycles.json, and toolchains.json files. The following is a .jervis.yml file for a ruby project.

language: ruby

It will generate the following Jenkins node label expression when generator.getLabels() is called.

stable && docker && ubuntu1404 && sudo && language:ruby && gemfile && env && rvm && jdk

Labels for additional toolchains

Sometimes a .jervis.yml file will list additional toolchains beyond the scope of the toolchains for a given language. This is typical for large projects which contain multiple languages. The following .jervis.yml,

language: ruby
node_js: "0.10"

will generate the following label expression when generator.getLabels() is called.

stable && docker && ubuntu1404 && sudo && language:ruby && gemfile && env && rvm && jdk && node_js

Defining custom additional labels

There may be a case where additional labels need to be identified for a given project on top of all of the other generated labels. This may be to pin a project to a node which has a custom label set for it. The following .jervis.yml file,

language: ruby
jenkins:
  additional_labels: "us-east"

will generate the following label expression when generator.getLabels() is called.

stable && docker && ubuntu1404 && sudo && language:ruby && gemfile && env && rvm && jdk && us-east

The .jervis.yml file can even define a list of additional labels.

language: ruby
jenkins:
  additional_labels:
    - "openstack"
    - "us-east"

which generates the following label expression.

stable && docker && ubuntu1404 && sudo && language:ruby && gemfile && env && rvm && jdk && openstack && us-east

Other ways labels can be used

Jenkins labels can be used for more than just tools. additional_labels can be used for things like specific hardware requirements of a build.

For example, let's say you have the following docker hosts and labels.

  • Ubuntu 16.04
    • AWS m4.xlarge (4cpu 16ram). Additional labels: m4_xlarge
    • m4.2xlarge (8cpu 32ram). Additional labels: m4_xlarge, m4_2xlarge
    • m3.2xlarge (8cpu 30ram). Additional labels: m3_2xlarge, ssd_storage

Then you can use label expressions in jobs to get the right host. For example, let's say you have a build which requires m4_xlarge (this job requires at least an m4.xlarge but it's possible to also run on an m4.2xlarge because of the matching labels)

language: ruby
jenkins:
  additional_labels:
    - "m4_xlarge"

This allows one to diversify infrastructure with labels not only with different operating systems or platforms, but also diverse hardware; some agents even physical.