statamic / spock

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

Commands don't appear to run on Publish #3

Closed mikemartin closed 6 years ago

mikemartin commented 8 years ago

Hey guys. I feel like I must be missing something. I haven't had any luck running Spock on my production or dev environments. Are there dependencies that Spock requires to run?

I've installed the Spock addon, configured the Spock settings and .env file.

/site/settings/addons/spock.yaml

environments:
  - dev
commands:
  - "git add {{ full_path }}"
  - "git commit -m '{{ url }} updated by {{ committer:username }}'"

.env

APP_ENV=dev

Update: There appears to be an issue with {{ full_path }} and local user permissions. See Lodge post here.

mikemartin commented 8 years ago

I logged some warnings in SpockListener.php.

The var $data returns empty in run() :

public function run($data)
    {
        // Do nothing if we aren't supposed to run in this environment.
        if (! $this->environmentWhitelisted()) {
            return;
        }

        $this->data = $data;

        $process = new Process($this->commands());

        Log::warning(json_encode($data));
        $process->run();
    }

But $data returns all of the content and $commands successfully returns the git command in the commands() function.

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);
        }

        Log::warning(json_encode($data));
        Log::warning(json_encode($commands));

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

$commands returns: [2016-04-27 19:12:57] dev.WARNING: ["git add \/Users\/mike\/Sites\/trychameleon\/statamic\/..\/pages\/1.product\/index.md","git commit -m '\/product updated by admin'"]

kretzm commented 8 years ago

@mikemartin I had the same issue as you, but the add, commit and push worked for me (locally) when I set the spock.yaml config to:

environments:
  - production

As it says in the docs

you should make sure the environments array contains only production. Spock will do nothing when its running in any other environments.

igregson commented 8 years ago

Having a similar problem here. git add . is running but the commit isn't (without any special variables).

trevorgehman commented 8 years ago

Having the same issue. The command git add {{ full_path }} is what's causing it not to run. If I replace that with git add . everything works fine.

mttjohnson commented 7 years ago

The results I was getting from {{ full_path }} which is coming from Path::assemble(root_path(), $this->data->path()) ended up producing the following: /mytestsite.dev/statamic/../pages/6.test/index.md but the actual file was at /mytestsite.dev/statamic/../site/content/pages/6.test/index.md so it's like it's missing the context of sites/content/ needing to be included in the path. The data passed in did not seem to have that information as $this->data->path() only contained the relative portion of pages/6.test/index.md

I was running this from Statamic 2.1.9

It would be helpful if the addon detected that the process returned with an error code and logged it. I'm not sure what it takes to write to the log files stored at local/storage/logs but that seems an appropriate place to log something that isn't working right, rather than it just failing silently and having to hook up a debugger to the code to figure out what's not working.

I tried just sending the command git add . but found that it didn't include all the files I expected it to and found that it related to the fact that I had statamic installed above the web root and the commands were getting executed from the public web root directory and not in the context of the statamic base directory so I submitted a pull request to account for sites installed above the web root.

mttjohnson commented 7 years ago

I went ahead and figured out how to add logging to the command execution in the event that the command fails for some reason, and issued a pull request with my changes since the last pull request was so successful.

This should help any of you more easily diagnose what is going on. In my case it reported back the errors I was getting when trying to setup everything to automatically commit stuff to a git repo.

Log files are typically stored in the local/storage/logs/ directory.

edalzell commented 7 years ago

OK, I think this code fixes the problem:

    private function commands()
    {
        $folder = 'content';
        if ($this->data instanceof \Statamic\Data\Users\User)
        {
            $folder = 'users';
        }

        $full_path = path(site_path($folder), $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);
    }

If someone can confirm, I'll submit it as a PR.

wesort commented 7 years ago

+1 on this issue (or one close to it) as I can't make it work either. For me, it runs git add but doesn't commit or push.

Context:

spock.yaml:

environments:
  - production
commands:
  - "git add --all"
  - "git commit -m 'AUTO: {{ url }} updated by {{ committer:username }}'"
  - "git push"
wesort commented 7 years ago

Did anyone ever get this to function?

edalzell commented 7 years ago

There are 2 PRs that might resolve this. @jasonvarga or @jackmcdade can you review and merge?

wesort commented 6 years ago

@jackmcdade - I had a read of Version Control Strategies in the Knowledge Base and it specifically mentions Spock. I never got it to work though and would LOVE to have changes in production sites committed to their GitHub repos.

Has anyone got this working automatically?

wesort commented 6 years ago

Update

I've made some progress in getting Spock to run in my environment (LAMP hosting with Webfaction, php 7.0, I am the only SSH user). Even though my git credentials (set with git config --global) work via SSH, I had to explicitly set: git config --local user.name "username" git config --local user.email "user@example.com"

Spock is running on production as it should when I save a changes to globals, pages and some entries (but not all?!). On one collection Spock isn't running past git add .. ie: If I save a change on a entry in a collection called Projects, and then run git status, it shows the changes have been staged but not committed.

Also, I've noticed seen that changes to assets (adding some alt-text) aren't even firing git add . by Spock. Should another event listener be added? Nothing is being logged to the Statamic log file. I'm sure it must be something in my environment but I cannot identify what it is.

My spock.yaml
environments:
  - production
#  - dev
commands:
  - "git add ."
  - "git commit -m 'AUTO: {{ title }} updated by {{ committer:username }}' -m '{{ permalink }}'"
  - "git push"
My SpockListener.php is based on #9 But in addition to:
    public $events = [
        'cp.published' => 'run'
    ];
It now has:
    public $events = [
        'cp.published' => 'run',
        'content.saved' => 'run'
    ];
wesort commented 6 years ago

Update: These are my final thoughts on using Spock as a content backup system on production sites.

I'm noting this here for anyone else trying to address the same post-launch issue of a client's site being automatically backed-up regularly to GitHub. NB: I know Github isn't supposed to be used as a backup solution, but I feel it suits for the scale of sites that I build.

I got close with Spock, but in the end it hasn't worked for me. Recently, when a client reordered entries in a collection they reported an error with waiting for available socket and the CP had duplicate IDs that needed resolving. I believe Spock caused this so I've gone for another (old school) approach with a daily cron job which I've described here.

jasonvarga commented 6 years ago

If you guys continue to have issues, let me know if you see anything in your logs. The PR from @rrelmy has (finally, sorry!) been merged.