statamic / spock

Automatically perform git commits, pushes, and other cli actions when Statamic content changes
95 stars 16 forks source link

Using Spock for auto commit to git repo #6

Closed mttjohnson closed 6 years ago

mttjohnson commented 7 years ago

My intention for using the spock addon was for the purpose of using Git to automatically commit and push any changes up to a repo for change management purposes, and in trying to set this up I found a few things I had to adjust to make it work the way I wanted, and thought I'd let anyone else know in the event anyone else is wanting to use it for the same purpose.

I found that the git add {{ full_path }} command didn't work correctly, so I switched to trying to use git add . and found that while that added new files and updated existing ones, it did not remove any of the deleted files. I found that I could use git add --all in order to also catch any deleted files.

After finding something that would catch deleted files for the git command when the events fired I quickly found while testing that the event wasn't getting triggered when pages were deleted, which I felt was worth trying to catch. I found that I could add PageDeleted::class => 'run' to the $events array in SpockListener.php but that it had a few issues with the code later in the commands() function that referenced properties on $this->data passed in from the event. Since I wasn't using any of the properties on $this->data I just went ahead and commented those lines out and was able to get it to catch deletions and commit them whenever they happened also.

SpockListener.php

    /**
     * Spock has no trouble listening for these events with those ears.
     *
     * @var array
     */
    public $events = [
        'cp.published' => 'run',
        PageDeleted::class => 'run'
    ];
    /**
     * Get the concat'ed commands
     *
     * @return string
     */
    private function commands()
    {
        #$full_path = Path::assemble(root_path(), $this->data->path());

        #$data = $this->data->toArray();
        #$data['full_path'] = $full_path;
        $data['committer'] = User::getCurrent()->toArray();

        $commands = [];

        foreach ($this->getConfig('commands', []) as $command) {
            $commands[] = Parse::template($command, $data);
        }

        return join('; ', $commands);
    }

spock.yml

environments:
  - production
commands:
  - "git add --all"
  - "git commit -m '{{ url }} updated by {{ committer:username }}'"
  - "git push"

I'd be curious to know if anyone else has come up with any better way of managing changes in the control panel to automatically get put back into a git repo for change management purposes.

We'd like to be able to use this so we have one site as a staging/development site to prepare content and changes, test it, get it approved, and then be able to issue pull requests to take a specific commit or branch (develop) over into production (master) on a separate environment.

aerni commented 6 years ago

@jasonvarga Is this something you could consider adding? It would be great if Spock would detect not only changed but also deleted pages, collections etc. As well it would be awesome if we could have assets to commit as well. My goal is to always have the same content in development as in production.

orlyapps commented 6 years ago

We are also interested in this feature :)

jesseleite commented 6 years ago

🖖

jesseleite commented 6 years ago

Spock 2.0 should do it! 🎉