Publishing with git + ssh: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
 
(49 intermediate revisions by the same user not shown)
Line 1: Line 1:
This recipe is to describe the steps for taking a local folder that's managed by git and using a remote server where you have ssh access, creating an "as-simple-as-possible" remote that can be pushed to (and eventually pulled from) and that is also mirrored on the same server. In other words, there's also a public working directory that automatically gets updated (via a githook) when the repo is pushed to. '''This recipe does not require that you use a git server like gitea, gitlab, or github. Just that you have ssh access to a server.''' You can combine this with also using a git server (as git repos can have multiple remotes).
This recipe is for taking a local folder that's managed by git and configuring a publicly accessible copy on a remote server that you can update just using git. This recipe requires:
* that you have (full) ssh access to the server,
* that ''git'' is installed on the server,


The steps
This recipe '''does not require that you have a specialized git server installed like gitea, gitlab, or github.'''


* Convert local folder into a bare repo
You can combine this with also using a git server (as git repos can have multiple remotes).
* scp the bare repo to a (non-public) part of your server
* Create the webhook in the bare repo on the server


== Convert local folder into a bare repo ==
Note: a simpler technique than this one would be [[publishing with scp]].
 
== On you local computer ==
 
Starting point, you have a folder name "project" that you want to publish. In a terminal, you cd to the parent directory  of your project folder.
 
=== Make a (bare) copy of your repo ===


     git clone --bare project project.git
     git clone --bare project project.git


Converts normal folder project (that is already git-managed!), into a ''bare repository'' project.git.
This command converts ''project'' a folder that is already git-managed, into a ''bare repository'' named project.git.


