Does the pre-merge commit hook get executed when I click merge request button in github?

Viewed 1716

I want to update the package version automatically when I create a pull request from release branch to master and after that I want whenever I merge it, the pre-merge git hook will be executed to launch another script.

pre-merge-commit:

cd my_app
    node ./hooks/post-commit-version
    RETVAL=$?

    if [ $RETVAL -ne 0 ]
    then
       exit 1
    fi

hooks/post-commit-version:

#!/usr/bin/env node
const exec = require('child_process').exec;
const path = require('path');
const moment = require('moment');
const fs = require('fs');

function getBranch(){
    return new Promise((resolve, reject) =>{
        exec(
            "git branch | grep '*'",
            function (err, stdout, stderr) {
                if(err)reject(err)
                const name = stdout.replace('* ','').replace('\n','');
                resolve(name)
            }
        )
    });
}

getBranch()
    .then((branch) => {
        if(branch === 'release') {
          const currentDate = moment().format('YY.MM.DD')
        var pathToFile = path.join(__dirname, "../package.json");

        if (fs.existsSync(pathToFile)) {
          const data = fs.readFileSync(pathToFile, 'utf-8')
          const content = JSON.parse(data);
          content.version = currentDate;
          fs.writeFileSync(pathToFile, JSON.stringify(content, null, 2), 'utf8');
          exec(`git add ${pathToFile}`, (err, stdout, stderr) => {
            if(err) console.log(err)
           console.log(stdout)
          })
      } else {
          console.log("Cannot find file : " + pathToFile);
          return;
        }
      }
        return;
    })
    .catch(error => {
      console.log(error)
    })

When I try this locally, with pre-commit hook and execute the git commands manually, it works successfully and update the repository in github as the one I want it to be. But I'm not sure that git hooks are executed in Github server when I click the merge request button.

1 Answers

The short answer is no.

Hooks are tied to one specific repository and are not transferred by Git operations.1 Any hooks you set up in your repository are not set up in some other repository. So the hook you have in your repository acts in and on your repository, but if you have a second clone elsewhere, it does not act in and on that second clone.

Besides this, GitHub use a different mechanism ("GitHub Actions") and just don't let you put any hooks into their repositories in the first place.


1If your OS provides symbolic links, you can (manually, once per clone) install a symlink as a Git hook, with the symlink pointing to a file in the work-tree for your repository. In this way, you can get a hook that is affected by various operations: since the hook's actual executable code lives in your work-tree, things that affect that file in your work-tree affect the hook.

Similarly, on OSes that don't provide symbolic links, you can (manually, once per clone) install a hook script-or-binary that works by running a script-or-binary out of your work-tree. That is, rather than relying on the OS's symbolic link mechanism to run the file directly from your work-tree, you write a hook whose "run" operation consists of "run file from work-tree and use its exit status as the hook's exit status".

Related