Exporting Environment Variables for Elastic Agent

Still need help?

The Atlassian Community is here for you.

Ask the community


 

Platform Notice: Cloud, Server, and Data Center - This article applies equally to all 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

Solutions

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:

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:

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:

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:

_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.

Last modified on May 17, 2021

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.