'How to upload .env file to gcloud by overriding default .gcloudignore settings

I am currently trying to migrate a PHP application to Google AppEngine. Everything works, except one thing: My .env file is not deployed alongside my application's code files and I can't get Google Cloud to stop ignoring this file.

I have already tried to add the entry !.env to the file .gcloudignore, but this doesn't change anything. The .env file is simply not showing up after the deployment of the app.

In case you need more details, feel free to take a look at the corresponding GitHub repository.

I would be really happy if anyone has an idea how to deploy my environment variables. There might even be a completely different solution which is way better. So, feel free to share your approach towards storing environment variables for Google AppEngine applications.

Thanks in advance!



Solution 1:[1]

I had the exact same problem with the deployment of a Node.js app to Google Cloud. This was my setupt:

.env file with credentials on my personal PC but not committed to git

.env file ignored in .gitignore to not have credentials in the GitLab repo

Environment variables set in the GitLab CI/CD variables

I used the .gitlab-ci.yml to read the GitLab variables into a .env file that is created during deployment:

image: docker:latest

stages:
  - deploy

deploy:
  stage: deploy
  image: google/cloud-sdk
  services:
    - docker:dind
  script:
    - rm .gitignore
    - echo NODE_ENV=production > .env
    - echo SERVER_PORT=8080 >> .env
    # gcloud deployment following

The crucial part here was the line

rm .gitignore

It deletes the .gitignore file before packing the Docker Image. This ensures that Google Clouds docker packing does not use the .gitignore file to exclude .env again.

Notice that other files from .gitignore may also end up staying in your Docker image so you might want to add a .dockerignore file to clean up your image.

Solution 2:[2]

You can define environment variables in the env_variables section of the app.yaml file:

https://cloud.google.com/appengine/docs/flexible/php/reference/app-yaml#defining_environment_variables

Solution 3:[3]

I had a similar problem, which was that gcloud build was not uploading a .env file, and found that the default .gcloudignore was including my .gitgnore file, which included .env. I took .env out of my .gitgnore and then gcloud build did not ignore it. For me that was an acceptable solution.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Finkle MacGraw
Solution 2 DazWilkin
Solution 3 Jonathan Munson