Passing environment variables when using SSH exec build step

Answered

Hello everyone,

I am running multiple SSH Exec build steps after each other which all have the need to access some shared information. I aimed at providing this information via the teamcity feature "Parameters -> Environment Variables". I defined a variable named "env.product" with its value "product_a". I disabled all my build steps and created a new one which only executes "printenv" on the linux target. The result in the log is:

Executing commands:
printenv
on host [10.1.1.81]
SSH_CONNECTION=10.1.1.80 64407 10.1.1.81 22
LANG=en_US.UTF-8
XDG_SESSION_ID=34
USER=teamcity
PWD=/home/teamcity
HOME=/home/teamcity
SSH_CLIENT=10.1.1.80 64407 22
MAIL=/var/mail/teamcity
SHELL=/bin/bash
SHLVL=1
LANGUAGE=en_US:en
LOGNAME=teamcity
XDG_RUNTIME_DIR=/run/user/1001
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

as you can see. the env variable is not included. Am I probably missunderstanding the scope of where this variable can be used in? Does it probably not work for the SSH Exec build step?

Greetings,

Mango

1
3 comments
Avatar
Permanently deleted user

I developed a workaround so far. My first build step is now

declare -a env=(
'GIT_REPO_PATH:/var/built/somerepo'
'BRANCH_NAME:feature/something'
'PRODUCT_NAME:someproduct'
)

for item in "${env[@]}"; do
readarray -d : -t strarr <<< "$item"
if cat /etc/environment | grep "${strarr[0]}" > /dev/null ; then
sudo sed -i "s#${strarr[0]}=\".*\"#${strarr[0]}=\"${strarr[1]%$'\n'}\"#g" /etc/environment
else
sudo echo "${strarr[0]}=\"${strarr[1]%$'\n'}\"" | sudo tee -a /etc/environment
fi
done

This code will set environment variables in /etc/environment and therefore they are availiable for all further steps. If a env variable already exists, its value is updated. There are some risks and shortcomings with this method e.g. environment variables stay forever on the system and sed could fail in specific cases, but right now its okay for me. Would be glad though if I could set them via Teamcity.


1

Hi,

As I understood, you are checking the environment on the server to which you SSH with the SSH Exec build step. The env.parametername sets the $parameternane environment variable on the build agent though. It is not supposed to set the environment on the server at which the SSH command in the build step is targeted.

I can see you've already found a workaround, but anyway I hope my explanation helps to understand the cause of the issue better.

Cheers,
-Anatoly  

Register for TeamCity Technology Day - Nov 5th 

1
Avatar
Permanently deleted user

Yes, thanks @Anatoly. My next steps will be to make the server I currently ssh into a build agent. That should solve the issue completely. 

3

Please sign in to leave a comment.