2

I'm trying to take an environment variable in travis-ci and replace the contents of a file at runtime using sed.

The file in question contains:

service_name: travis-ci
repo_token: COVERALLS_TOKEN

On an ubuntu system, using sed -i 's/COVERALLS_TOKEN/ASDF/g' .coveralls.yml in the command line works, but carrying that over to the travis-ci configuration something like sed -i 's/COVERALLS_TOKEN/$COVERALLS_TOKEN/g' .coveralls.yml doesn't pull the environment variable.

What really throws me off is that I have a project today where the below .travis.yml entry works, but adapting it to this circumstances it doesn't.

Original implementation, still works today

sed -ri 's/^MY_ENV_VAR=/MY_ENV_VAR='$MY_ENV_VAR'/' .env

Adaptation (doesn't work)

sed -ri 's/^COVERALLS_TOKEN/$COVERALLS_TOKEN/' .coveralls.yml
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Ben
  • 60,438
  • 111
  • 314
  • 488
  • Possible duplicate of [Environment variable substitution in sed](http://stackoverflow.com/questions/584894/environment-variable-substitution-in-sed) – Benjamin W. May 08 '17 at 04:29

3 Answers3

3

You have two problems with your command. First, the ^ means it will only match COVERALLS_TOKEN where it occurs at the very beginning of a line. Since it's not at the beginning of a line in your YAML file, there is no match and the sed command does nothing.

Second, there is no variable substitution inside single quotation marks.

So remove the ^and use double quotes instead of single ones:

sed -ri "s/COVERALLS_TOKEN/$COVERALLS_TOKEN/" .coveralls.yml

Some notes:

  1. The variable $COVERALLS_TOKEN must be set in the shell at the time that you run the sed command.
  2. The substitution will fail with a syntax error if the value of $COVERALLS_TOKEN contains the delimiter you use on the substitution command. The command above uses /, but you can change that if needed - just pick something that doesn't occur in the token string.
  3. The token value will not be quoted in any way in the YAML file. Normally that's ok, but if there are any weird characters in the value, you will need to put quotes around it in the YAML by adding them to the replacement side of the substitution command as well:

    sed -ri "s/COVERALLS_TOKEN/'$COVERALLS_TOKEN'/" .coveralls.yml
    
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
1

The single quotes suppress variable expansion.

Michael Vehrs
  • 3,293
  • 11
  • 10
  • Are you suggesting it should be `sed -ri 's/^COVERALLS_TOKEN/"$COVERALLS_TOKEN"/' .coveralls.yml`? Unfortunately that's not working. I also tried `sed -ri 's/^COVERALLS_TOKEN/'"$COVERALLS_TOKEN"'/' .coveralls.yml` given the "possible duplicate" flag on this. – Ben May 08 '17 at 16:18
  • Yes, I am suggesting `'s/^COVERALLS_TOKEN/'"$COVERALLS_TOKEN"'/'`. What is your problem with that command? – Michael Vehrs May 09 '17 at 05:35
  • @Webnet Well, it works for me. Are you using `sed` in the usual fashion (shell), or in some bizarre environment in which your variable is not defined? – Michael Vehrs May 10 '17 at 06:27
0

This is works for me.

sed -ri 's,IMAGE_REPOSITORY,'"$IMAGE_REPO"',g' chart/values.yaml
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Alperen
  • 1
  • 1