A bare repo is basically ''only the .git folder'' part of a git-managed folder (it's insides if you like). You could in principle just copy this (inner) folder, but git's clone command has a ''bare'' option that does it for you. It's a good idea to name a bare repo .git to remind yourself that it's a bare git repo.
A bare repo is basically ''only the .git folder'' part of a git-managed folder (it's insides if you like). You could in principle just copy this (inner) folder, but git's clone command has a ''bare'' option that does it for you. It's a good idea to name a bare repo .git to remind yourself that it's a bare git repo.


== scp the bare repo to your server ==
=== scp the bare repo to your server ===
 
If you have ssh access to your server with credentials like:
 
<pre>
user: sshuser
hostname: myvps.net
</pre>
 
And ''assuming you have have already created folder called git in the home folder of your (remote) server''. You don't have to name the folder git, but it makes sense to have a special place to keep bare repos on your server.
 
    scp -r project.git sshuser@myvps.net:git/
 
or if you could also configure a host alias in your .ssh/config:
 
<pre>
Host myvps
    Hostname myvps.net
    User sshuser
</pre>
 
And then just (instead of the scp command above):
 
    scp -r project.git myvps:git/
 
=== Add the remote to your original repo ===
 
At this point you can ''delete the bare repo'', aka the local folder ''project.git'', as it's now on the server.
 
Let's add the ssh address of the repo on the server as the "myserver" remote. You can name the remote whatever you want including "origin" if you would like the remote server to use this, the default and most common remote name.
 
first
    cd project
 
then
    git remote add myserver sshuser@myvps.net:git/project.git
 
''or using your ssh host alias''
 
    git remote add myserver myvps:git/project.git
 
If you would like to be able to simply type "git push" to publish to your server, you should set the server as the "upstream" branch with the following command (assuming you are using the branch named ''main''):
 
    git push --set-upstream myserver main
 
== On the remote server ==
 
ssh to your server
 
    ssh sshuser@myvps.net
 
or again if you have an ssh host alias, you can just use it:
 
    ssh myvps
 
=== Clone the bare repo to a (public) working directory ===
 
    cd public_html
    git clone ~/git/project.git
 
=== Create the webhook ===
 
    cd ~/git/project.git/hooks
 
Ok, let's create post-receive
 
    nano post-receive
 
<pre>
echo Updating the website...
GIT_WORK_TREE=~/public_html/project git checkout
</pre>
 
If you want to use force (overwriting any local changes) add the -f flag.
<pre>
echo Updating the website...
GIT_WORK_TREE=~/public_html/project git checkout -f
</pre>
 
=== Make the hook executable ===
 
    chmod +x post-receive
 
== Test it out (from your local machine) ==
 
Imagine you've made some changes, and then you (as usual)...
 
    git commit -a -m "local changes"
 
If you set the upstream branch above, you can:
 
    git push
 
Otherwise (and always) you can specify the remote + branch name when you push:


Here assuming you have a folder called git in your remote home folder.
    git push myserver main


    scp -r project.git remote:git/
You should see messages from the post-receive hook, importantly you should see the output of the echo command we put in the hook (Updating website...); when you see this, you know the hook is running.


== create the webhook on the server ==
<pre>
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 287 bytes | 287.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Updating website...
To myserver:git/project.git
  2dabf42..9be0276  main -> main
</pre>


Now ssh to the server and edit a post-receive githook.
If any errors occur, you should see them here at the end of the message stream.


    ssh server
[[Category:Cookbook]] [[Category:git]] [[Category:SSH]]
    nano git/project.git/githooks/post-receive.sh

Latest revision as of 08:42, 13 October 2024

This recipe is for taking a local folder that's managed by git and configuring a publicly accessible copy on a remote server that you can update just using git. This recipe requires:

  • that you have (full) ssh access to the server,
  • that git is installed on the server,

This recipe does not require that you have a specialized git server installed like gitea, gitlab, or github.

You can combine this with also using a git server (as git repos can have multiple remotes).

Note: a simpler technique than this one would be publishing with scp.

On you local computer

Starting point, you have a folder name "project" that you want to publish. In a terminal, you cd to the parent directory of your project folder.

Make a (bare) copy of your repo

   git clone --bare project project.git

This command converts project a folder that is already git-managed, into a bare repository named project.git.

A bare repo is basically only the .git folder part of a git-managed folder (it's insides if you like). You could in principle just copy this (inner) folder, but git's clone command has a bare option that does it for you. It's a good idea to name a bare repo .git to remind yourself that it's a bare git repo.

scp the bare repo to your server

If you have ssh access to your server with credentials like:

user: sshuser
hostname: myvps.net

And assuming you have have already created folder called git in the home folder of your (remote) server. You don't have to name the folder git, but it makes sense to have a special place to keep bare repos on your server.

   scp -r project.git sshuser@myvps.net:git/

or if you could also configure a host alias in your .ssh/config:

Host myvps
    Hostname myvps.net
    User sshuser

And then just (instead of the scp command above):

   scp -r project.git myvps:git/

Add the remote to your original repo

At this point you can delete the bare repo, aka the local folder project.git, as it's now on the server.

Let's add the ssh address of the repo on the server as the "myserver" remote. You can name the remote whatever you want including "origin" if you would like the remote server to use this, the default and most common remote name.

first

   cd project

then

   git remote add myserver sshuser@myvps.net:git/project.git

or using your ssh host alias

   git remote add myserver myvps:git/project.git

If you would like to be able to simply type "git push" to publish to your server, you should set the server as the "upstream" branch with the following command (assuming you are using the branch named main):

   git push --set-upstream myserver main

On the remote server

ssh to your server

   ssh sshuser@myvps.net

or again if you have an ssh host alias, you can just use it:

   ssh myvps

Clone the bare repo to a (public) working directory

   cd public_html
   git clone ~/git/project.git

Create the webhook

   cd ~/git/project.git/hooks

Ok, let's create post-receive

   nano post-receive
echo Updating the website...
GIT_WORK_TREE=~/public_html/project git checkout

If you want to use force (overwriting any local changes) add the -f flag.

echo Updating the website...
GIT_WORK_TREE=~/public_html/project git checkout -f

Make the hook executable

   chmod +x post-receive

Test it out (from your local machine)

Imagine you've made some changes, and then you (as usual)...

   git commit -a -m "local changes"

If you set the upstream branch above, you can:

   git push

Otherwise (and always) you can specify the remote + branch name when you push:

   git push myserver main

You should see messages from the post-receive hook, importantly you should see the output of the echo command we put in the hook (Updating website...); when you see this, you know the hook is running.

Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 287 bytes | 287.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Updating website...
To myserver:git/project.git
   2dabf42..9be0276  main -> main

If any errors occur, you should see them here at the end of the message stream.