Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git post-recieve hook to run virtualenv, Django & various python scripts

I've been successfully using Git post-receive hook for a while to push files from local to server using the following in my post-receive file:

#!/bin/sh  

git --work-tree=/home/myuser/myproject/app --git-dir=/home/myuser/myproject/myproject.git checkout -f 

Now I want to also start virtual env, run Django manage.py and various python scripts after files are pushed to server using post-receive.

Based on this other question I have added following to my original post-receive file:

#!/bin/sh  

git --work-tree=/home/myuser/myproject/app --git-dir=/home/myuser/myproject/myproject.git checkout -f 
source /home/myuser/venv/bin/activate
python /home/myuser/myproject/manage.py makemigrations
python /home/myuser/myproject/manage.py migrate
python /home/myuser/myproject/app/python_script1.py
python /home/myuser/myproject/app/python_script1.py
sudo systemctl restart gunicorn

But it appears that the virtual env is not being started giving me error:

remote: hooks/post-receive: 4: hooks/post-receive: source: not found

Also, the remaining errors appear related to virtual env not starting. manage.py doesn't execute with following error:

SyntaxError: invalid syntax
remote:   File "/home/myuser/myproject/manage.py", line 16
remote:     ) from exc

The python scripts appear to run but because virtual env is not starting they are not finding modules.

The paths, file names, etc are all correct. Edit to add: Django, python scripts & virtual env run independently successfull, user has sudo no password permissions eg user ALL = NOPASSWD: /bin/systemctl restart gunicorn

Edit to add: remote server is Ubuntu 18

Does anything look obviously wrong or missing with my code for this?

like image 854
curtisp Avatar asked Oct 23 '25 10:10

curtisp


1 Answers

The solution turned out to be quite simple. I just had to change the shebang.

from: #!/bin/sh

to: #!/bin/bash

like image 100
curtisp Avatar answered Oct 24 '25 23:10

curtisp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!