Simple Local CI installation for your Android team

Tram Ho

Hi everyone, yesterday I have instructions on using ktlint and detekt to check code style, lint for android . Today, continue to improve the code series , I continue to share about how to run the above tools automatically when commit code .

OK, let’s get started!

Git Hooks

In the past, I always wanted to find a tool to help eliminate basic git errors and coding convention before sending PR , like commit messages according to company standards, the code must be formatted in general, the unit test must be run pass. local or lint errors must be handled … find it often so I have to remind you, sometimes remind it to unload. Because everyone’s code is managed on a personal computer, whoever does anything intervenes, I think I will always be hated because of my interest every day, repeating things like “Yeah I know, it is hard, say forever “. And then…

As if I accidentally picked up a secret, I stumbled upon a way to check the keyword commit message rule git and git hooks appeared as salvation. With git hooks , all my problems have been completely solved.

What are git hooks?

Like other version control systems, Git also provides us with a way to interfere with some of its special processes with custom scripts , namely hook .

How do Git hooks become saviors?

I immediately selected 2 client hook suitable for checking commit message rules , and running tools / tasks that automatically code style , lint , unit tests before committing .

  1. pre-commit : This hook is called first, executed before you enter the content for the commit message . This hook is used to check the contents of the committed files. You can write scripts to test code convention , run tests or run static analysis before committing . If the script returns greater than zero, the commit will be aborted.
  2. commit-msg : It is called after we have entered the message content for the commit . Suitable for normalizing commit messages . The only parameter that is received in this hook is the file name containing the commit message . Similarly, if this script returns a result greater than zero, the commit will be canceled.

Setting

Create a folder team-props with the following structure:

Note: if you change the name or directory structure, the steps below you have to manually search and fix the path to the files according to your directory.

Create script checking commit message ( commit-msg.sh )

For Linux:

For Mac:

You can replace regex as desired, currently I am following the rules of the project .

For example, the following commits will be valid:

Create scripts that automatically run gradle tasks and Unit tests ( pre-commit.sh )

In the above code, I combined the use of gradle tasks in the lesson using ktlint và detekt để kiểm tra code convention và code smell .

At any point, I stop at that moment. Commit only succeeds when all the above steps succeed.

Note: the syntax of unit run tests is as shown below, you can customize the way the project is operating.

Copy Hooks to git hooks folder

Now let’s create a gradle task to copy hooks into the correct .git/hooks project directory.

In the above code , there will be 2 tasks : copyGitHooks and installGitHooks , 2 tasks will automatically run each time you clean the code . Therefore, only if you run the clean project at least once will the git hooks (changes) be copied and run properly.

Once you have created this task , you should add it to the build.gradle level project :

Test results

Now clean the project once, then try to edit and make a commit . Celebrate if the gradle task is successfully run! (beer) (beer) (beer).

Conclusion

Ok, so I have instructed you to create a small local CI , help check through the code of your team members for code style, lint, unit test before creating Pull Request . Hopefully the article will benefit everyone.

Reference: I will share the detailed code soon when I have time.

HAPPY CODING!

Share the news now

Source : Viblo