trilbymedia / grav-plugin-git-sync

Collaboratively Synchronize your Grav `user` folder hosted on GitHub, BitBucket or GitLab
Apache License 2.0
243 stars 58 forks source link

Disabling plugin should disable schedule additions #210

Closed ndarilek closed 2 years ago

ndarilek commented 2 years ago

I'm attempting to use this plugin, but am running into issues with my development environment messing with production.

My goal is to sync everything, including accounts. I don't think this can happen without using the scheduler, so I've enabled that. My repository is hosted on Gitea, and I'm using SSH authentication.

Unfortunately, the scheduler runs both in my local development environment as well as in production. I don't necessarily want to disable the scheduler completely since I may develop with it. But right now it does undesirable things like automatically add content from my current directory and sync it with the remote. So something like a draft post I'm experimenting with locally is likely to get synced accidentally.

What I guess I need to do is disable the plugin in an environment I use for development. Unfortunately, it looks like disabling the plugin in localhost would break things in production since the scheduler uses that environment. The key is to probably run the scheduler in its own environment, but despite having:

enabled: false

in env/localhost/config/plugins/git-sync.yaml, my production environment is still syncing.

It looks like the plugin still makes scheduler additions even if it is disabled. Is that understanding correct? If so, I'd suggest not doing this since disabling the plugin implies disabling the scheduler--at least, so it does for me.

Any advice on how to resolve this is welcome. I'm trying to move a site to Grav, but the fact that it's locally mucking around with my git repo has me completely blocked and hesitant to continue that work. Essentially, I want to leave the scheduler active, and I'm happy to pull in remote changes that the production site makes. I just don't want the plugin committing and pushing changes on my behalf before I'm ready for them to go live. And it isn't clear how to disable the plugin in the localhost environment when doing so would be indistinguishable from running in the scheduler, or even that my attempts in that direction are working.

Thanks.

w00fz commented 2 years ago

Funny the timing at which you bring this up as I stumped upon a very similar issue, just now. It looks like this is a bug with Grav itself where environments are not taken into account.

What you could do meanwhile is to disable the scheduler from your local machine. Not the configuration for enabling/disabling but literally removing the line entry from the crontab -e. Although not ideal, you can still synchronize from command line via bin/plugin git-sync synch and even create your own cron job, outside of Grav, that runs that command every now and then.

This should prevent your localhost to run any schedule without having to change any of the configurations. If you have admin, you can double check this being disabled if you go to /admin/tools/scheduler. A red notice should say "Not enabled for: ".

ndarilek commented 2 years ago

Thanks, yeah, I guess I'll just disable the scheduler in my Dockerfile locally and be very careful not to commit or deploy that change.

If there's another issue filed for this environment bug, please link it here so I can follow it. I'm definitely going to have to take care not to commit this Dockerfile change if that's the route I go.

mahagr commented 2 years ago

I just fixed this in https://github.com/getgrav/grav/commit/cc8ec10098fb7acf370596fbbbe8fbb2ca278a1a

mahagr commented 2 years ago

BTW, you can still workaround the bug by adding --env=localhost (or your environment) as the first parameter after the command. The fix makes it to work in any order.

I recommend using the production environment in cron as it better reflects the configuration of the site.

ndarilek commented 2 years ago

Hmm, so what is the fix for specifically not having git-sync add to the scheduler in a development environment? There are a few things going on here:

  1. Explicitly disabling the plugin in an env/-specific configuration for localhost still adds its configuration to the scheduler. I think disabling a plugin should disable it completely and not install any of its hooks/templates.

  2. It seems like I can now change the scheduler environment, but if I can't specifically disable git-sync scheduler in a development environment then it won't help me much.

Sorry, I'm fairly new to Grav and still climbing the learning curve. I'm glad that was fixed--I'm just not sure how or if that fix will help me at all.

Thanks.

w00fz commented 2 years ago

I think what you need to do in your local dev environment is to just set the --env in the crontab command, where it says bin/grav scheduler you want it to be bin/grav scheduler --env=localhost. At that point your plugin will be detected disabled and wont be triggered.

ndarilek commented 2 years ago

But according to https://learn.getgrav.org/17/advanced/environment-config, the scheduler automatically uses the localhost environment, right?

And even if I did get it running in a different environment, git-sync would load, add itself to the scheduler, and run anyway. I need to not load git-sync at all in a specific environment.

Sorry if I'm missing something obvious.

mahagr commented 2 years ago

Actually it uses cli environment, I just checked it.

ndarilek commented 2 years ago

Ah, so the docs are inaccurate? If so, that'd explain why the plugin isn't being disabled.

Thanks, I should be able to rebuild my development container in such a way that git-sync is disabled locally based on that. I'll report back.

ndarilek commented 2 years ago

No, looks like my analysis is correct. I added a Docker build arg like so:

ARG SCHEDULER_ENVIRONMENT=cli
...
# Create cron job for Grav maintenance scripts
RUN (crontab -l; echo "* * * * * cd /var/www/html;/usr/local/bin/php bin/grav scheduler --env=$SCHEDULER_ENVIRONMENT 1>> /dev/null 2>&1") | crontab -

I then added this to my devcontainer.json:

    "build": {
        "args": {
            "SCHEDULER_ENVIRONMENT": "dev"
        }
    },

Then I moved env/localhost to env/dev, which again disables the git-sync plugin. I've confirmed that the crontab in the devcontainer does in fact use --env=dev. This should get Grav to stop monkeying around with my local Git repo.

Then I opened up a post in VS Code. I modified it, but left it unstaged.

Within 15 minutes, git-sync committed this change and pushed it to production. So unless I'm missing something, it isn't honoring the enabled setting in this environment--that, or I'll need the Grav change from earlier today.

Fortunately the change only set a published_date for this draft post in the future, but as things stand, this basically prevents me from making local changes unless I can finish them before git-sync commits them on my behalf, unless I completely disable the scheduler in my development environment. And completely disabling the scheduler isn't ideal, since part of my move from Hugo to Grav was intended to get access to scheduling and other CMS features without having to set up GitHub Actions or similar.

Thanks.

ndarilek commented 2 years ago

Works for me now. Thanks!