The sed
command will allow you to edit the contents of a file on a *nix system. For example, to change the text HELLO
to WORLD
everywhere it appears in the file example.txt
:
sed -i "s/HELLO/WORLD/g" example.txt
Leave off the g
at the end and add the line number before the s
if you want to replace it at a specific line:
sed -i "34s/HELLO/WORLD/" example.txt
The -i
is "inline", so it will edit the file you specify.
Here's how I use this in my pipelines:
I have an empty env file that has the structure you need, but without the values. For my use case, it looks something like this:
DB_HOST={{DB_HOST}}
DB_PORT=3306
DB_USER={{DB_USER}}
DB_PASSWORD={{DB_PASSWORD}}
In a qa job, it might look like this:
sed -i "s/{{DB_HOST}}/${QA_DB_HOST}/g" config.js
sed -i "s/{{DB_USER}}/${QA_DB_USER}/g" config.js
sed -i "s/{{DB_PASSWORD}}/${QA_DB_PASSWORD}/g" config.js
where the ${} values are Gitlab CI variables in your project's CI settings.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…