Exporting Environment Variables for Elastic Agent

Platform Notice: Cloud and Data Center - This article applies equally to both cloud and data center platforms.

Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

It may be necessary to export environment variables that the elastic agent will pick up when starting so that such environment variables can be used in Bamboo jobs. Since an agent forks OS processes the traditional approach of adding exports to bashrc or bash_profile does not work. This article outlines are few possible solutions.

Environment

Bamboo 7.x

Solution

Both solutions imply building a custom AMI since changes will be made to either environment files or elastic agent startup scripts.

Add exports to /etc/environment 

This is the simplest solution that works well for cases when static environment variables need to be exported. Simply add env var declaration in /etc/environment and save the image. By default /etc/environment has just PATH variable. Add as many new ones as you need:

1 2 PATH="/usr/local/bin:..." FOO="BAR"

Modify elastic Bamboo startup scripts

If a more flexible solution is required, it is possible to modify elastic Bamboo startup scripts to execute scripts that will export environment variables before calling agent startup function. The example below does not present instructions to follow but rather outlines the approach.

When the EC2 instance starts, a series of scripts in /opt/bamboo-elastic-agent is executed. Namely, /opt/bamboo-elastic-agent/etc/rc.local defines what happens automatically when the instance starts. It is /opt/bamboo-elastic-agent/bin/bamboo-elastic-agent that calls auxiliary scripts and eventually executes agent jar. This is where it's possible to add execution of a custom script (or a couple) that will export environment variables.

The last line in /opt/bamboo-elastic-agent/bin/bamboo-elastic-agent is calling a startAgent function. Let's add a few lines of bash to source all scripts in /opt/bamboo-elastic-agent/etc/startup.d:

1 2 3 4 5 6 7 8 9 10 if [ -d $bambooAgentBin/../etc/startup.d ] then logger -p local0.info "ELASTIC-AGENT: Loading custom environment" bambooAgentStartup=$(cd -P -- $bambooAgentBin/../etc/startup.d && pwd) for script in $(ls $bambooAgentStartup) do . $bambooAgentStartup/$script done fi startAgent ${bootstrapScriptFile} 2>&1 | tee -a ${logFile}

There's one change that's required in startAgent though. $startupScript needs to be called with bash rather than sh (which is the default) to preserve the exported function:

1 2 3 4 5 6 7 8 startAgent() { local startupScript=$1 # MARKER: you can modify the bootstrap script file here cat $startupScript bash $startupScript }

Here's an example of /opt/bamboo-elastic-agent/etc/startup.d/load-nvm.sh:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 _home=$HOME [ -z "$_home" ] && _home=/home/bamboo export NVM_DIR="$_home/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion # Export all NVM functions for func in $(declare -F | grep nvm_ | cut -d ' ' -f 3) do export -f $func done # Export nvm itself export -f nvm

Similarly, it is possible to drop such scripts to /opt/bamboo-elastic-agent/etc/startup.d and run exports there. The above example is for nvm.

Updated on February 24, 2025

Still need help?

The Atlassian Community is here for you.