class lifecycleGenerator extends Object
Generates the build scripts from the Jervis YAML.
To run this example, clone Jervis and execute ./gradlew console to bring up a Groovy Console with the classpath set up.
Please note, if you are writing Job DSL plugin groovy scripts you should not use the relative file paths to access files in the repository where your DSL scripts reside. Instead, use the readFileFromWorkspace method provided by the Job DSL plugin in Jenkins.
import net.gleske.jervis.lang.lifecycleGenerator String yaml = """ language: ruby env: - hello=world three=four - hello=test three=five rvm: ["1.9.3", "2.1.0", "2.0.0"] jdk: openjdk8 matrix: exclude: - env: hello=world three=four rvm: 2.1.0 - rvm: 1.9.3 include: - rvm: 2.1.0 - rvm: 2.0.0 env: hello=test three=five jenkins: sudo: false """ def generator = new lifecycleGenerator() generator.loadPlatforms('resources/platforms.json') generator.preloadYamlString(yaml) //os_stability requires preloadYamlString() to be called def os_stability = "${generator.label_os}-${generator.label_stability}" generator.loadLifecycles("resources/lifecycles-${os_stability}.json") generator.loadToolchains("resources/toolchains-${os_stability}.json") generator.loadYamlString(yaml) generator.folder_listing = ['Gemfile.lock'] println 'Exclude filter is...' println generator.matrixExcludeFilter() println 'Matrix axis value for env is...' println generator.matrixGetAxisValue('env') println 'Generating the matrix build script.' println generator.generateAll() print 'Labels: ' println generator.getLabels()
Type | Name and description |
---|---|
List |
cipherlist A list of secrets loaded from the YAML file. |
Map |
ciphermap A map of secrets loaded from the YAML file. |
String |
filter_type Defines the kind of branch filtering which is in effect. |
List |
folder_listing This is a folder listing of the root of the repository so that scripts can be conditionally generated depending on build tool is being used. |
Boolean |
is_pr A variable set by any external system relaying to Jervis that this is a peer review build (commonly called a pull request). |
Boolean |
is_tag A variable set by any external system relaying to Jervis that this is a build associated with a tag. |
Map |
jervis_yaml Contains the Jervis YAML loaded as an object. |
String |
label_os An optional label which is used by the getLabels() function to generate advanced job labels across different operating systems. |
String |
label_platform An optional label which is used by the getLabels() function to generate advanced job labels across different platforms. |
String |
label_stability An optional label which is used by the getLabels() function to generate advanced job labels for agent stability. |
String |
label_sudo An optional label which is used by the getLabels() function to generate advanced job labels for agents with sudo or nosudo access. |
String |
lifecycle_key The value is the key to be looked up in the lifecycles file by default when determining how to generate scripts. |
def |
lifecycle_obj An instance of the lifecycleValidator class which has loaded a lifecycles file. |
Boolean |
matrix_build A variable for determining whether or not this is a matrix build. |
Map |
matrix_fullName_by_friendly A map of friendly key names for matrix steps which can be reference to show a full key name as if the friendly name were not needed. |
List |
plainlist A list of decrypted values from the cipherlist. |
Map |
plainmap A list of decrypted values from the ciphermap. |
def |
platform_obj An instance of the platformValidator class which as loaded a platforms file. |
def |
secret_util A utility for decrypting RSA encrypted strings in YAML files. |
def |
toolchain_obj An instance of the toolchainValidator class which has loaded a toolchains file. |
String[] |
yaml_keys A quick access variable for what root keys are in the loaded Jervis YAML. |
String |
yaml_language A quick access variable for what language is selected for the loaded Jervis YAML. |
List |
yaml_matrix_axes A quick access variable for matrix build axes. |
Type Params | Return Type | Name and description |
---|---|---|
|
void |
decryptSecrets() Decrypts encrypted values stored in cipherlist. |
|
String |
generateAll() Generate the build script which would be used in the Jenkins step. |
|
String |
generateSection(String section) A generic function to generate code for different sections of the build script. |
|
String |
generateToolchainSection() Generate the toolchains shell script based on the Jervis YAML or taking defaults from the toolchains file. |
|
String |
getBranchRegexString() Get the regular expression which is used for branch filtering. |
|
List |
getFilteredBranchesList() Get a list of static branch names which is used for branch filtering. |
|
String |
getFullBranchRegexString(List branches) Get the regular expression for all whitelisted branches. |
|
String |
getJenkinsfile() Get the Jenkinsfile for a pipeline. |
|
String |
getLabels() Returns a groovy expression which Jenkins would use to pin a job to specific nodes. |
|
Map |
getMatrix_fullName_by_friendly() |
|
static Object |
getObjectValue(Map object, String key, Object defaultValue) Get an object from a Map or return any object from defaultValue. |
|
Boolean |
hasRegexFilter() This method makes it easy to detect if regular expressions are defined for branch filtering. |
|
Boolean |
isFilteredByRegex(String branch) Detect if this branch is already covered by a regex filter. |
|
Boolean |
isGenerateBranch(String branch) Is this a branch which will generate a job? |
|
static boolean |
isInstanceFromList(Object object, List<Class> list) Check if an object is an instance of any of the classes in a list. |
|
Boolean |
isMatrixBuild() This will check if the loaded YAML is a matrix build. |
|
boolean |
isPipelineJob() Determine if this instance is compatible with pipeline multibranch jobs. |
|
boolean |
isRestricted(String project) Given a project combination of org/project determine if a project is allowed to be generated for a platform combination. |
|
boolean |
isSupportedPlatform() Checks if a job that is using a platform and toolchain which is supported in the loaded platforms file. |
|
void |
loadLifecycles(String file) Load a lifecycles file so that default scripts can be generated. |
|
void |
loadLifecyclesString(String json) Load a lifecycles JSON String so that default scripts can be generated. |
|
void |
loadPlatforms(String file) Load a platforms file so that advanced labels can be generated for multiple platforms. |
|
void |
loadPlatformsString(String json) Load a platforms JSON String so that advanced labels can be generated for multiple platforms. |
|
void |
loadToolchains(String file) Load a toolchains file so that default scripts can be generated. |
|
void |
loadToolchainsString(String json) Load a toolchains JSON String so that default scripts can be generated. |
|
void |
loadYamlString(String raw_yaml) Load Jervis YAML to be interpreted. |
|
String |
matrixExcludeFilter() This function generates a Jenkins groovy expression from Jervis YAML which will be used to exclude matrix build combinations. |
|
String |
matrixGetAxisValue(String axis) Get a value for a given axis for matrix building. |
|
void |
preloadYamlString(String raw_yaml) Preload Jervis YAML for the purpose of loading lifecycles files for other platforms and operating systems. |
|
void |
setFolder_listing(List listing) This function sets the folder_listing and based on the listing conditionally sets lifecycle_key. |
|
void |
setLabel_stability(String stability) This function sets the label_stability property. |
|
void |
setLabel_sudo(String sudo) This function sets the label_sudo property. |
|
void |
setPrivateKey(String pem) Load an RSA private key in-memory rather than depending on a file existing on disk. |
A list of secrets loaded from the YAML file.
A map of secrets loaded from the YAML file.
Defines the kind of branch filtering which is in effect. Possible values: empty string, only, except. only is used for whitelist filtering and except is used for blacklist filtering.
This is a folder listing of the root of the repository so that scripts can be conditionally generated depending on build tool is being used. This way we can do neat things like generate different script output depending on if there's a build.gradle, pom.xml, or build.xml. loadYamlString(java.lang.String) should be called before this.
A variable set by any external system relaying to Jervis that this is a peer review build (commonly called a pull request).
A variable set by any external system relaying to Jervis that this is a build associated with a tag.
Contains the Jervis YAML loaded as an object.
An optional label which is used by the getLabels() function to generate advanced job labels across different operating systems. This is populated by preloadYamlString(java.lang.String).
An optional label which is used by the getLabels() function to generate advanced job labels across different platforms. This is populated by preloadYamlString(java.lang.String).
An optional label which is used by the getLabels() function to generate advanced job labels for agent stability. This is populated by preloadYamlString(java.lang.String). The value is always stable or unstable.
An optional label which is used by the getLabels() function to generate advanced job labels for agents with sudo or nosudo access. This is populated by preloadYamlString(java.lang.String). The value is always sudo or nosudo.
The value is the key to be looked up in the lifecycles file by default when determining how to generate scripts. This is set when a list of files is set in the folder_listing.
An instance of the lifecycleValidator class which has loaded a lifecycles file.
A variable for determining whether or not this is a matrix build.
A map of friendly key names for matrix steps which can be reference to show a full key name as if the friendly name were not needed. Basically, it allows the unfriendly name to be accessible via the friendly name.
A list of decrypted values from the cipherlist.
An instance of the platformValidator class which as loaded a platforms file.
A utility for decrypting RSA encrypted strings in YAML files. This is an instance of the securityIO.
An instance of the toolchainValidator class which has loaded a toolchains file.
A quick access variable for what root keys are in the loaded Jervis YAML.
A quick access variable for what language is selected for the loaded Jervis YAML.
A quick access variable for matrix build axes.
Decrypts encrypted values stored in cipherlist. All decrypted values will be stored in plainlist which is where they should be accessed. Any malformatted Hash Maps within cipherlist which don't have a secret and key item key in the HashMap will simply be ignored. Decryption errors will throw an exception.
Generate the build script which would be used in the Jenkins step. This function combines the output of: generateToolchainSection(), generateBeforeInstall(), generateInstall(), generateBeforeScript() , and generateScript().
A generic function to generate code for different sections of the build script. Typical sections include: before_install, install, before_script, script
section
- A section from the build lifecycle. e.g. before_install, install, before_script, script, etc.Generate the toolchains shell script based on the Jervis YAML or taking defaults from the toolchains file.
Get the regular expression which is used for branch filtering.
Get a list of static branch names which is used for branch filtering. This is a simple literal list of Strings.
Get the regular expression for all whitelisted branches. This is the combination of which is the combination of getBranchRegexString() and branches into one regex to cover all cases.
branches
- A list of branches.Get the Jenkinsfile for a pipeline.
Returns a groovy expression which Jenkins would use to pin a job to specific nodes.
Get an object from a Map or return any object from defaultValue. Guarantees that what is returned is the same type as defaultValue. This is used to get optional keys from YAML or JSON files.
object
- A Map which was likely created from a YAML or JSON file.key
- A String with keys and subkeys separated by periods which is
used to search the object for a possible value.defaultValue
- A default value and type that should be returned.This method makes it easy to detect if regular expressions are defined for branch filtering.
Detect if this branch is already covered by a regex filter.
branch
- The name of a branch to be compared with the regex.Is this a branch which will generate a job?
branch
- A branch to check if the job should be generated.Check if an object is an instance of any of the classes in a list.
object
- Any kind of object.list
- A list of classes deriving from type Class.This will check if the loaded YAML is a matrix build. The requirements for it to be a matrix build is that it must be a matrix specifically for the selected language and the array for the section must be greater than 1.
For example the following YAML would not produce a matrix build.
language: groovy env: foo=bar
language: groovy env: - foo=bar
However, the following YAML will produce a matrix build. This assumes that matrix: disabled is not set for env in the toolchains file.
language: groovy env: - foobar=foo - foobar=bar
Determine if this instance is compatible with pipeline multibranch jobs.
Given a project combination of org/project determine if a project is allowed to be generated for a platform combination. The platform combination is determined by label_platform and label_os. A Jenkins job should not be generated if it is restricted via the restrictions section of the platforms file.
Checks if a job that is using a platform and toolchain which is supported in the loaded platforms file.
Load a lifecycles file so that default scripts can be generated. Lifecycles provide the build portions of the script. This project comes with a lifecycles file. The lifecycles file in this repository relative to the repository root is /src/main/resources/lifecycles.json.
file
- A path to a lifecycles file.Load a lifecycles JSON String so that default scripts can be generated. Lifecycles provide the build portions of the script. This project comes with a lifecycles file. The lifecycles file in this repository relative to the repository root is /src/main/resources/lifecycles.json.
json
- A String containing JSON which is from a lifecycles file.Load a platforms file so that advanced labels can be generated for multiple platforms. A platform could be a local datacenter or a cloud providor. The platforms file allows labels to be generated which include stability, sudo access, and even operating system. This could be used to load lifecycles and toolchains by platform and OS. This project comes with a platforms file. The platforms file in this repository relative to the repository root is /src/main/resources/platforms.json.
file
- A path to a platforms file.Load a platforms JSON String so that advanced labels can be generated for multiple platforms. A platform could be a local datacenter or a cloud providor. The platforms file allows labels to be generated which include stability, sudo access, and even operating system. This could be used to load lifecycles and toolchains by platform and OS. This project comes with a platforms file. The platforms file in this repository relative to the repository root is /src/main/resources/platforms.json.
json
- A String containing JSON which is from a platforms file.Load a toolchains file so that default scripts can be generated. Toolchains provide the default tool setup of the script (e.g. what version of Java will be used). This project comes with a toolchains file. The toolchains file in this repository relative to the repository root is /src/main/resources/toolchains.json.
file
- A path to a toolchains file.Load a toolchains JSON String so that default scripts can be generated. Toolchains provide the default tool setup of the script (e.g. what version of Java will be used). This project comes with a toolchains file. The toolchains file in this repository relative to the repository root is /src/main/resources/toolchains.json.
json
- A String containing JSON which is from a toolchains file.Load Jervis YAML to be interpreted. This YAML will be used to generate the build scripts and components of a Jenkins job. Please note: you must call loadToolchains(java.lang.String) and loadLifecycles(java.lang.String) before calling this function.
raw_yaml
- A String which contains Jervis YAML to be parsed.This function generates a Jenkins groovy expression from Jervis YAML which will be used to exclude matrix build combinations. This is typically done from a matrix exclusion in the form of a whitelist or blacklist.
Get a value for a given axis for matrix building. Each matrix job has multiple axes. This function is designed to return the value of the axis if given a name.
axis
- A matrix building axis. e.g. envPreload Jervis YAML for the purpose of loading lifecycles files for other platforms and operating systems. Please note: you must call loadPlatformsString(java.lang.String) or loadPlatforms(java.lang.String) before calling this function.
raw_yaml
- A String which contains Jervis YAML to be parsed.This function sets the folder_listing and based on the listing conditionally sets lifecycle_key. This uses the fileExistsCondition and fallbackKey from the lifecycles file to determine the contents of lifecycle_key. loadYamlString(java.lang.String) should be called before this.
listing
- An List which is a list of files from a directory
path in a repository.This function sets the label_stability property. It forces the value to always be stable or unstable.
stability
- A String which determines the stability. A value of
unstable or true will set the property as
unstable. All other values set the property as
stable.This function sets the label_sudo property. It forces the value to always be sudo or nosudo.
sudo
- A String which determines sudo access required. A value of
sudo or true will set the property as
sudo. All other values set the property as
nosudo.Load an RSA private key in-memory rather than depending on a file existing on disk. This is required to decrypt secrets in a YAML file. Call this function before decryptSecrets() if expecting encrypted YAML keys.
pem
- A String whose contents is an X.509 PEM encoded RSA
private key.