如何从TeamCity服务器修改构建代理工作目录至其他路径?
Looks like your Autodeploy.bat script is hitting a snag when trying to locate zip files in the target directory. Let's break down the issue and fix it step by step.
First, let's recap the execution log you shared:
Starting:C:\AUTOEBCDeployment\Autodeploy.bat in directory: D:\ProgramData\JetBrains\TeamCity BuildAgent\work\8a77def6c3e16d10
D:\ProgramData\JetBrains\TeamCity BuildAgent\work\8a77def6c3e16d10>cd C:\AUTOEBCDeployment\dist
D:\ProgramData\JetBrains\TeamCity BuildAgent\work\8a77def6c3e16d10>for /F "tokens=*" %a in ('dir /b /a-d *.zip') do set DIST_FILE=%a
File Not Found
This error typically means one of two things: either the target directory has no .zip files, or the script didn't actually switch to the intended directory before running the dir command. Here's how to diagnose and fix each scenario:
1. Verify the target directory has zip files and exists
Log into the TeamCity build agent server manually and check C:\AUTOEBCDeployment\dist:
- Confirm the directory itself exists (if not, create it as part of your deployment pipeline or manually)
- Check if there are any
.zipfiles present. If not, trace back your build process to make sure the artifacts are being generated and copied to this location correctly.
2. Ensure the cd command actually succeeds
The log shows the cd command was executed, but it doesn't tell us if it worked. Add error checking to your batch script to catch directory switching failures:
cd C:\AUTOEBCDeployment\dist if %errorlevel% neq 0 ( echo ERROR: Failed to switch to directory C:\AUTOEBCDeployment\dist exit /b 1 )
This will immediately exit the script with a clear error message if the directory doesn't exist or the agent lacks permissions to access it.
3. Use an absolute path in the dir command
Avoid relying on the current working directory entirely. Modify your for loop to target the zip files directly with a full path:
for /F "tokens=*" %%a in ('dir /b /a-d "C:\AUTOEBCDeployment\dist\*.zip"') do set DIST_FILE=%%a
(Note: In batch files, you need to use double percent signs %%a instead of single %a for loop variables.)
4. Check TeamCity agent permissions
Make sure the Windows service account running the TeamCity build agent has read access to C:\AUTOEBCDeployment\dist. Sometimes restricted service accounts can't access certain directories, even if they exist. You can test this by running the batch script manually under the same service account to replicate the issue.
内容的提问来源于stack exchange,提问作者Ayesha

