1

I found a lot of questions about pass environment variables from Google Cloud Build to App Engine, but my case is a bit different.

I have a website in NextJS deployed in GAE, but i used the Gitlab CI to deploy to GAE not the Cloud Build. The problem is NextJS needs the envirmonte variables in build time (that occurs in Cloud Build) and the variables defined in app.yaml arent setted when the build occurs.

To fix this i followed this answer https://stackoverflow.com/a/55581164 and created a sh file that generates the .env on Gitlab before deploy to GAE.

My question is: is there a better way to access variables in Cloud Build since they are already present in app.yaml? Is this the default behavior or is it a bug?

My .gitlab-ci.yml:

image: rlancer/gcloud-node:1.0.2

deploy_production:
  stage: deploy
  environment: Production
  only:
  - master
  before_script:
    - chmod +x ./setup_env.sh
    - ./setup_env.sh
  script:
  - npx gae-ayaml-env # generates the app.yaml putting the env_variables from gitlab
  - echo $SERVICE_ACCOUNT > /tmp/$CI_PIPELINE_ID.json
  - gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
  - gcloud --quiet --project $PROJECT_ID app deploy app.yaml dispatch.yaml

after_script:
- rm /tmp/$CI_PIPELINE_ID.json

My app.yaml generated by npx gae-ayaml-env:

runtime: nodejs12

handlers:
- url: /.*
  script: auto
  secure: always
  redirect_http_response_code: 301
env_variables:
  NEXT_PUBLIC_API_URL: https://example.com
Victor Assis
  • 162
  • 7
  • From Cloud Build perspective this response [1] mentioned one easy way to list environment variables in a file at Cloud Storage and use the cloud-builders/gsutil to retrieve this file in a build step and copy it to the /workspace directory. The next build step can then populate the app.yaml file with the environment variables. This third party document [2] provides other alternatives. [1] https://stackoverflow.com/a/53503450/10415225 [2] https://medium.com/@brian.young.pro/how-to-add-environmental-variables-to-google-app-engine-node-js-using-cloud-build-5ce31ee63d7 – Mohammad I Jun 13 '20 at 02:00
  • For specific queries related to GitLab, .gitlab-ci.yml or build stage you can contact GitLab support [1]. [1] https://about.gitlab.com/support/ – Mohammad I Jun 13 '20 at 02:05
  • @MohammadI IMHO, you do not answer the OP question at all. The question is: how do you load env_variables from app.yaml in the build environment so that they can be used at build time `npm run build` ? – Syffys Dec 06 '20 at 21:38

0 Answers0