I’m using a shell script in a gitlab ci pipeline to update .env variables depending on the branch’s context. Overall it is working as expected, except that 1 of the 3 variables is being repeated 3 times.
here is the call from the pipeline:
- chmod +x manage_back_env.sh
- source manage_back_env.sh "$MONGO_DB_URI" "$MONGO_DB_NAME" "$APP_HOST"
here is the script:
#!/bin/sh
update_env() {
local key="$1"
local value="$2"
local env_file="$3"
sed -i "s|^$key=.*|$key=$value|g" "$env_file"
}
update_env "MONGO_DB_URI" "$MONGO_DB_URI" "config.env"
update_env "MONGO_DB_NAME" "$MONGO_DB_NAME" "config.env"
update_env "APP_HOST" "$APP_HOST" "config.env"
(i’ve tried this without the function, making it very basic, and updating the file 1 variable at a time, but this creates the same result).
The .env file has values in place for local development-
MONGO_DB_URI: "value for the URI"
MONGO_DB_NAME: "nameOfTheSpecificDB"
APP_HOST: "[email protected]"
when the following script gets run in the gitlab ci pipeline, I end up with :
MONGO_DB_URI: "new value for the URI"MONGO_DB_URI: "new value for the URI"MONGO_DB_URI: "new value for the URI"
MONGO_DB_NAME: "newNameOfTheDB"
APP_HOST: "[email protected]"
I only have the most basic understanding of working with scripts like these, but cannot find any clear direction online, and cannot figure out why only the 1 value is repeated 3 times but the others come through as expected. I define the variables at the start of the job stage and have verified (multiple times) that the config.env file on the repo only has the MONGO_DB_URI variable written once.
Any guidance is appreciated. Thanks