Introduction
In this tutorial, we will see how to use RHPAM Git Hooks push all changes from a project to a remote repository in GitHub, with a script dedicated to a project, and how to define a default git hook to execute with all projects, this one will log information from projects in a file.
NOTE this tutorial has some git, Linux, and EAP commands. As the focus of this tutorial is not these commands it is not explained, if you need more details about these commands, look the references.
Environment
- OS: Linux Fedora 29
- RHPAM 7.11.1
- JBoss EAP 7.3.7
Push changes to remore repository
First we will work with the push to GitHub, in the next steps we will create a new GitHub repository, a new project in RHPAM, and a script to be run and push the changes to the remore repository
Create and configure the repositories
To RHPAM can connect a project with a remote repository, the project and the repository must have the same name.
Here we will create the project, the repository and will configure the local repository to have access to the remote repository.
-
Create a git repository with any name, eg postHook
-
Create a project with the same name, eg postHook
-
Go to the repository of the project:
$ cd $JBOSS_HOME/bin/.niogit/MySpace/postHook.gitWhere
- $JBOSS_HOME is the path to JBoss EAP installation
- MySpace is the space name where the project was created
- postHook is the project name
-
Add the git repository as remote in the local project repository
$ git remote add origin git@github.com:/.git -
Check if the remote was set correctly
$ git remote -v origin git@github.com/ribeirorvs/postHook.git (fetch) origin git@github.com/ribeirorvs/postHook.git (push)
Create the post-commit script
Now we will create and set the correct permissions to the script in the project hooks directory.
-
Go to git hook directory
$ cd $JBOSS_HOME/bin/.niogit/MySpace/postHook.git/hooks -
Create a file named post-commit
$ touch post-commit -
By default the file will be created with the permissions -rw-rw-r–, but we need the permissions -rwxr–r–. Use the command below to change the permission
$ chmod 744 post-commit $ ll post-commit -rwxr--r--. 1 roribeir roribeir 0 Sep 22 12:03 post-commit
Configure the githook script
The post-commit file created is the file where the RHPAM will find the executable commands to run in post commit git phase. Here we will explore some options about how to configure it, but it is possible to include any complex executable commads here.
As the commands is not the scope of this post, I will only shows some options on how to configure this file.
Push the changes to remote repository
Using a text editor, include the commands below in the post-commit file:
#!/bin/sh
git push origin master
Now, every time you save any change in the project using the Business Central, this change will be reflected to the GitHub repository
Create a log file with commit details
It is possible to create a post commit that will be executed in any projects, including when a project is creating.
For this example, we will create a directory inside the EAP installation path, named hooks, to store the script and the log files.Create and configure the repositories
Create needed files
-
Go to EAP installation path
$ cd $JBOSS_HOME -
Create the files
$ touch post-commit post-commit.logs $ ll -rw-rw-r--. 1 roribeir roribeir 0 Nov 1 02:36 post-commit -rw-rw-r--. 1 roribeir roribeir 0 Nov 1 02:36 post-commit.logs
Edit the log script
As mentioned above, the script can contain any shell commands, in this case, we will take details about the commits from a project and will save it in a file. The example below shows how to take this list of information from the commit
- Banch name
- Commit comment
- Commit hash (only the firsts characteres)
- Commit author
- Commit date
#!/bin/bash
BRANCH=$(git for-each-ref --count=1 --sort=-committerdate refs/heads/ --format='%(refname:short)')
LOGDIR=/opt/jboss/jboss-eap-7.3/hooks/post-commit.log
COMMIT=$(git log $BRANCH -1 HEAD --pretty=tformat:'Commit: %h' )
AUTHOR=$(git log $BRANCH -1 HEAD --pretty=tformat:'Author: %an' )
DATE=$(git log $BRANCH -1 HEAD --pretty=tformat:'Date: %ad' )
COMMENT=$(git log $BRANCH -1 HEAD --pretty=tformat:'Comment: %s%n' )
REPO=$PWD
echo "" >> $LOGDIR
echo "----------------------------------------------------" >> $LOGDIR
echo "REPO: $REPO" >> $LOGDIR
echo "BRANCH: $BRANCH" >> $LOGDIR
echo $COMMIT >> $LOGDIR
echo $AUTHOR >> $LOGDIR
echo $DATE >> $LOGDIR
echo $COMMENT >> $LOGDIR
This script will generate logs in the file like this
----------------------------------------------------
REPO: /path/to/project
BRANCH: master
Commit: af59d48
Author: Rodrigo Vitor Ribeiro
Date: Thu Oct 3 12:47:44 2019 +0200
Comment: Commit comment message.
Turn the post hook default to all projects
With the script created we will configure the RHPAM to find and use the script as the post commit hook.
To do that, include the property org.uberfire.nio.git.hooks and the value is the path to the script file. It is possible to do with two ways, using the CLI and editing the standalone file directly.
To change with the CLI use this steps
-
Start the server
-
With another terminal, go to server bin directory
$ cd $JBOSS_HOME/bin -
Run the jboss-cli.sh script
$ .jboss-cli.sh -c -
Add the system property
[standalone@localhost:9990 /] /system-property=org.uberfire.nio.git.hooks:add(value=/path/to/jboss/eap/hooks/post-commit) {"outcome" => "success"} [standalone@localhost:9990 /] /system-property=org.uberfire.nio.git.hooks:read-resource { "outcome" => "success", "result" => {"value" => "/home/roribeir/rhpam-7110/hooks"} }
To change in the standalone, with the server stopped, add the line below to the ... block
NOTE: as this script will be executed after you have a chance to create the remote URL in the project, the push to remote git repository will work only after you follow the steps #3 and #4 of Create and configure the repositories
