I have a Gitlab CI where it build a Docker image, push it the Gitlab Registry and then pull and start it on a server.
Problem is my application needs a .env file. As I don't want to commit this file, I thought I'd create it during the build. But I can't manage to do it.
Here's what I already tried :
1 - Setting all the different variables as Gitlab environment variables and using a bash script to create the .env file with the one stored in Gitlab
deploy_staging:
stage: deploy
before_script:
- chmod +x ./scripts/setup_env.sh
- ./scripts/setup_env.sh
#!/bin/bash
echo NEXT_PUBLIC_GOOGLE_MAP_API_KEY=$NEXT_PUBLIC_GOOGLE_MAP_API_KEY >> .env
echo NEXT_PUBLIC_APOLLO_BROWSER_URL=$NEXT_PUBLIC_APOLLO_BROWSER_URL >> .env
echo NEXT_PUBLIC_APOLLO_SERVER_URL=$NEXT_PUBLIC_APOLLO_SERVER_URL >> .env
echo NEXT_PUBLIC_APOLLO_PROD_URL=$NEXT_PUBLIC_APOLLO_PROD_URL >> .env
2 - Storing the whole .env as a file variable on Gitlab and copying it directly
deploy_staging:
stage: deploy
before_script:
- echo "$ENV_FILE" > ./.env
But with either solution, same result: the .env file is nowhere to be seen. Even if in the build logs, all seems fine.
Any idea what I'm missing?