czwartek, 26 listopada 2015

Gitlab CI and Maven

Using GitLab CI for building Maven/Java

Hello,
I've finally found some time to write this post. Quite recently we upgraded our GitLab CE to wersion 8.x, which ships with bundled Continuous Integration module. I wanted to give it a try and use it to build Java application in Maven project format, however I didn't found usefull example how to do it. Now I feel compelled to fill this gap.

I assume that you have working GitLab CE version 8.1 or newer. It already has bundled "CI coordinator service" so there is no need to install any component on GitLab itself. Just open https://your-gitlab/ci/admin/projects to see its there. Only thing you have to do is to set up runners, which do the actual work. You can install runners on the same machine as GitLab, on dedicated server or even on your own PC - more about runners can be found in Runners README

Here are the steps which I did - I installed "GitLab CI Multi Runner", which is written in GO on dedicated Ubuntu 14.04 LTS VM:

1. If you're using self-signed certificates, than you have to import root certficate to ubuntu system-wide trust store:
    1. Copy cert to: /usr/local/share/ca-certificates/yourcompany-root.crt 
    2. Run: 
$ sudo update-ca-certificates 
2. Install Java 8 JDK:
$ sudo add-apt-repository ppa:webupd8team/java
$ sudo apt-get update
$ sudo apt-get install oracle-java8-installer
3. Install Maven:
$ sudo apt-get install maven
4. Install Runner:
$ curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash$ sudo apt-get install gitlab-ci-multi-runner
5. Register runner in your gitlab
$ sudo gitlab-ci-multi-runner register
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/ci):
https://your-gitlab-fqdn/ci
Please enter the gitlab-ci token for this runner:
TOKEN
Please enter the gitlab-ci description for this runner:
[your-runner-hostname]:
Please enter the gitlab-ci tags for this runner (comma separated):
linux,java,maven,java8
INFO[0040] 59ec3e06 Registering runner... succeeded
Please enter the executor: ssh, shell, parallels, docker, docker-ssh:
shell
INFO[0044] Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
Token needed for runner registration can be found on GitLab CI runners page: https://your-gitlab/ci/admin/runners. In the example above, I've set up runner with shell executor. You can set up your own to use Docker if you want.

Now everything is set up and you should see your runner on the runners page in GitLab CI. To build your project, you need to add file called .gitlab-ci.yml to root directory in git repository. It's content can be like this
maven-build:
  script:
    - mvn clean integration-test
Give it a try! Commit .gitlab-ci.yml to master and you should see that your commit is picked up by CI and built using maven. You should further customise YML to suit your needs. If you have more runners, than you should point that this job must be executed by runner with tag "maven" and "java". Have fun using your GitLab CI!


1 komentarz:

  1. Thank you so much for this! This worked perfectly and is much nicer than a docker-based runner. :)

    OdpowiedzUsuń