2
votes

I'm setting up TeamCity server and agents. The agents are running in docker containers (windows server core), and I define various environment variables to be set on the agent containers, which ultimately get set as Windows env vars. Some are passed in via the docker-compose, others are set in my agent Dockerfile when building the image, using RUN SETX /M NAME VALUE. When I open a console on the agent and type SET in cmd I can see them.

However when I look at the agents in the TeamCity server portal, and look in their Agent Parameters -> Environment Variables - I don't see any of the OS environment variables I defined. It looks like the PATH variable is passed in, but not any others.

How do I pass in specific environment variables to my agent build configurations?

1

1 Answers

2
votes

Unfortunately the only way I have found is listed here

to put the following in the buildAgent.properties file, like this:

env.MYVAR=%MYVAR%
env.MYVAR2=%MYVAR2%

The TeamCity Agent image is based on windows nanoserver, which isn't suitable for my needs so I have to create the buildagent.properties file myself (Not sure exactly how you would edit an existing file created automatically). To do so I'm using the following batch script, which I run as the entrypoint for my agent's docker container:

@echo off
::------------------------------
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
set DTStamp=%YYYY%-%MM%-%DD%_%HH%-%Min%
::------------------------------

echo ##############################################################
echo Run-Agent.bat: %DTStamp%
echo ##############################################################

cd C:\BuildAgent\conf\
IF NOT EXIST buildagent.properties echo buildagent.properties not found - Creating buildagent.properties... && (
echo serverUrl=%SERVER_URL%
echo name=%AGENT_NAME%
echo workDir=../work
echo tempDir=../temp
echo systemDir=../system
echo authorizationToken=%AGENT_TOKEN%
echo env.MYVAR1=%MYVAR1%
echo env.MYVAR2=%MYVAR2%
echo env.MYVAR3=%MYVAR3%
) > buildagent.properties

echo running TeamCity Build Agent in background...
cd C:\BuildAgent\bin\
start /b service.start.bat

cd \
C:\Windows\System32\cmd.exe
@echo on

This seems to be working for me.