Trying to use empty variable in artifact name

Eric J. Smith's Avatar

Eric J. Smith

07 Oct, 2016 06:11 PM

I'm setting a variable like this:
Set-AppveyorBuildVariable 'BuildAppScopePrefix' $BuildAppScopePrefix

Then I'm trying to create an artifact like this:

artifacts:
  - path: api_artifacts
    name: '$(BuildAppScopePrefix)crm-api'
    type: zip

But I'm getting the following error if the variable is set to an empty string. If the variable has a non-empty value it seems to work fine.

BuildAppScopePrefix : The term 'BuildAppScopePrefix' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
correct and try again.
At line:2 char:15
+ $artifacts["$(BuildAppScopePrefix)crm-api"] = @{ name = '$(BuildAppSc ...
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (BuildAppScopePrefix:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

  1. Support Staff 1 Posted by Feodor Fitsner on 07 Oct, 2016 06:18 PM

    Feodor Fitsner's Avatar

    Try this: $artifacts["$($BuildAppScopePrefix)crm-api"] = ...

    In PS strings $(something) means "execute something and put result into string". For example: Write-Host "Today date is $(Get-Date)" where Get-Date is cmdlet.

  2. 2 Posted by Eric J. Smith on 07 Oct, 2016 06:20 PM

    Eric J. Smith's Avatar

    Not sure how I would use that. Doesn't look like I can just set the artifact name to that.

  3. Support Staff 3 Posted by Feodor Fitsner on 07 Oct, 2016 06:25 PM

    Feodor Fitsner's Avatar

    Can you please describe what you're trying to achieve?

  4. 4 Posted by Eric J. Smith on 07 Oct, 2016 06:33 PM

    Eric J. Smith's Avatar

    I am creating an artifact that is zipping up a folder and then giving the artifact a dynamic name based on that variable:

    artifacts:
      - path: api_artifacts
        name: '$(BuildAppScopePrefix)crm-api'
        type: zip

  5. Support Staff 5 Posted by Feodor Fitsner on 07 Oct, 2016 06:42 PM

    Feodor Fitsner's Avatar

    How/where do you set that variable value?

  6. 6 Posted by Eric J. Smith on 07 Oct, 2016 07:19 PM

    Eric J. Smith's Avatar

    It is being set in a powershell script that runs before all of this. Here is a simplified version of the script that doesn't have any logic in it.

    $BuildAppScopePrefix=""
    Set-AppveyorBuildVariable 'BuildAppScope' $BuildAppScope

  7. Support Staff 7 Posted by Feodor Fitsner on 07 Oct, 2016 07:24 PM

    Feodor Fitsner's Avatar

    OK, now I see.

    whenever you say name: '$(BuildAppScopePrefix)crm-api' in artifacts section it expects that variable to be environment variable, not just regular PS variable. I think you should be fine with just:

    $env:BuildAppScopePrefix="<some-value>"
    

    without Set-AppveyorBuildVariable cmdlet.

  8. 8 Posted by Eric J. Smith on 07 Oct, 2016 07:36 PM

    Eric J. Smith's Avatar

    That's what I was doing before and it was working. It works now if the variable has a value in it. But blows up if the variable is empty.

  9. Support Staff 9 Posted by Feodor Fitsner on 07 Oct, 2016 07:37 PM

    Feodor Fitsner's Avatar

    Can you give me that place with $artifacts["$(BuildAppScopePrefix)crm-api"] please?

  10. 10 Posted by Eric J. Smith on 07 Oct, 2016 08:06 PM

    Eric J. Smith's Avatar

    I literally just have this:

    artifacts:
      - path: api_artifacts
        name: '$(BuildAppScopePrefix)crm-api'
        type: zip

  11. Support Staff 11 Posted by Feodor Fitsner on 07 Oct, 2016 08:09 PM

    Feodor Fitsner's Avatar

    But you are saying it "blows up". It can't blow up there if variable is not set. Where do you get the error?

    -Feodor Fitsner, AppVeyor

    ________________________________

  12. 12 Posted by Eric J. Smith on 07 Oct, 2016 08:11 PM

    Eric J. Smith's Avatar
  13. 13 Posted by Eric J. Smith on 07 Oct, 2016 08:13 PM

    Eric J. Smith's Avatar

    Hmm... actually it's not replacing the variable... and putting the $(BuildAppScopePrefix) in the file name and then blowing up when it's trying to upload the file.

  14. Support Staff 14 Posted by Feodor Fitsner on 07 Oct, 2016 08:13 PM

    Feodor Fitsner's Avatar

    Nope, add me to collaborators please.

    -Feodor Fitsner, AppVeyor

    ________________________________

  15. 15 Posted by Eric J. Smith on 07 Oct, 2016 08:16 PM

    Eric J. Smith's Avatar

    Added you

  16. Support Staff 16 Posted by Feodor Fitsner on 07 Oct, 2016 09:24 PM

    Feodor Fitsner's Avatar

    right, variable is not expanded/emptied if not defined. that's a new behavior, the same as Windows batch/cmd.

    Workaround for your case would be another variable for file name:

    $env:BuildAppScopePrefix = "<some-value>"
    $env:artifactName = "$($env:BuildAppScopePrefix)crm-api"
    

    and then:

    artifacts: 
      - path: api_artifacts 
        name: '$(artifactName)crm-api' 
        type: zip
    
  17. 17 Posted by Eric J. Smith on 07 Oct, 2016 09:34 PM

    Eric J. Smith's Avatar

    It is defined though. It's defined with an empty string. It works if it has a non-empty string value and it used to work with an empty string value.

  18. Support Staff 18 Posted by Feodor Fitsner on 07 Oct, 2016 09:40 PM

    Feodor Fitsner's Avatar

    Right, I'm saying we changed that variable expansion behavior to be consistent with system behavior. If environment variable is set to an empty string it's not defined then and you won't be able to see it in neither Get-ChildItem env: (PS), nor ENV (CMD). If variable is not defined then it's not expanded, for example running echo %none_var% will output %none_var%, but not an empty string.

  19. 19 Posted by Eric J. Smith on 07 Oct, 2016 09:50 PM

    Eric J. Smith's Avatar

    Ok I will work around it. Thanks.

  20. Ilya Finkelshteyn closed this discussion on 25 Aug, 2018 02:08 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac