Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commit via php artisan does not work with Laravel scheduler or Cronjob #11318

Open
sparmin opened this issue Jan 7, 2025 · 4 comments
Open
Labels

Comments

@sparmin
Copy link

sparmin commented Jan 7, 2025

Bug description

The docs offer a way to use Git Automation with Laravel Scheduler to consolidate changes. Unfortunately the php artisan command php artisan statamic:git:commit does not work via the Laravel Scheduler or Cronjob but it works when executing it manually via terminal.

I've set up two scheduler tasks in console.php: one for committing, one for pushing.

Schedule::command('statamic:git:commit')->everyTenMinutes()->appendOutputTo(storage_path('logs/git.log'));
Schedule::exec('git push')->everyTenMinutes()->appendOutputTo(storage_path('logs/git.log'));

The log file shows the success message saying "Content committed", but a git status proves that nothing has been committed. When executing php artisan statamic:git:commit manually in the terminal, it commits correctly and the second scheduled task of pushing also works.

To test it without the usage of schedulers, I've set up the two tasks as cronjobs:

* * * * * cd /home/ohostudi/public_html/laravel/firalux.ohodesign.ch/src && php artisan statamic:git:commit >> storage/logs/git.log
* * * * * cd /home/ohostudi/public_html/laravel/firalux.ohodesign.ch/src && git push >> storage/logs/git.log

The behaviour was the same: first one only returned a success message without actual commit, but after manual committing the push was successful.

When nothing is to be committed, the log message also says so accordingly. So it seems like the command is indeed executed, and also kinda works but not really.

I also don't think it's a git issue, since the commit and push work per se.

How to reproduce

Set up Git Automation. Use these settings in the .env:

STATAMIC_GIT_ENABLED=true
STATAMIC_GIT_AUTOMATIC=false
STATAMIC_GIT_PUSH=false
STATAMIC_GIT_DISPATCH_DELAY=

Set up the two scheduler tasks as shown above or the two cronjobs. Make a change in a tracked file. Track if the commit command was successful with git status.

Logs

No response

Environment

Environment
Application Name: Firalux
Laravel Version: 11.34.2
PHP Version: 8.3.10
Composer Version: 2.8.3
Environment: production
Debug Mode: ENABLED
URL: firalux.ohodesign.ch
Maintenance Mode: OFF
Timezone: Europe/Zurich
Locale: de_CH

Cache
Config: NOT CACHED
Events: CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: file
Database: sqlite
Logs: stack / single
Mail: log
Queue: sync
Session: file

Sentry
Enabled: MISSING DSN
Environment: production
Laravel SDK Version: 4.10.1
PHP SDK Version: 4.10.0
Release: NOT SET
Sample Rate Errors: 100%
Sample Rate Performance Monitoring: NOT SET
Sample Rate Profiling: NOT SET
Send Default PII: DISABLED

Statamic
Addons: 4
Sites: 4 (Firalux - DE, Firalux - EN, Firalux - FR, Firalux - IT)
Stache Watcher: Disabled (auto)
Static Caching: half
Version: 5.41.0 PRO

Statamic Addons
aryehraber/statamic-logbook: 3.3.0
mitydigital/statamic-two-factor: 2.4.1
statamic/eloquent-driver: 4.18.0
webographen/statamic-admin-log: 1.1.0

Statamic Eloquent Driver
Asset Containers: file
Assets: file
Blueprints: file
Collection Trees: file
Collections: file
Entries: file
Forms: file
Global Sets: file
Global Variables: file
Navigation Trees: file
Navigations: file
Revisions: file
Sites: file
Taxonomies: file
Terms: file
Tokens: file

Installation

Fresh statamic/statamic site via CLI

Additional details

Our host provider has several php functions disabled for security reason, for example exec(), shell_exec(), system(), dl() and passthru(). I've enabled them for testing purposes, but that didn't change anything. There might be possibly some other similar setting responsible, although I doubt it personally.

