Questo contenuto non è disponibile nella lingua selezionata.
Chapter 38. Git hooks and remote Git repository integration
Git hooks are bash scripts that execute before or after Git events such as git commit or git push. In Business Central, you can use Git hooks to configure repositories to trigger specified actions every time events happen. For more information about Git hooks, see Customizing Git Hooks.
You can integrate remote Git repositories with Business Central by using post-commit Git hooks. This enables you to automate content replication between Business Central and remote repositories. For example, you can implement a real-time backup strategy where changes you make to your Business Central projects are replicated to your remote Git repositories.
Business Central only supports post-commit Git hooks.
A post-commit Git hook executes after every commit as a sync operation. Business Central waits for the post-commit bash to complete and no other write operation occurs in the repository.
38.1. Creating post-commit Git hooks Copia collegamentoCollegamento copiato negli appunti!
You can create a post-commit Git hook bash script file that executes code contained in that file or execute code from a different file such as a Java program.
Procedure
Create a
post-commitGit hook file:touch post-commit
$ touch post-commitCopy to Clipboard Copied! Toggle word wrap Toggle overflow Set the permissions of the
post-commitfile to755:chmod 755 post-commit
$ chmod 755 post-commitCopy to Clipboard Copied! Toggle word wrap Toggle overflow Add
#!/bin/bashand any required code to thepost-commitfile, for example:To push all changes to a remote repository:
#!/bin/bash git push origin +master
#!/bin/bash git push origin +masterCopy to Clipboard Copied! Toggle word wrap Toggle overflow To log a message:
#!/bin/bash echo 'Hello World'
#!/bin/bash echo 'Hello World'Copy to Clipboard Copied! Toggle word wrap Toggle overflow To execute code of another file:
#!/bin/bash java -jar _EAP_HOME_/bin/.niogit/<SPACE>/<PROJECT_NAME>.git/hooks/git-push.jar
#!/bin/bash java -jar _EAP_HOME_/bin/.niogit/<SPACE>/<PROJECT_NAME>.git/hooks/git-push.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow NoteTo use post-commit Git hooks that execute Java code, you must use the following Java libraries:
- JGit: Used to interact with internal Business Central Git repositories.
- GitHub API for Java: Used to communicate with GitHub.
For more information about post-commit Git hook and Java code examples, see Business Central post-commit Git Hooks Integration.
38.2. Importing remote Git repositories Copia collegamentoCollegamento copiato negli appunti!
You can import a remote Git repository in to Business Central and configure a post-commit Git hook to automatically push changes to that remote repository.
Prerequisites
- Red Hat Decision Manager is installed in a Red Hat JBoss EAP 7.3 server instance.
- Red Hat Decision Manager projects exist in an external Git repository.
- Read access credentials for the external Git repository.
-
(For Windows) Cygwin is installed with the Git package added during installation and the path to the Cygwin
/binfolder is added to your environmentPATHvariable. For example,C:\cygwin64\bin. For more information about Cygwin installation, see Installing and Updating Cygwin Packages.
Procedure
-
In Business Central, go to Menu
Projects. - Select or create the space that you want to import the Git projects into.
-
Click
on the right side of the screen and select Import Project.
-
In the Import Project window, enter the URL of your Git repository, for example,
https://github.com/USERNAME/REPOSITORY_NAME.git, and the credentials for the Git repository. Click Import.
The project is added to the Business Central Git repository and is then available in the space.
ImportantUse the HTTPS or Git protocol instead of a SCP-style SSH URL. Business Central does not support the basic SSH URL and an error appears if you use this URL.
You must have your public ssh key configured in your Git provider.
The Git repository must be a KJAR project, containing only a single KJAR that is compatible with the Red Hat Decision Manager version. The KJAR content must be in the root of the repository.
In a command terminal, navigate to the
hooksfolder located in the repository Git folder of the project. For example:cd _EAP_HOME_/bin/.niogit/<SPACE>/<PROJECT_NAME>.git/hooks
$ cd _EAP_HOME_/bin/.niogit/<SPACE>/<PROJECT_NAME>.git/hooksCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
post-commitfile that pushes changes to the remote Git repository. For example:#!/bin/sh git push origin +master
#!/bin/sh git push origin +masterCopy to Clipboard Copied! Toggle word wrap Toggle overflow For more information about creating post-commit Git hooks, see Section 38.1, “Creating post-commit Git hooks”.
Optional: To check that the configuration was successful, create a guided rule in Business Central:
-
In Business Central go to Menu
Projects Add Asset Guided Rule. - On the Create new Guided Rule page, enter the required information.
Click Ok.
Business Central automatically pushes all changes to the remote repository.
-
In Business Central go to Menu
Additional resources
38.3. Configuring Git hooks for existing remote Git project repositories Copia collegamentoCollegamento copiato negli appunti!
If you have an existing remote Git repository project you can create a post-commit Git hook in a remote Git repository of that existing project and integrate the remote Git repository with Business Central.
Prerequisites
- Red Hat Decision Manager is installed in a Red Hat JBoss EAP 7.3 server instance.
- Red Hat Decision Manager projects exist in an external Git repository.
- Read access credentials for the external Git repository.
-
(For Windows operating system) Cygwin is installed with the Git package added during installation and the path to the Cygwin
/binfolder is added to your environmentPATHvariable. For example,C:\cygwin64\bin. For more information about Cygwin installation, see Installing and Updating Cygwin Packages.
Procedure
In a command terminal, navigate to the
hooksfolder located in the repository Git folder of the project. For example:cd _EAP_HOME_/bin/.niogit/<SPACE>/<PROJECT_NAME>.git/hooks
$ cd _EAP_HOME_/bin/.niogit/<SPACE>/<PROJECT_NAME>.git/hooksCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
post-commitfile that pushes changes to the remote Git repository. For example:#!/bin/sh git push origin +master
#!/bin/sh git push origin +masterCopy to Clipboard Copied! Toggle word wrap Toggle overflow For more information about creating post-commit Git hooks, see Section 38.1, “Creating post-commit Git hooks”.
Optional: To check that the configuration was successful, create a guided rule in Business Central:
-
In Business Central go to Menu
Projects Add Asset Guided Rule. - On the Create new Guided Rule page, enter the required information.
Click Ok.
Business Central automatically pushes all changes to the remote repository.
-
In Business Central go to Menu
38.4. Configuring Git hooks as a system property for Business Central Copia collegamentoCollegamento copiato negli appunti!
If you do not have an existing Git repository project or if you want to apply post-commit Git hooks to a large number of project repositories you can specify a directory containing a hook file for the value of the org.uberfire.nio.git.hooks system property. This directory is copied to the Git repositories.
If you specify the org.uberfire.nio.git.hooks system property, all Business Central internal repositories and project repositories use the post-commit Git hook. You should only use fully qualified paths in your script.
Prerequisites
- Red Hat Decision Manager is installed in a Red Hat JBoss EAP 7.3 server instance.
-
(For Windows operating system) Cygwin is installed with the Git package added during installation and the path to the Cygwin
/binfolder is added to your environmentPATHvariable. For example,C:\cygwin64\bin. For more information about Cygwin installation, see Installing and Updating Cygwin Packages.
Procedure
Create a post-commit Git hook in a directory on your local system.
For more information about creating post-commit Git hooks, see Section 38.1, “Creating post-commit Git hooks”.
To specify the directory with the hook file for the value of the
org.uberfire.nio.git.hookssystem property, do one of the following tasks:Add the
org.uberfire.nio.git.hookssystem property to thestandalone.xmlfile. For example:<system-properties> <property name="org.uberfire.nio.git.hooks" value="_EAP_HOME_/hooks"> </property> ... </system-properties>
<system-properties> <property name="org.uberfire.nio.git.hooks" value="_EAP_HOME_/hooks"> </property> ... </system-properties>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
-Dorg.uberfire.nio.git.hooksenvironment variable when executing Business Central. For example:./standalone.sh -c standalone-full.xml -Dorg.uberfire.nio.git.hooks=_EAP_HOME_/hooks
$ ./standalone.sh -c standalone-full.xml -Dorg.uberfire.nio.git.hooks=_EAP_HOME_/hooksCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Start Business Central.
The post-commit Git hook is copied to all Business Central internal repositories and project repositories.
Additional resources
38.5. Integrating remote Git repositories Copia collegamentoCollegamento copiato negli appunti!
In the following example, you use a post-commit Git hook and Java code to integrate Business Central with a remote Git repository. For the Java code example, see Business Central post-commit Git Hooks Integration. The example provides the following functionality:
-
Automatic generation of the template
.gitremoteconfiguration file -
Validation of the
.gitremoteconfiguration file for required parameters -
Patterns defined in the ignore parameter of the
.gitremotefile are ignored by Git - Message and notification output to users
- Support for GitLab and GitHub token authentication
- Support for GitLab group and subgroup project creation
- Support for GitHub organization repository creation
Prerequisites
- Red Hat Decision Manager is installed in a Red Hat JBoss EAP 7.3 server instance.
- Java Development Kit (JDK) 8 is installed.
- Maven is installed.
Procedure
In a terminal window, clone the GitHub repository to your system:
git clone https://github.com/kiegroup/bc-git-integration-push.git
$ git clone https://github.com/kiegroup/bc-git-integration-push.gitCopy to Clipboard Copied! Toggle word wrap Toggle overflow Navigate to the cloned repository:
cd bc-git-integration-push
$ cd bc-git-integration-pushCopy to Clipboard Copied! Toggle word wrap Toggle overflow Execute a Maven clean install:
mvn clean install
$ mvn clean installCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a
/hooksfolder in yourEAP_HOMEdirectory:mkdir -p _EAP_HOME_/hooks/
$ mkdir -p _EAP_HOME_/hooks/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Copy the
git-push-2.1-SNAPSHOT.jarto theEAP_HOME/hooks/folder:cp bc-git-integration-push/target/git-push-2.1-SNAPSHOT.jar _EAP_HOME_/hooks/
$ cp bc-git-integration-push/target/git-push-2.1-SNAPSHOT.jar _EAP_HOME_/hooks/Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: To create a template
.gitremoteconfiguration file, rungit-push-2.1-SNAPSHOT.jar:java -jar git-push-2.1-SNAPSHOT.jar
$ java -jar git-push-2.1-SNAPSHOT.jarCopy to Clipboard Copied! Toggle word wrap Toggle overflow Example template
.gitremoteconfiguration fileCopy to Clipboard Copied! Toggle word wrap Toggle overflow Modify the
.gitremoteconfiguration file parameters.Expand Table 38.1. Example .gitremote parameters Parameter Description providerThe Git provider. Only two values are accepted: GIT_HUB and GIT_LAB. Required
loginThe user name for the Git provider. Required
passwordA plain text password. Not required if a
tokenis provided.tokenA generated token to replace the
usernameandpasswordbased unsecured connection. Note: If this is not set a warning is displayed that you are using an unsecured connection. Not required if apasswordis provided. Note: GitLab only supports token authentication.remoteGitUrlA public provider URL or a locally hosted enterprise for any provider. Required. Note: The public GitHub URL should be the API URL. For example, api.github.com.
useSSHBoolean to allow the SSH protocol to push changes to the remote repository. Optional. Default = false. Note: This parameter uses the local
~/.ssh/directory to obtain the SSH configuration.ignoreA comma separated regular expressions to ignore project names that match any of these expressions. Optional.
githubOrgDefines the repository organization if GitHub is used as the provider. Optional.
gitlabGroupDefines the repository group and subgroup if GitLab is used as the provider Optional.
Create a
post-commitGit hook file inEAP_HOME/hooks:touch post-commit
$ touch post-commitCopy to Clipboard Copied! Toggle word wrap Toggle overflow Set the permissions of the
post-commitfile to755:chmod 755 post-commit
$ chmod 755 post-commitCopy to Clipboard Copied! Toggle word wrap Toggle overflow Add
#!/bin/bashand code to executegit-push-2.1-SNAPSHOT.jarto thepost-commitfile:echo "#\!/bin/bash\njava -jar $APP_SERVER_HOME/hooks/git-push-2.1-SNAPSHOT.jar" > hooks/post-commit
$ echo "#\!/bin/bash\njava -jar $APP_SERVER_HOME/hooks/git-push-2.1-SNAPSHOT.jar" > hooks/post-commitCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start Business Central with the
-Dorg.uberfire.nio.git.hooksenvironment variable set. For example:./standalone.sh -c standalone-full.xml -Dorg.uberfire.nio.git.hooks=_EAP_HOME_/hooks
$ ./standalone.sh -c standalone-full.xml -Dorg.uberfire.nio.git.hooks=_EAP_HOME_/hooksCopy to Clipboard Copied! Toggle word wrap Toggle overflow
To use post-commit Git hooks that execute Java code, you must use the following Java libraries:
- JGit: Used to interact with internal Business Central Git repositories.
- GitHub API for Java: Used to communicate with GitHub.
For more information about post-commit Git hook and Java code examples, see Business Central post-commit Git Hooks Integration.
38.6. Git hook exit codes Copia collegamentoCollegamento copiato negli appunti!
When a Git hook exits an integer value is returned which determines the status of the Git hook execution. This integer value is known as a Git hook exit code. The execution status can be a success (1), warning (2 to 30) or error (31 to 255).
38.7. Customizing Git hook notifications Copia collegamentoCollegamento copiato negli appunti!
Business Central provides a mechanism that enables users to receive customized Git hook notifications based on the hook exit codes.
To enable the notification mechanism you must create a *.properties file containing the custom messages and then specify the path to that file as the value of the appformer.git.hooks.bundle system property.
Procedure
Create the
*.propertiesfile and add a line for each exit code with a corresponding message in the following format:<exit_code>=<display_message>The
<exit_code>is the Git hook exit code and the<display_message>is the custom message that is displayed to a user.For example:
0=Success! All working as expected. 1=Warning! Please check the logs and advise your admin. . . 31=Error! Please advise your admin immediately.
0=Success! All working as expected. 1=Warning! Please check the logs and advise your admin. . . 31=Error! Please advise your admin immediately.Copy to Clipboard Copied! Toggle word wrap Toggle overflow NoteIt is not necessary to define all the possible exit codes in the *.properties file. Notifications appear only for the exit codes defined in the *.properties file.
ImportantThe notification service only supports the
ISO 8859-1(LATIN 1) character set in the properties file. If you want to use extended characters, please use their escaped Unicode character code sequences.To enable Git hook notifications, specify the path to the file as the value of the
appformer.git.hooks.bundlesystem property.See the following example of a
standalone.xmlfile with the setting that points to aMessages.propertiesfile:<system-properties> <property name="appformer.git.hooks.bundle" value="/opt/jboss-as/git-hooks-messages/Messages.properties"> </property> ... </system-properties>
<system-properties> <property name="appformer.git.hooks.bundle" value="/opt/jboss-as/git-hooks-messages/Messages.properties"> </property> ... </system-properties>Copy to Clipboard Copied! Toggle word wrap Toggle overflow
38.7.1. Git hook notifications in Business Central Copia collegamentoCollegamento copiato negli appunti!
You can view Git hook notifications in Business Central. There are three Git hook exit code notification types.
| Exit code | Customized message | UI notification color |
|---|---|---|
|
| Success! All working as expected. | Green |
|
| Warning! Please check the logs and advise your admin. | Orange |
|
| Error! Please advise your admin immediately. | Red |
UNIX machines only support error codes between 0 (success) to 255 (error), any exit code outside of this range will end up being converted into a different code which may cause showing a wrong notification message.
Windows machines don’t have this limitation and support a wide range of exit codes.
38.7.2. Git hook notification internationalization support Copia collegamentoCollegamento copiato negli appunti!
You can internationalize notification messages by placing additional properties files in the same path as the original properties file specified as the appformer.git.hooks.bundle system property.
The name of the different localized files must be <filename>_<lang>.properties, where the <filename> is the same as the original. For example, where the system property points to Messages.properties, you can create Messages_en.properties for English, Messages_fr.properties for French, or Messages_it.properties for Italian.
The notification service will choose the properties file based on the user’s language, if there are no available translations for that language it will use the entries from the original Messages.properties file.