3

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?

curtisp
  • 2,227
  • 3
  • 30
  • 62

1 Answers1

2

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

from: #!/bin/sh

to: #!/bin/bash

curtisp
  • 2,227
  • 3
  • 30
  • 62