sed mongodb connection string copying multiple times in env variable through github action

this is command which i run in github action its copying into .env.production file

sed -i "s#DB_URI=.*$#DB_URI=mongodb+srv://xxxxxx:xxxxxxxx@db-mongodb-sfo3-api-discovery-f417ebf8.mongo.xxxxxxxxx.com/webapp_test?tls=true&authSource=admin&replicaSet=db-mongodb-sfo3-api-discovery#"

And this is error/job build status which shows multiples copying

DB_URI=***db-mongodb-sfo3-api-discovery-f417ebf8.mongo.xxxxxxx.com/webapp_test?tls=trueDB_URI=***db-mongodb-sfo3-api-discovery-f417ebf8.mongo.xxxxxx.com/webapp?tls=true&authSource=admin&replicaSet=db-mongodb-sfo3-api-discoveryauthSource=adminDB_URI=***db-mongodb-sfo3-api-discovery-f417ebf8.mongo.xxxxxx.com/webapp?tls=true&authSource=admin&replicaSet=db-mongodb-sfo3-api-discoveryreplicaSet=db-mongodb-sfo3-api-discovery

Plse let me how i can solve this

Asked By: DevOPs

||

Escape a few characters, and your command will work as the following:

sed -i 's#^DB_URI=.*$#DB_URI=mongodb+srv://xxxxxx:xxxxxxxx@db-mongodb-sfo3-api-discovery-f417ebf8.mongo.xxxxxxxxx.com/webapp_test?tls=true&authSource=admin&replicaSet=db-mongodb-sfo3-api-discovery#'

Input test.txt

ABC=abc
DB_URI=Hello
LATER=abc
# comment
// comment

Output test.txt

ABC=abc
DB_URI=mongodb+srv://xxxxxx:xxxxxxxx@db-mongodb-sfo3-api-discovery-f417ebf8.mongo.xxxxxxxxx.com/webapp_test?tls=true&authSource=admin&replicaSet=db-mongodb-sfo3-api-discovery
LATER=abc
# comment
// comment

Analysis

  • The double quotes were causing bash to expand the $# to 0 or something. Stop that expansion with single quotes. Or else sed will crash with sed: -e expression #1, char 187: unterminated `s' command
  • Add a ^ start anchor to stop one kind of duplication. EDIT: This doesn’t seem to be necessary, but it’s faster and more reliable
  • Escape the & symbols to become & so that sed doesn’t expand them. According to man sed:

The replacement may contain the special character & to refer to that portion of the pattern space which matched

Answered By: Daniel T