Skip to content

Instantly share code, notes, and snippets.

@yosukehasumi
Last active July 25, 2023 20:07
Show Gist options
  • Star 43 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save yosukehasumi/66cc762ea8bc30a4b5b9e99b2fc4cb57 to your computer and use it in GitHub Desktop.
Save yosukehasumi/66cc762ea8bc30a4b5b9e99b2fc4cb57 to your computer and use it in GitHub Desktop.
Setting Up Git-Auto-Deploy on Digital Ocean

Install software-properties-common

sudo apt-get install software-properties-common

Add Repo

sudo add-apt-repository ppa:olipo186/git-auto-deploy

Update apt-get

sudo apt-get update

Install git-auto-deploy

sudo apt-get install git-auto-deploy

Modify /etc/git-auto-deploy.conf.json

{
  "pid-file": "/etc/git-auto-deploy/.gitautodeploy.pid",
  "http-host": "0.0.0.0",
  "http-port": 8001,
  "repositories": [
    {
      "url": "git@gitlab.example.com:user/repo.git",
      "branch": "master",
      "remote": "origin",
      "path": "/var/www/html",
      "filters": [
        {
          "object_kind": "push",
          "ref": "refs/heads/master"
        }
      ]
    }
  ]
}

Generate key

cd /root/.ssh/; ssh-keygen -t rsa;

copy key to git-auto-deploy

sudo cp /root/.ssh/id_rsa /etc/git-auto-deploy/.ssh/

change permissions for script to git-auto-deploy user

sudo chown -R git-auto-deploy:git-auto-deploy /etc/git-auto-deploy

Add git-auto-deploy user to the www-data group

usermod -a -G www-data git-auto-deploy

For some reason I'm not so good with permission stuff and the above command doesn't work the way i expect, my solution is to change the ownership of the entire html directory. EDIT: The group name may not be git-auto-deploy based on your configuration and the nginx user name. Modify this to suit (see comments below what worked for others).

chown -R git-auto-deploy:git-auto-deploy /var/www/html

You may need to make sure that the repo IP fingerprint has been added to the ~/.ssh/known_hosts file and copy this to the git-auto-deploy/.ssh/known_hosts file

ssh-keyscan -H -t rsa example.com  >> ~/.ssh/known_hosts
cp ~/.ssh/known_hosts /etc/git-auto-deploy/.ssh/
chown git-auto-deploy:git-auto-deploy /etc/git-auto-deploy/.ssh/known_hosts

Next make sure you add your public key from /root/.ssh/id_rsa.pub to gitlab and add a webhook that triggers the "push" event to http://mydomain.com:8001

Start and check your git-auto-deploy server by running

service git-auto-deploy start
service git-auto-deploy status

For Digital ocean you may have to open up the 8001 port

sudo ufw allow 8001/tcp

Install git

sudo apt-get install git

You may need to verify the authenticity (fingerprint) of the git repo. In my case this was a Digital Ocean git repo so I ssh'd into my /var/www/html directory and ran

git init
git remote add origin git@gitlab.example.com:user/repo.git
git fetch origin

To see git-auto-deploy logs you can watch the log file:

tail -f /var/log/git-auto-deploy.log
@Artem-Schander
Copy link

Thanks for the instruction. There is one thing that is missing (at least in my case).
chown -R git-auto-deploy:git-auto-deploy /var/www/html/.git

@yosukehasumi
Copy link
Author

Glad someone found it helpful :)

I believe that's what this was about:

chown -R git-auto-deploy:git-auto-deploy /var/www/html

Shouldn't -R do this recursively? maybe it skips hidden directories? Anyway good thing for others to know, thanks for the comment!

@Artem-Schander
Copy link

You're right. I initialized git afterwards. My bad :)

@amanjuman
Copy link

Update
chown -R git-auto-deploy:www-data /var/www/html
Otherwise Nginx will show 403 error

@rajasimon
Copy link

Is it possible to restart some of the systemctrl commands?

@yosukehasumi
Copy link
Author

yosukehasumi commented Apr 11, 2020

@rajasimon, have a look here: https://stackoverflow.com/questions/43537851/difference-between-systemctl-and-service-command

I wrote this gist a while back for my own personal use, I don't remember what kind of environment I was using but systemctl and service reference the same thing in this gist. Your environment might use sudo systemctl restart git-auto-deploy or something like that.

@stevegrant1983
Copy link

It seems as though the git-auto-deploy repo may have gone missing - apt-get is reporting a 404 when it tries to get the Release file for it.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment