0

I search the method to copy files to "test" host when I launch the "git push" command.

git push   ------- TO REPO --> REPO_SERVER
           \
            \_________ TO DIR --> Host_TEST

Git Version: 2.20.1

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
CH06
  • 139
  • 2
  • 14
  • Use a [`post-receive` or `post-update` hook](https://stackoverflow.com/a/18804805/7976758) either at the `REPO_SERVER` or create a bare repo at the `test` server. It's called push to deploy: https://stackoverflow.com/search?q=%5Bgithooks%5D+%22post-update+hook%22+push+to+deploy, https://stackoverflow.com/search?q=%5Bgithooks%5D+%22post-receive+hook%22+push+to+deploy. Like this: https://stackoverflow.com/a/3387030/7976758 – phd May 19 '21 at 14:38

1 Answers1

1

It sounds like you are trying to (re) invent CI/CD. If you are using GitHub or GitLab as a remote server you can use Pipelines (or Actions in GitHub). In there you can define (almost) anything you want to happen after a push, I am assuming your Host_TEST is accessible online.

In case you are running your own git server
You can implement "push to deploy" using the post-receive hook. Hooks are scripts that are placed inside .git/hooks and executed at a precise phase of a git command. you can find several example implementations in .git/hook. See here for more information: Setting up Push-to-Deploy with git

In case you don't have access to your own git server
You can use the pre-push script on your local machine, BUT THIS IS A BAD IDEA
This hooks is executed after you execute git push but before git actually pushes anything. If your script fails (i.e non-zero return code) it will not push. Also, if your script manages to copy but then git fails to push you will end up testing code that's not in your repo.

If all this sound way too complicated
You can create a bash function that does both operations and add it to your .bashrc.

Here is an example:

push_copy() {
   if git push
   then 
      # Copy for command here: scp ...
   else
     echo "Failed..."
   fi
}
Moha the almighty camel
  • 4,327
  • 4
  • 30
  • 53