golang/appengine

Support for environment variables in app.yaml

st0ffern opened this issue · 2 comments

Example usage:
When running CI/CD in pipelines, deployment settings in app.yaml should depend on environment variables defined within the pipelines environment.

As of now i can no see that app.yaml and gcloud deploy supports using enviroment variables.

runtime: go112
service: default

env_variables:
  GOOGLE_PROJECT_NAME: ${GOOGLE_PROJECT_NAME}

Problem exists for all go runtimes (go1, go111 and go112)

sbuss commented

app.yaml doesn't support interpolation natively, but feel free to use envsubst to do it manually, eg:

Starting with this template file, app.yaml.template:

runtime: go112
service: default

env_variables:
  GOOGLE_PROJECT_NAME: ${GOOGLE_PROJECT_NAME}

Use envsubst to do variable interpolation:

$ GOOGLE_PROJECT_NAME=foo envsubst < app.yaml.template
runtime: go112
service: default

env_variables:
  GOOGLE_PROJECT_NAME: foo
# Or redirect to a file
$ GOOGLE_PROJECT_NAME=foo envsubst < app.yaml.template  > app.yaml
$ cat app.yaml 
runtime: go112
service: default

env_variables:
  GOOGLE_PROJECT_NAME: foo

I'm writing using nodejs reference even though it's a golang repo.

Another solution using gae-ayaml-env.
You should have a app.template.yaml file which would contain everything but env_variables: e.g.:

runtime: nodejs12
env: standard
service: default

automatic_scaling:
  min_instances: 0
  max_instances: 2

And your environment variables are defined in gitlab CI/CD as follows:

APP_DB_USER=testuser
APP_DB_PW=testpass

And your .gitlab-ci.yml file's something like this:

image: google/cloud-sdk:alpine

deploy_in_appengine:
  stage: deploy
  environment:
    name: production
  only:
    - develop
  script:
    - echo $SERVICE_ACCOUNT > /tmp/$SERVICE_ACCOUNT_KEY.json
    - gcloud auth activate-service-account --key-file /tmp/$SERVICE_ACCOUNT_KEY.json

    # update and install yarn
    - apk update && apk add nodejs npm

    # populate app.yaml file from app.template.yaml and env variables defined in gitlab CI/CD
    - npx gae-ayaml-env

    # install dependencies from package.json
    - npm i
    
    # deploy in app engine
    - gcloud --quiet --project=$PROJECT_ID app deploy app.yaml
  
after_script:
  - rm -f /tmp/$SERVICE_ACCOUNT_KEY.json