I'm running MAMP locally. I have a php script that I use to build a web site, to generate static HTML files, that I then push to a web server.
This process acts a lot like Jekyll on github pages.
For now, I simply make changes to Markdown files (my content) and then hit a local url, localhost/mysite/build, to which generates the html files. Simple.
I do have git installed locally to version the PHP script itself, along with the markdown files.
Ideally I would like to create a post-commit hook that will simply "ping" that build URL to allow it do run.
A couple things. I have been trying but the post-commit doesn't run the URL.
I would like to use PHP in the post-commit file, is that possible? I don't need any validation or anything yet, just simply want to call the URL to have the process run when I commit.
I have done the chmod to make sure the script is executable.
The post-commit file is named 'post-commit', no extension.
I would think I could just add one simple line, like a file_get_contents(myurl), or something close to that.
I have been googling for a few hours now and found things that are close but not exactly right. Its really very simple, just sort of a noob with git hooks.
Thanks.
Commit hooks are shell scripts, so it is definitely possible to either run a php script (just as you would do from cmd line) or issue a request to your URL using curl: curl --request GET --url http://stackoverflow.com -v in order to fire the build. Depending on the build time you might want to run curl in the background.
First of all, try to prepare a shell script, which runs the build. Then setup the hook itself. See Simple git post-commit hook to copy committed files to a certain folder as an example hook.
Related
I wrote a simple project boostraper using bash.
It asks simple questions such as the parent folder to create the project inside, the type of the project (cli, web php, web symfony, web npm, etc…), the required dependencies, bootstrap everything, init git, and so on.
Then it returns the terminal to the user after changing the working directory to the freshly bootstraped one.
Fine.
I decided to rewrite the whole script using php in order to have a more user friendly cli interface (nice menus, progress bars, etc…) and to be able to pack the whole script in one and only binary (phar).
I struggle with the last part of the script: The one which was the simpliest part of the previous bash script:
cd $newprojectpath
exit 0;
For a reason I can't really figure out, I cannot do the same using a php script.
chdir allows me to change the working directory for the current php process but as soon as it is over, I'm back to working directory from where I invoked the php script (the one from where I spawned the php interpreter sub-process, obviously).
The only way I found in order to achieve this very simple move is to spawn a new shell sub-process from the php script itself.
It works, but I don't like it.
chdir($project->path);
pcntl_exec(getenv('SHELL'));
Did I miss a more simple way to change working directory using PHP cli ?
I am trying to run a cron-job that automates the cache rebuild function of Woo Search Box plugin.
The plugin has this dynamic cron command, that I set to run once a day at 1AM:
php /home/carit/public_html/index.php 16021417635f7ebe43c604a
I am well experienced with cron jobs inside of C-Panel but I can't figure out what prevents this cron from running. All in all, I have tried what the documentation suggests here, also tried usr/bin/php instead of php but it didn't make any difference.
If I run this cron-job inside my server's terminal, it works like a charm and the cache is being rebuilt. It only seems to not be working when I run it via cron-job.
Does anyone have any suggestions or ideas on why doesn't this cron-job works?
Please correct me in the comments in case I forgot to provide any key information about my problem,
Thanks.
It seems that some newer versions of CentOs or Ubuntu servers use either /usr/local/bin/php or simply /usr/bin/php. If you have ftp access to the server you want the cron to run in, make sure to check for the php file in both paths recommended above before trying to use the ordinary php before a cron job.
i'm making an extension that use a php file in the 'public' directory as a webhook.
From this hook, I will need to run some shell commands (things like npm run build or similar). Is there some kind of api from plesk that let me do this? Or should I use a php function like exec or shell_exec?
I looked through the net, but in the end it looks like what I'm trying to accomplish is not doable. You can indeed use shell_exec to run shell commands, but the user used to run this commands is psaadm, which is a severely restricted user when operating inside a domain document folder. You can see the files, but not create or execute any script, which renders this function useless for what I wanted to do.
If you want to use a webhook tu run your npm build (or other scripts), you can always create a php file in the document root folder and execute your commands using shell_exec. When you call this file, it will execute the commands as the customer user, and will have the ability to run those scripts.
P.S. It might actually be accomplished if you change the permission for the psaadm user, but it's not something a customer which install an extension should do. Still, not sure if it will work for the domain document root folder of your customers.
Is there any way to set up git such that it listens for updates from a remote repo and will pull whenever something changes? The use case is I want to deploy a web app using git (so I get version control of the deployed application) but want to put the "central" git repo on Github rather than on the web server (Github's interface is just soooo nice).
Has anyone gotten this working? How does Heroku do it? My Google-fu is failing to give me any relevant results.
Git has "hooks", actions that can be executed after other actions. What you seem to be looking for is "post-receive hook". In the github admin, you can set up a post-receive url that will be hit (with a payload containing data about what was just pushed) everytime somebody pushes to your repo.
For what it's worth, I don't think auto-pull is a good idea -- what if something wrong was pushed to your branch ? I'd use a tool like capistrano (or an equivalent) for such things.
On unix-likes you can create cron job that calls "git pull" (every day or every week or whatever) on your machine. On windows you could use task scheduler or "AT" command to do the same thing.
There are continuous integrations programs like Jenkins or Bamboo, which can detect commits and trigger operations like build, test, package and deploy. They do what you want, but they are heavy with dependencies, hard to configure and in the end they may use periodical check against git repository, which would have same effect like calling git pull by cron every minute.
I know this question is a bit old, but you can use the windows log and git to autopull your project using a webhook and php (assuming your project involves a webserver.
See my gist here :
https://gist.github.com/upggr/a6d92e2808e9628ebe0d01fd93569f4a
As some have noticed after trying this, if you use php exec(), it turns out that solving for permissions is not that simple.
The user that will execute the command might not be your own, but www-data or apache.
If you have root/sudo access, I recommend you read this Jonathan's blog post
When you aren't allowed/can't solve permissions
My solution was a bit creative. I noticed I could create a script under my username with a loop and git pull would work fine. But that, as pointed out by others, bring the question of running a lot of useless git pull every, say, 60 seconds.
So here the steps to a more delicate solution using webhooks:
deploy key: Go to your server and type:
ssh-keygen -t rsa -b 4096 -C "deploy" to generate a new deploy key, no need write-permissions (read-only is safer). Copy the public key to your github repository settings, under "deploy key".
Webhook: Go to your repository settings and create a webhook. Lets assume the payload address is http://example.com/gitpull.php
Payload: create a php file with this code example bellow in it. The purpose of the payload is not to git pull but to warn the following script that a pull is necessary. Here the simple code:
gitpull.php:
<?php
/* Deploy (C) by DrBeco 2021-06-08 */
echo("<br />\n");
chdir('/home/user/www/example.com/repository');
touch(GITPULLMASTER);
?>
Script: create a script in your preferred folder, say, /home/user/gitpull.sh with the following code:
gitpull.sh
#!/bin/bash
cd /home/user/www/example.com/repository
while true ; do
if [[ -f GITPULLMASTER ]] ; then
git pull > gitpull.log 2>&1
mv GITPULLMASTER GITPULLMASTER.`date +"%Y%m%d%H%M%S"`
fi
sleep 10
done
Detach: the last step is to run the script in detached mode, so you can log out and keep the script running in background.
There are 2 ways of doing that, the first is simpler and don't need screen software installed:
disown:
run ./gitpull.sh & to put it in background
then type disown -h %1 to detach and you can log out
screen:
run screen
run ./gitpull.sh
type control+a d to detach and you can log out
Conclusion
This solution is simple and you avoid messing with keys, passwords, permissions, sudo, root, etc., and also you prevent the script to flood your server with useless git pulls.
The way it works is that it checks if the file GITPULLMASTER exists; if not, back to sleep. Only if it exists, then do a git pull.
You can change the line:
mv GITPULLMASTER GITPULLMASTER.date +"%Y%m%d%H%M%S"`
to
rm GITPULLMASTER
if you prefer a cleaner directory. But I find it useful for debug to let the pull date registered (and untracked).
For our on-premises Windows test servers, we use Windows Task Scheduler tasks, set to run every 3 minutes, pulling from Bitbucket Cloud to repositories on those servers. While not instantaneous, it meets our needs, and has proven to be reliable.
I am building a PHP/Yii application. While developing and testing on different machines, I'd like to be able to quickly see in the footers if the version I am seeing is the latest. So basically I want to generate some kind of version number every time I modify something.
I was thinking since I use Git (and GitHub) I could use some of the meta data generated at every commit?
How could I achieve this?
Note: I'd like to avoid using command-line stuff as my current hosting won't allow me to do this.
Add a git hook to create a txt file in the root folder of your app to keep track of the version (or tag, or whatever) of the current deployed code.
Just a 30 sec. example (search google for more details and how to use hooks in git) put in the .git/hooks folder a file named pre-commit and add this shell code:
#!/bin/sh
rm version.txt -i
git describe --tags >> version.txt
git add version.txt
If you deploy with Capistrano, one very serious possibility is to read the 'REVISION' file from the base of the site. If you deploy from a git repo, it will be the commit SHA.