Skip to content

Commit

Permalink
chore: set up git automation and document necessary steps (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sm1lEE authored Aug 30, 2024
1 parent 8697527 commit 02cb229
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Welcome to the GitHub repository for the 21st digital website! Our aim is to tra
## Table of Contents

- [Git LFS](#git-lfs)
- [Git Automation](#git-automation)

## Git LFS

Expand All @@ -27,3 +28,57 @@ git lfs install
```

This setup only needs to be done once per user account.

## Git Automation

It might be necessary to push content changes from a staging or production server back to the Git repository. This mechanism is enabled by Laravel's scheduling system via cron jobs. An appropriate task has already been defined in `routes/console.php`:

```php
Schedule::command('statamic:git:commit')
->everyTenMinutes();
```

Additionally some [environment variables](#local-environment) have to be set and a [cron job](#cron-job-setup) to execute the scheduler has to be installed.

### Local Environment

It is important to disable the automatic git commit functionality. The best way for the local environment is to disable the git functionality completely:

| Variable | Description |
| ---------------------------- | -------------------------------------------------------- |
| `STATAMIC_GIT_ENABLED=false` | Disable Statamic's git system for the local environment. |

This is also the default value so it might not be necessary to explicitely set this variable locally.

### Server Environment

The following environment variables have to be set on production or staging servers:

| Variable | Description |
| ------------------------------ | --------------------------------------------------------------------------------------------------------------------- |
| `STATAMIC_GIT_ENABLED=true` | Enable Statamic's git system. |
| `STATAMIC_GIT_AUTOMATIC=false` | Disable the automatic commit functionality. This allows us to consolidate multiple commit messages into a single one. |
| `STATAMIC_GIT_PUSH=true` | Automatically push changes to the repository after a commit has been made. |

### Cron job setup

A cron job has to be set up on the production/staging server to automatically run the scheduled tasks:

```
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
```

Further information can be found in [Laravel's Scheduling documentation](https://laravel.com/docs/11.x/scheduling#running-the-scheduler).

### Handle commits from server

The Git setup for production/environment servers collects multiple commits and combines them into a single one. Therefore a generic Git user and email is used (see `config/git.php`). The consolidated commit message from the server starts with the string `chore(cms):`. This can be used in different workflows to enable/disable steps. A common example is the auto deployment feature on services like Laravel Forge:

```
if [[ $FORGE_DEPLOY_MESSAGE =~ ^chore\(cms\): ]]; then
echo "AUTO-COMMITTED ON PRODUCTION. NOTHING TO DEPLOY."
exit 0
fi
```

This can also be used in github workflows to skip actions or do specific operations.
2 changes: 1 addition & 1 deletion config/statamic/git.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@

'commands' => [
'git add {{ paths }}',
'git -c "user.name={{ name }}" -c "user.email={{ email }}" commit -m "chore(cms): update content ({{ message }})"',
'git -c "user.name={{ name }}" -c "user.email={{ email }}" commit -m "chore(cms): update content"',
],

/*
Expand Down
8 changes: 3 additions & 5 deletions routes/console.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Schedule;

Artisan::command('inspire', function () {
$this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote')->hourly();
Schedule::command('statamic:git:commit')
->everyTenMinutes();

0 comments on commit 02cb229

Please sign in to comment.