2

I'd like to make it so that TeamCity will deploy to one folder for builds from master and another folder for builds from anything else. After some googling, I ran across this article: TeamCity, passing an id generated in one build step to a later build step

Unfortunately, it doesn't seem to be working for me and I was hoping someone would tell me why.

enter image description here

And then my powershell script looks like this:

if ("%teamcity.build.branch%".CompareTo("refs/heads/master")) {
    Write-Host "##teamcity[setParameter name='artifactory_deploy_dir' value='test1']"
} else {
    Write-Host "##teamcity[setParameter name='artifactory_deploy_dir' value='test2']"
}

Write-Host %artifactory_deploy_dir%

This is what it outputs: enter image description here

I'd rather do this via Commandline, but I've read that echo doesn't really work, is there any validity to that claim?

Thanks

Community
  • 1
  • 1
user1079703
  • 442
  • 6
  • 15
  • 1
    When your powershell script is generated by TeamCity, the variable value will have been injected into that script using %artifactory_deploy_dir% so this will be static at that point. Updating the parameter using set parameter will work, but you are just writing out that static injected value. Add an additional build step and move your final line (Write-Host %artifactory_deploy_dir%) to that and you will see the value has been updated – Matt Jul 10 '15 at 22:54
  • 1
    Also, the result of the CompareTo() method is not being tested. It will return 0 if the strings match a case sensitive comparison. – Matt Jul 10 '15 at 23:24

2 Answers2

1

DevOps was correct. Here is his answer:

"When your powershell script is generated by TeamCity, the variable value will have been injected into that script using %artifactory_deploy_dir% so this will be static at that point. Updating the parameter using set parameter will work, but you are just writing out that static injected value. Add an additional build step and move your final line (Write-Host %artifactory_deploy_dir%) to that and you will see the value has been updated

Also, the result of the CompareTo() method is not being tested. It will return 0 if the strings match a case sensitive comparison."

I ran another build step afterwards and queried the value and it had indeed changed. I also went ahead and updated the if statement to be "if ("%teamcity.build.branch%".equals("refs/heads/master"))," so that part works as well.

Thanks DevOps. If you post the answer in a way where I can mark it as "answered" I'll totally mark yours as the answer instead of mine.

user1079703
  • 442
  • 6
  • 15
0

Looks like the powershell script mentioned above is running as final step in your build configuration. First line of your screenshot:

[16:12:57] Step 2/2:rrtwr (Powershell) (3s)

You need to set the value of artifactory_deploy_dir before actually using it. So the powershell should run as first step. You just need to reorder the build steps and make sure artifactory_deploy_dir gets set before its use.

Mohammad Nadeem
  • 9,134
  • 14
  • 56
  • 82