Skip to content

Instantly share code, notes, and snippets.

@fgilio
Last active February 20, 2023 17:25
Show Gist options
  • Save fgilio/2c863bf12ecae14e534b721a1181dd5c to your computer and use it in GitHub Desktop.
Save fgilio/2c863bf12ecae14e534b721a1181dd5c to your computer and use it in GitHub Desktop.

Revisions

  1. fgilio revised this gist Sep 13, 2022. No changes.
  2. fgilio revised this gist Sep 13, 2022. No changes.
  3. fgilio created this gist Sep 13, 2022.
    18 changes: 18 additions & 0 deletions .gitlab-ci.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    stages:
    - deploy

    .setup_staging_env_file: &setup_staging_env_file |
    echo "$STAGING_SECRETS" > staging_secrets.php

    .setup_production_env_file: &setup_production_env_file |
    echo "$PRODUCTION_SECRETS" > production_secrets.php

    staging:
    script:
    - *setup_staging_env_file
    - vapor deploy staging

    production:
    script:
    - *setup_production_env_file
    - vapor deploy production
    8 changes: 8 additions & 0 deletions STAGING_SECRETS
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    <?php
    // Store this files as a regular text variable in GitLab CI
    return [
    'SECRET_1' => 'foo',
    'SECRET_2' => 'bar',
    // This variable contains a double $$ to overcome interpolation in GitLab CI
    'SECRET_3' => '7iryufv1gui2hj$$C&F)Jfghsajsb@a(&YFV',
    ];
    39 changes: 39 additions & 0 deletions Secrets.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    <?php

    namespace Laravel\Vapor\Runtime;

    /**
    * We're overriding Vapor's original class witht this one using composer.
    * Make sure it's located in this path:
    * vendor/laravel/vapor-core/src/Runtime/Secrets.php
    */
    class Secrets
    {
    public static function addToEnvironment($path, $parameters, $file)
    {
    echo 'Overriden Secrets management'.PHP_EOL;

    /**
    * Extract the path to project root.
    * Vapor will automatically call this method when building
    * the project, and will provide the path to vaporSecrets.php
    * which will be located at the root.
    */
    $path = str_replace('vaporSecrets.php', '', $file);

    if (file_exists($path.'staging_secrets.php')) {
    $parameters = require $path.'staging_secrets.php';
    }
    if (file_exists($path.'production_secrets.php')) {
    $parameters = require $path.'production_secrets.php';
    }

    return tap($parameters, function ($variables) {
    foreach ($variables as $key => $value) {
    echo "Injecting secret [{$key}] into runtime.".PHP_EOL;
    $_ENV[$key] = $value;
    $_SERVER[$key] = $value;
    }
    });
    }
    }
    10 changes: 10 additions & 0 deletions composer.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    {
    "autoload": {
    "files": [
    "vendor-overrides/laravel/vapor-core/src/Runtime/Secrets.php"
    ],
    "exclude-from-classmap": [
    "vendor/laravel/vapor-core/src/Runtime/Secrets.php"
    ]
    }
    }