Sharing configuration information can be done in several ways between attributes, instances, containers and environments.
Below are the different scenarios explained.
Attribute “File scope” lets you choose where a file should be stored.
The following table shows the possible locations:
File scope | Description |
Current instance | Default. The file is stored with the current instance only |
Current container | The file is stored at container level where it can be used by other scripts. Useful for environment variables and data that should be shared with all instances on the current container. Alternative can be the "Container" scope for attributes. |
Current environment | The file is stored at environment level where it can be used by other scripts. Useful for environment variables and data that should be shared with all instances on the current environment. Alternative can be the "Environment" scope if only a few attributes need to be shared. An example would be a JSON-file with the full configuration of Vault, a AWS VPC network, ... |
Every instance | The file will be generated as if it existed in every packet used in the environment. Every instance will get its own version of this file unless 'Skip "Every instance" files' is checked, which can be useful for configuration items that exist many times and that have no actions. File "init_instance.sh" is provided by default and it sources environment- and container-level files. It also handles the module-dependencies of the instance. Be very careful when using this option because it can have an impact on script generation performance in environments with many instances. |
Every container | Same as "Current Container", but the file will also be stored at the container-level for the other containers in the environment. Useful when a container with such an instance is shared with other environments. |
You can include the contents of a file in other files using attribute “Include name”.
Write some Velocity macros and share them.
Specify “include_me” for the attribute “Include name” on a file. Then enter the following anywhere in other files where you want to include the contents of this file:
<INCLUDE include_me>
It can be useful to execute scripts from other instances or to collect files.
The following example is from the Terraform Builder packet. It has Velocity parsing enabled and retrieves all instances underneath it in the instance tree that have at least one Terraform file.
For each instance, it executes “init.sh” if that file exists. In this case “init.sh”-scripts are used to retrieve AMIs for autoscaling groups and EC2 instances.
#foreach ($terraformInstance in $instance.getInstancesByFileExtension(".tf"))
dir="$terraformInstance.getRelativePath()";
cd ../../../${D}dir;
if [ -f "init.sh" ]; then
log_info "Sourcing init.sh in ${D}dir";
. ./init.sh;
fi;
log_info 'Copying .tf and .tpl files from "$terraformInstance.toString()"';
instance_id="$terraformInstance.getId()";
#[[
for f in $(find . -maxdepth 1 -type f -name \*.tf); do
f="$(basename "$f")" # remove ./
log_info "Copying $f as ${instance_id}_$f";
cp $f /tmp/${instance_id}_$f;
done;
for f in $(find . -maxdepth 1 -type f -name \*.tpl); do
f="$(basename "$f")" # remove ./
log_info "Copying $f from ../../../$dir";
cp $f /tmp/;
done;
]]#
#end
Modules can be used by any packet and so can the files within them.
The following table lists the possible attribute scopes:
Scope | Description |
Instance | Default. The attribute is available in the current instance. It's value is automatically stored in an environment variable with the same name when running actions of the instance.
Use it in Velocity through |
Container | The attribute is available to all instances on the container of the instance. Any action under the container can retrieve the value of this attribute through an environment variable with the same name.
Use it in Velocity through |
Environment | The attribute is available to all instances on the container of the instance. Any action under the container can retrieve the value of this attribute through an environment variable with the same name.
Use it in Velocity through |
Any attribute or file can use attributes from any instance in the environment.
There are many ways to retrieve instances. See Apache Velocity for all available methods.
This is the most frequently used method of retrieving data from the environment.
Retrieve the hostname of the parent Jenkins-instance (which can be up multiple level in the instance tree:
$instance.parent.getAttribute("jenkins_hostname")