How to conditionally skip maven tests on Jenkins for CI

We use GitLab and Jenkins for CI. Whenever a merge request is approved on a branch master

, GitLab sends a request to Jenkins to perform a build job, which, upon successful completion, is deployed to tomcat.

The build job checks the files from the gitlab branch master

, builds the project with, clean install

and saves the artifacts.

There are many cases where commits are property file updates, html file updates, and we don't have any tests for those files.

I know that there is a switch to miss all the tests at the time mvn clean install

, but is there a way to conditionally skip the test if commits only include files that are css, js, properties, html, etc.?

Thank!

+3


source to share


2 answers


If you are using Jenkins GIT plugin , then you can link in lines to the current and previous builds the last command with the GIT_PREVIOUS_COMMIT and GIT_COMMIT variables. Combine it with the GIT diff command to have a list of all changed files since the previous build:

git diff --name-only $GIT_PREVIOUS_COMMIT $GIT_COMMIT

      



Then, using the grep command , you can check if there are files with other extensions than the ones you mentioned. If so, you can execute the correct Maven command line.

+2


source


Following @ PiotrOktaba answer (line breaks in commands for better readability):

~/git/test  (master)$ git diff --name-only $GIT_PREVIOUS_COMMIT $GIT_COMMIT
test.txt

~/git/test  (master)$ git diff --name-only $GIT_PREVIOUS_COMMIT $GIT_COMMIT |
    grep -vE '.*\.css|.*\.js|.*\.properties|.*\.html|.*\.txt' | wc -l
      0

      

There are no other files in the commit and rarr files other than the ones listed. skip tests.



~/git/test  (master)$ git diff --name-only $GIT_PREVIOUS_COMMIT $GIT_COMMIT
test.java
test.txt

~/git/test  (master)$ git diff --name-only $GIT_PREVIOUS_COMMIT $GIT_COMMIT |
    grep -vE '.*\.css|.*\.js|.*\.properties|.*\.html|.*\.txt' | wc -l 
      1

      

The file other than the one specified is in the commit file -> don't skip tests.

Use this in a conditional build step that checks the return value of a Bash script.

+1


source







All Articles