Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create a jenkins job, who must execute test on commit?

I'm beginning with Jenkins.

I want, that each time I do a git commit (or push?), that the jasmine test of my ionic project was executed and must work before the commit can be done.

In reality, it has 2 questions:

  • How execute jasmine test with Jenkins?

In this moment I execute the test with:

npm test
  • How can I do for executing this tests with a commit (or a push)?

Thanks Best regards

like image 447
anubis Avatar asked Oct 17 '22 10:10

anubis


1 Answers

You have two ways to achieve the task.

  1. GIT Hook: From GIT after commit or push execute the Jasmine test
  2. Jenkins Trigger with GIT Hook: From Jenkins check the repo and execute the Jasmine test

Hooks from GIT

Look for the hidden directory in your git repo, you'll find a directory called "hooks" and inside it many examples of hooks:

First list the content of your repo main directory:

ls -ltra

You should see something like:

m.ortiz.montealegre@CPX-XYR3G1DTHBU ~/-argentina/.git
$ vim hooks/
applypatch-msg.sample      pre-applypatch.sample      pre-push.sample            update.sample
commit-msg.sample          pre-commit.sample          pre-rebase.sample
post-update.sample         prepare-commit-msg.sample  pre-receive.sample

You have a whole guide of how to setup hooks here.

In your case maybe update would do the thing:

update The update script is very similar to the pre-receive script, except that it’s run once for each branch the pusher is trying to update.

Triggers from Jenkins with GIT Hooks

In this one you'll setup your Jenkins project build trigger with "Poll SCM" but do not specify a schedule.

Then with a post-receive hook from GIT notify the Jenkins Job about the changes:

http://yourserver/jenkins/git/notifyCommit?url=<URL of the Git repository>?token=<get token from git to build remotely>

I found that example here.

Run the Jasmine tests

I don't know which O.S you're using but I hope it's a beautiful Linux box.

You can achieve pretty much the same with Jenkins. You need to consider the user (your user) and its permissions and check if the user which runs the Jenkins instance is allowed to execute the same.

Just create a new Jenkins Project and add a shell execution step with the test just like you said:

npm test

There are many questions regarding your particular environment, but I think that this will be a good guide for you.

like image 144
Miguel Ortiz Avatar answered Oct 20 '22 00:10

Miguel Ortiz