odan / slim4-skeleton

A Slim 4 Skeleton
https://odan.github.io/slim4-skeleton/
MIT License
448 stars 80 forks source link

Letting the bin/console.php setup command have non-interactive mode #95

Closed peter279k closed 2 years ago

peter279k commented 2 years ago

As title, sometimes it should let the bin/console.php setup command run automatically in non-interactive mode.

And it can run above command during the continuous delivery running.

I think it can run automated bin/console.php script when the config/env.php is existed.

odan commented 2 years ago

Hi! There are different use-cases we should not mix here.

The setup command is only for the developer's local project setup. For continuous integration or continuous delivery, you should use a build / deployment pipeline instead.

peter279k commented 2 years ago

Is there any reference about running the script in the build/deployment pipeline?

I think we should consider using the setup command for the remote development project setup when demonstrating the skeleton application.

odan commented 2 years ago

The setup command is not intended to be used in a build/deployment pipeline. This command should only be used if you, as a developer, need to create a local development database for this project.

If you have a very specific use-case, like creating a demo-site using the CLEARDB_DATABASE_URL environment variables, then better create a script for this specific task. For example, add the needed commands to your sync.yml file.

For example:

name: Sync original repository

on:
  schedule:
    - cron: '*/10 * * * *'
  workflow_dispatch:

jobs:
  up-and-running:
    runs-on: ubuntu-latest
    steps:
      - name: Get working copy
        uses: actions/checkout@master
        with:
          fetch-depth: 1
      - name: Commit and push if it changed
        run: |
          git config user.name "username"
          git config user.email "username@example.com"
          git add -A
          timestamp=$(date -u)
          git commit -m "Last Commit: ${timestamp}(UTC)" || exit 0
          git pull https://github.com/odan/slim4-skeleton master --no-edit
          mysql -uroot -proot -e 'CREATE DATABASE IF NOT EXISTS demo CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;'
          mysql -uroot -proot demo < resources/schema/schema.sql
          composer install --no-dev --optimize-autoloader
          echo "<?php" > config/env.php
          echo "return function (array \$settings): array {" >> config/env.php
          echo "\$url = parse_url(\$_ENV['CLEARDB_DATABASE_URL']);" >> config/env.php
          echo "\$settings['db']['host'] = \$url['host'];" >> config/env.php
          echo "\$settings['db']['port'] = '3306';" >> config/env.php
          echo "\$settings['db']['username'] = \$url['user'];" >> config/env.php
          echo "\$settings['db']['password'] = \$url['pass'];" >> config/env.php
          echo "\$settings['db']['database'] = substr(\$url['path'], 1);" >> config/env.php
          echo "return \$settings;" >> config/env.php
          echo "};" >> config/env.php

The mysql command creates a database with the name demo. Change that according to your environment.

Note that I use the composer install --no-dev --optimize-autoloader command, because in this very specific case it's actually a deployment to a production server and not a CI environment.

The echo commands are generating the config/env.php file for this specific setup. Usually this env.php should not be store there in production, but for this use-case it's ok. The getenv function is not thread safe, so this examples uses the $_ENV variable.

peter279k commented 2 years ago

Thanks. I got it. Let me think about the proper reference for the Heroku deployment.

peter279k commented 2 years ago

@odan, just notice that I've created the deployment.yml and sync.yml to complete the Heroku deployment and sync the original repository. Thanks for your help and appreciate your time and the file contents you can refer are as follows:

And the demonstration URL is available here.

Just notice that using the $_ENV will not get specific config vars in the Heroku platform and it will get the 500 interval server error after deploying to the Heroku platform.