1

RESUME

When I push from my local workspace to the target repo in GitLab on my remote VPS, I want GitLab run a script and ask a beta repo on the same VPS to git checkout and pull in order to check my changes.

Current configurations

Gitlab configurations

Suppose you already have a project in your admin area, you just need to get it's relative path

  1. Admin area > Projects

  2. Select your project

  3. Get the path in the project profil (hashed for this case)

  4. Create a new directory in this location called custom_hooks.

sudo su
cd /var/opt/gitlab/git-data/repositories/hashed/path/of/project
mkdir custom_hooks
  1. Inside the new custom_hooks directory, create a file with a name matching the hook type. For a post-receive hook the file name should be post-receive with no extension.
cd custom_hooks
nano post-receive
  1. Write the code to make the server hook function as expected. Hooks can be in any language. Ensure the ‘shebang’ at the top properly reflects the language type. In that case the script code used is :
 #!/bin/sh
 unset GIT_INDEX_FILE
 cd /var/repo/beta.git && git checkout master && git up # --[see the note 2 below]
  1. Make the hook file executable and make sure it’s owned by Git.
chmod +x post-receive

Note 1 :

You can find more informations about git hooks here : GitLab Documentation : Server hooks



VPS configurations

  1. Create new alias with @RichardHansen recommandations

    git config --global alias.up '!git remote update -p; git merge --ff-only @{u}'


Note 2 :

During my researches, I've found an interesting answer about git pull on the forum. I've decided to follow that advice and I made an alias named git up. What is important is that :

  • it works with all (non-ancient) versions of Git,
  • it fetches all upstream branches (not just the branch you're currently working on), and;
  • it cleans out old origin/* branches that no longer exist upstream.

You can find more informations about "In what cases could git pull be harmful ?" here : Link to answer


  1. Create a directory for git repos only and access it to process Hooks configurations
 # Create a repo for the project in apache area

 mkdir /var/www/beta


 # Create the git repo only folder

 cd /var

 mkdir repo && cd repo


 # Create git repo and init

 mkdir beta.git && cd beta.git

 git init --bare # --bare means that our folder will have no source files, just the version control.


 # Add gitlab remote

 git remote add gitlab


 # Accessing hooks folder to create script

 cd hooks

 cat > pre-receive


 # On the blank line, write this script then 'ctrl + d' to save and press enter to exit

 #!/bin/sh
 unset GIT_INDEX_FILE
 git --work-tree=/var/www/beta --git-dir=/var/repo/beta.git checkout -f



 # Make the file executable

 chmod +x post-receive


Note 3 :

'git-dir' is the path to the git repository. With 'work-tree', we can define a different path to where the files will actually be transferred to.

The 'post-receive' hook will be looked into every time a push is completed and will set the path where the files will be transferred to /var/www/beta in that case.



Local Workspace configurations

# Create in your workspace a folder to hold the project

 cd /path/to/workspace

 mkdir project && cd project



# Initialize git and add gitlab remote

  git init

  git remote add gitlab ssh://user@mydomain.com/gitlab/path/of/project



# Create an index.html file and send the initial commit 

 nano index.html

# copy this into the file then 'ctrl + x' then 'y' then 'enter' to save 

<html>
    <head>
        <title>Welcome to Beta domain!</title>
    </head>
    <body>
        <h1>Success!  The beta virtual host is working!</h1>
    </body>
</html> 


# prepare the changes and then send the commit

 git status

 git add index.html

 git commit -m "chore: add index.html :tada: :rocket:"

 git push gitlab master


EXPECTED RESULTS

The expected result of this process is that when the git push gitlab master is done, the hook inside the gitlab hashed directory of the project, run a script who make something like this :

# Access the beta.git directory

 cd /var/repo/beta.git



# Run command for updating repo

 git checkout master && git up 



# If we access the beta folder in apache area we should see index.html

 cd /var/www/beta
 ls
 --index.html

ACTUAL RESULTS

No result.

ERROR MESSAGES

No error messages.

REQUEST

How can I set up a process like this one ?

There is something in my process I did not take in consideration ?

Yohan W. Dunon
  • 502
  • 10
  • 18

0 Answers0