Method and scripts to deploy to a remote repository which can also be directly modified.
Many methods using a git bare repository exist and allow deployments through git push
, but they fall short when the remote content must also be modified by other means.
Such a situation should not arise in a traditional development environment. Nevertheless, there are cases where it does. There are even cases where the software itself is designed in this way. Flat file CMS are such an instance.
The remote - let us call it production - has two repositories:
- bare
special repository that allows us to push to it - live
live application repository with the actual files (worktree)
To deploy from a local repository:
- We push to production, more precisely to
bare
. - The hooks on
bare
check the state oflive
:- if there are modifications, they are pushed to
bare
; - if there are no modifications, our push is accepted and
live
pulls frombare
.
- if there are modifications, they are pushed to
- git installed locally
- git installed on remote
- access to the remote through SSH for deployments with
git push
-
Create the
bare
repository:git init --bare bare.git
-
Copy the content of bare.git to the newly created directory.
-
Add execution permissions:
chmod +x deploy-config.sh chmod +x hooks/pre-receive chmod +x hooks/post-receive
-
Create the
live
repository where your application should be located:git init
-
In
live
, addbare
to remotes:git remote add bare /path/to/bare.git
-
Edit
deploy-config.sh
according to your setup.
-
If no local repository exists, create one:
git init
-
Add production to remotes, more precisely its
bare
repository:git remote add production user@yourserver:/path/to/bare.git
When everything above is set up, and provided that an SSH connection can be established with the remote server, we can deploy from the local repository with:
git push production master
Directly pulling from live
is impossible, as git does not have any fetch or pull hook.
If you absolutely need to pull from live
without pushing any commit, there is a method that only works if changes were made to production since the last commit (otherwise your dummy commit will be register on production):
- make a dummy commit (
touch dummy
); - push it to production;
- if as expected new work was made on
live
, the push is rejected and both production repositories get synchronized; - cancel the dummy commit (
git reset HEAD~1 ; git clean -f
) - pull from remote
- There are no cron jobs involved.
- For a
push
to be accepted,bare
andlive
must be synchronized. bare
andlive
are automatically synchronized on eachpush
trial.- You cannot directly pull new work from
live
(see: Pull fromlive
without pushing); - This setup is not a replacement for a backup system.