Search code examples
bashyamlappveyor

Adding quotes to an appveyor.yml echo line


I have this line that adds some version info to my build:

install:
  - 'echo "export const VERSION = Git Hash: %APPVEYOR_REPO_COMMIT%" > version.js'

obviously that is no valid JS because the quotes are missing. Could someone help me so I can get this string written to my version.js file:

export const VERSION = 'Git Hash: some_hash'

I did try some variations of escaped and unescaped quotes but build times are somewhat agonizing.


Solution

  • You are wasting the use of single quotes by quoting that sequence element. You can not have a plain scalar (because of the mid-sentence colon+space), but it often much better to use literal block style scalars. The following is equivalent to your YAML:

    install:
      - |-
        echo "export const VERSION = Git Hash: %APPVEYOR_REPO_COMMIT%" > version.js
    

    And so you can use single quotes for the echo parameter and still have double quotes within those:

    install:
      - |-
        echo 'export const VERSION = "Git Hash: %APPVEYOR_REPO_COMMIT%"' > version.js
    

    (the - after | is needed to strip the final newline)

    Alternatively, assuming Appveyor uses a relatively standard echo, you might be able to use its -e option:

        -e     enable interpretation of backslash escapes
    

    in which case you don't need to use literal block style scalars to get something less readable:

    install:
      - 'echo -e "export const VERSION = \"Git Hash: %APPVEYOR_REPO_COMMIT%"" > version.js'
    

    Please note that, according to the official YAML FAQ, whenever possible, the correct extension for a YAML file, is .yaml. You can tell Appveyor not to default to the .yml extension, using the Custom configuration .yml [sic!] file name. For my YAML parser I have also set this to be a hidden file: .appveyor.yaml