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
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:
$COVERALLS_TOKEN
must be set in the shell at the time that you run the sed
command.$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.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