@duncanmcclean
Copy link
Member

Can you try running git status as a cron job and post the output?

Also, I'm assuming your cron jobs are running as the same user as you're logged into when executing the commands manually? If they're not, that might be causing issues.

@sparmin
Copy link
Author

sparmin commented Jan 7, 2025

Output of git status with cronjob:

# On branch main
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   content/collections/pages/2025-01-06.test.md
#
no changes added to commit (use "git add" and/or "git commit -a")

Yep, running whoami via cronjob is the same user as the one I log in with to execute the command.

@duncanmcclean
Copy link
Member

Thanks for checking!

What does your config/statamic/git.php config file look like?

@sparmin
Copy link
Author

sparmin commented Jan 16, 2025

This is my config/statamic/git.php:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Git Integration
    |--------------------------------------------------------------------------
    |
    | Whether Statamic's git integration should be enabled. This feature
    | assumes that git is already installed and accessible by your
    | PHP process' server user. For more info, see the docs at:
    |
    | https://statamic.dev/git-automation
    |
    */

    'enabled' => env('STATAMIC_GIT_ENABLED', true),

    /*
    |--------------------------------------------------------------------------
    | Automatically Run
    |--------------------------------------------------------------------------
    |
    | By default, commits are automatically queued when `Saved` or `Deleted`
    | events are fired. If you prefer users to manually trigger commits
    | using the `Git` utility interface, you may set this to `false`.
    |
    | https://statamic.dev/git-automation#committing-changes
    |
    */

    'automatic' => env('STATAMIC_GIT_AUTOMATIC', true),

    /*
    |--------------------------------------------------------------------------
    | Queue Connection
    |--------------------------------------------------------------------------
    |
    | You may choose which queue connection should be used when dispatching
    | commit jobs. Unless specified, the default connection will be used.
    |
    | https://statamic.dev/git-automation#queueing-commits
    |
    */

    'queue_connection' => env('STATAMIC_GIT_QUEUE_CONNECTION'),

    /*
    |--------------------------------------------------------------------------
    | Dispatch Delay
    |--------------------------------------------------------------------------
    |
    | When `Saved` and `Deleted` events queue up commits, you may wish to
    | set a delay time in minutes for each queued job. This can allow
    | for more consolidated commits when you have multiple users
    | making simultaneous content changes to your repository.
    |
    | Note: Not supported by default `sync` queue driver.
    |
    */

    'dispatch_delay' => env('STATAMIC_GIT_DISPATCH_DELAY', 10),

    /*
    |--------------------------------------------------------------------------
    | Git User
    |--------------------------------------------------------------------------
    |
    | The git user that will be used when committing changes. By default, it
    | will attempt to commit with the authenticated user's name and email
    | when possible, falling back to the below user when not available.
    |
    | https://statamic.dev/git-automation#git-user
    |
    */

    'use_authenticated' => true,

    'user' => [
        'name' => env('STATAMIC_GIT_USER_NAME', '<my-git-user>'),
        'email' => env('STATAMIC_GIT_USER_EMAIL', '<my-git-user-email>'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Tracked Paths
    |--------------------------------------------------------------------------
    |
    | Define the tracked paths to be considered when staging changes. Default
    | stache and file locations are already set up for you, but feel free
    | to modify these paths to suit your storage config. Referencing
    | absolute paths to external repos is also completely valid.
    |
    */

    'paths' => [
        base_path('content'),
        base_path('users'),
        resource_path('blueprints'),
        resource_path('fieldsets'),
        resource_path('forms'),
        resource_path('users'),
        resource_path('preferences.yaml'),
        resource_path('sites.yaml'),
        storage_path('forms'),
        public_path('assets'),
    ],

    /*
    |--------------------------------------------------------------------------
    | Git Binary
    |--------------------------------------------------------------------------
    |
    | By default, Statamic will try to use the "git" command, but you can set
    | an absolute path to the git binary if necessary for your environment.
    |
    */

    'binary' => env('STATAMIC_GIT_BINARY', 'git'),

    /*
    |--------------------------------------------------------------------------
    | Commands
    |--------------------------------------------------------------------------
    |
    | Define a list commands to be run when Statamic is ready to `git add`
    | and `git commit` your changes. These commands will be run once
    | per repo, attempting to consolidate commits where possible.
    |
    | https://statamic.dev/git-automation#customizing-commits
    |
    */

    'commands' => [
        '{{ git }} add {{ paths }}',
        '{{ git }} -c "user.name={{ name }}" -c "user.email={{ email }}" commit -m "bot: {{ message }}"',
    ],

    /*
    |--------------------------------------------------------------------------
    | Push
    |--------------------------------------------------------------------------
    |
    | Determine whether `git push` should be run after the commands above
    | have finished. This is disabled by default, but can be enabled
    | globally, or per environment using the provided variable.
    |
    | https://statamic.dev/git-automation#pushing-changes
    |
    */

    'push' => env('STATAMIC_GIT_PUSH', false),

    /*
    |--------------------------------------------------------------------------
    | Ignored Events
    |--------------------------------------------------------------------------
    |
    | Statamic will listen on all `Saved` and `Deleted` events, as well
    | as any events registered by installed addons. If you wish to
    | ignore any specific events, you may reference them here.
    |
    */

    'ignored_events' => [
        // \Statamic\Events\UserSaved::class,
        // \Statamic\Events\UserDeleted::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Locale
    |--------------------------------------------------------------------------
    |
    | The locale to be used when translating commit messages, etc. By
    | default, the authenticated user's locale will be used, but
    | feel free to override this using the provided variable.
    |
    */

    'locale' => env('STATAMIC_GIT_LOCALE', null),

];

And having following .env:

APP_NAME="MyAppName"
APP_ENV=production
APP_KEY=MyKey
APP_DEBUG=true
APP_TIMEZONE="Europe/Zurich"
APP_URL=MyURL

APP_LOCALE=de_CH
APP_FALLBACK_LOCALE=de_CH
APP_FAKER_LOCALE=de_CH

APP_MAINTENANCE_DRIVER=file
# APP_MAINTENANCE_STORE=file

BCRYPT_ROUNDS=12

LOG_CHANNEL=stack
LOG_STACK=single
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

# DB_CONNECTION=sqlite
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=laravel
# DB_USERNAME=root
# DB_PASSWORD=

SESSION_DRIVER=file
SESSION_LIFETIME=120
SESSION_ENCRYPT=false
SESSION_PATH=/
SESSION_DOMAIN=null

BROADCAST_CONNECTION=log
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync

CACHE_STORE=file
CACHE_PREFIX=

MEMCACHED_HOST=127.0.0.1

REDIS_CLIENT=phpredis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=log
MAIL_HOST=127.0.0.1
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="[email protected]"
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

VITE_APP_NAME="${APP_NAME}"

STATAMIC_LICENSE_KEY=MyLicenseKey
STATAMIC_PRO_ENABLED=true
STATAMIC_STACHE_WATCHER=auto
STATAMIC_STATIC_CACHING_STRATEGY=half
STATAMIC_REVISIONS_ENABLED=false
STATAMIC_GRAPHQL_ENABLED=false
STATAMIC_API_ENABLED=false

STATAMIC_GIT_ENABLED=true
STATAMIC_GIT_USER_NAME="MyGitUser"
STATAMIC_GIT_USER_EMAIL="MyGitUserEmail"
STATAMIC_GIT_AUTOMATIC=false
STATAMIC_GIT_PUSH=false
STATAMIC_GIT_DISPATCH_DELAY=

STATAMIC_TWO_FACTOR_ENABLED=true
STATAMIC_TWO_FACTOR_ATTEMPTS_ALLOWED=3
STATAMIC_TWO_FACTOR_VALIDITY=

DEBUGBAR_ENABLED=false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants