Laravel Artisan interactive prompts do not work from Git hook - php

I have a Laravel Artisan command that works great and has some interactive-type questions. It works great when run from the command line.
However, when invoked from a Git hook (a Bash script), it doesn't display the interactive questions like "confirm" or "ask", etc. I just need to know in which context the Artisan command is being run and whether I will be able to display my "confirm" or other interactive questions. Is it possible?
My code below:
#!/bin/bash
php artisan some:command

Git executes the hook scripts in a non-interactive shell, so the script's stdin isn't connected to the terminal. We can redirect the terminal to the hook script's process so we can interact with the commands:
#!/bin/sh
# This is a git hook.
# Connect terminal to STDIN...
exec < /dev/tty
# Run any interactive commands...
php artisan some:command
# Close STDIN...
exec <&-
We use exec in this context to control the file descriptors available to the script. See this question or the POSIX spec for more information about exec.
This technique should work fine from the command-line. We may encounter issues if we use other tools that wrap or interact with git.

Related

Laravel artisan serve and npm run dev single command

Is there a way to run terminal commands simultaneously
Like
php artisan serve
And
npm run dev (for laravel vite)
Run it in a comment or terminal with a shortcut
Yes. There is a creative way
You should define a variable in your command line
İf you're using linux run this command:
export part="php $PWD/artisan serve"
Note:part is variable name. Name it whatever you want (part stands for php artisan)
Note:by running this command the variable will accessible just in that CMD window. That means this way is a temporary way and if you want to access it all the time you need to add this command to your .bash or .profile file

PhP Exec GIT command from crontab

I have a deploy script which i have to execute every time i change my config files. Which is a bummer.
I tried to add the deploy script to my crontab, but from the logs i see that this command is failing in crontab because you are tehnically not in a git repo
exec('git log -1 --format="%H"')
This fella should deliver a revision number so the code can use it, but it dous not execute when you run the script from crontab.
Anyone has any idea how to make it work?

Run GruntJs task from php exec function

I would like to perform some grunt task by running command from php exec function.
I've already:
Install grunt globally: npm -g install grunt-cli
Setup apache and windows to allow running CLI from php (I use
wampserver)
I ran this script from php:
<?php
exec('grunt --gruntfile Gruntfile.js');`
And I found in apache log that grunt command is not found whereas I can run this command from CLI and grunt is in my env path.
So I've decided to create a batch file that contains that script and launch the batch instead of the grunt command directly.
<?php
exec ('gruntTask.bat'); // I know that it can execute script because I can
// create folder if I put mkdir command in gruntTask.bat`
gruntTask.bat contains:
grunt --gruntfile Gruntfile.js
But why it couldn't execute this script? Have I missed something?
Need helps guys,
Regards,

System commands not executing in PHP

I have a php script where some system commands are running fine and others are not. The commands that are not running can be copy and pasted to the shell and be ran just fine.
System: OSX 10.9.2 (everything is updated).
I have tried many different commands like the following.
backticks, exec(), shell_exec(), system(), passthru()
This command works fine.
exec("drush si -y --db-url=mysql://user:pass#localhost:3306/dbname");
But these commands do not run.
exec("drush sql-sync #remote.staging #dev.anme -y");
exec("git ls-remote --heads git#github.com:blablaname/name.git");
The commands that do not run can be copy and pasted into the shell and run great. I have made sure the script is being ran in the proper directory using the getcwd() function.
If you call php program having exec() from web browser,It executes as www user. So www user may not have privilege to connect/sync to remote host.That's why it works on localhost and failing on remote host.
So one solution is
1)save the command as bash script
2)set uid bit(It can be root or user having sufficient privilege).
3)execute that bash script by exec so that it will run as previlged user.
4)You should ip restrict your program since setuid is dangerous.
setuid

travis-ci script

I'm trying to setup phing to work with travis-ci, but I can't get it to run a setup script to get all the dependencies installed.
My .travis.yml file is:
language: php
php:
- 5.2
script: ./.travis-phing.sh
In travis, I get the error:
/home/travis/build.sh: line 105: ./.travis-phing.sh: Permission denied
What is causing that?
Solved
The script to be set to execute. I used:
chmod a+x .travis-phing.sh
Then simply commit, and push back to github.
Run the script using bash
Another option would be to run the script using bash, this would omit the need to modify the files' permissions.
bash path/to/file.sh
Alternatively:
sh path/to/file.sh
Note that
In this case you're not executing the script itself, you're executing bash or sh which then runs the script. Therefore the script does not need to be executable.
Make sense?
I've found this solution incredibly useful myself. I'm mainly running node & npm projects on travis-ci, those builds make use of the npm test command which you can configure to be anything.
I'm order to modify file permission I need to use sudo chmod ... on my local machine. But you can't always use sudo on travis-ci.
sh file.sh allows me to run my tests both locally and on travis-ci without having to manually update permissions.

Categories