Как добавить переменные среды в Google App Engine - PullRequest
1 голос
/ 04 августа 2020

Я развернул свой проект Django в Google App Engine, и мне нужно добавить переменные среды.

В документах говорится, что нужно добавить их в app.yaml, но это кажется плохой практикой, потому что app.yaml должен быть в вашем репозитории git.

Есть ли способ добавить переменные среды в App Engine так же, как вы можете добавить их в Cloud Run> Services> Variables & Secrets ?

Ответы [ 2 ]

2 голосов
/ 04 августа 2020

Google Secret Manager доступен с этой весны:

введите описание изображения здесь

def create_secret(project_id, secret_id):
    """
    Create a new secret with the given name. A secret is a logical wrapper
    around a collection of secret versions. Secret versions hold the actual
    secret material.
    """

    # Import the Secret Manager client library.
    from google.cloud import secretmanager

    # Create the Secret Manager client.
    client = secretmanager.SecretManagerServiceClient()

    # Build the resource name of the parent project.
    parent = client.project_path(project_id)

    # Create the secret.
    response = client.create_secret(parent, secret_id, {
        'replication': {
            'automatic': {},
        },
    })

    # Print the new secret name.
    print('Created secret: {}'.format(response.name))

  • Использовать секреты из приложения вместо переменных среды:
def access_secret_version(project_id, secret_id, version_id):
    """
    Access the payload for the given secret version if one exists. The version
    can be a version number as a string (e.g. "5") or an alias (e.g. "latest").
    """

    # Import the Secret Manager client library.
    from google.cloud import secretmanager

    # Create the Secret Manager client.
    client = secretmanager.SecretManagerServiceClient()

    # Build the resource name of the secret version.
    name = client.secret_version_path(project_id, secret_id, version_id)

    # Access the secret version.
    response = client.access_secret_version(name)

    # Print the secret payload.
    #
    # WARNING: Do not print the secret in a production environment - this
    # snippet is showing how to access the secret material.
    payload = response.payload.data.decode('UTF-8')
    print('Plaintext: {}'.format(payload))
0 голосов
/ 05 августа 2020

Если вы используете процесс непрерывного развертывания, вы можете переписать (или создать) app.yaml, включив в него переменные, относящиеся к каждой цели развертывания в системе сборки компакт-диска.

Мы переписываем несколько файлов как часть нашего процесс развертывания в движке приложений с помощью конвейеров Bitbucket. Переменные можно определить на уровне рабочей области (в нескольких репозиториях), внутри репозитория, а также для каждой определенной цели развертывания. Эти переменные можно защитить, чтобы они не читались. Bitbucket deployment variables

build: &build
  - step:
      name: Update configuration for deployment
      script:
        - find . -type f -name "*.yaml" -exec sed -i "s/\[secret-key-placeholder\]/$SECRET_KEY/g" {} +

Refer to https://support.atlassian.com/bitbucket-cloud/docs/variables-in-pipelines/#Deployment -переменные

...