Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure gitlab ci + nexus release for Maven projects

Hi I started using the gitlab ci for my CI and CD. I am using Nexus for storing my jar and wars.

gitlab config

stages:
    - build
    - package


services:
    - name: mongo:3.2.4
      alias: mongodb


variables:
    mongodb_hosts: "mongodb"

build_maven:
    image: maven:3-jdk-8
    stage: build
    script:
        - echo $pwd
        - "./docker/wait-for-it.sh mongodb:27107 -t 30 -- git checkout master && mvn -B -Dresume=false release:prepare release:perform"

    artifacts:
        paths:
            - target/*.jar
    only:
        - master

This works fine, But it triggers the build circular way. Because I am using maven release plugin for release maven artefacts.

The release plugin changes the changes the snapshot and commits back to gitlab again. in that time git lab triggers builds again, so it because circular trigger.

How to handle this properly? I like to achieve the following.

when it build the master branch, I like to release the final version to nexus and increase the version in pom file automatically like the release plugin does.

like image 209
vimal prakash Avatar asked Aug 19 '17 11:08

vimal prakash


2 Answers

I have the following job for releasing an artifact:

Release Maven:
  stage: release
  image: maven:3-jdk-8
  script:
    - git checkout master
    - git reset --hard "origin/master"
    # Gitlab clones as HTTPS and there's no plan to support SSH
    # The SSH key needed is built into the custom Maven image
    - git remote set-url origin $SSH_GIT_URL
    - mvn --batch-mode clean release:prepare release:perform -Dresume=false -DautoVersionSubmodules=true -DdryRun=false -Dmaven.test.skip=true -DskipITs -DscmCommentPrefix="[ci skip]"

The main attribute here is -DscmCommentPrefix="[ci skip]" - this creates a commit message with a prefix, that prevents the gitlab-ci from starting a new pipeline

like image 143
Alexey Khisamutdinov Avatar answered Nov 03 '22 00:11

Alexey Khisamutdinov


You can use gitlab ci except variables:

release:
  stage: release
  only:
    - /^(release|RELEASE).*/
  except:
     variables:
        - $CI_COMMIT_MESSAGE =~ /maven-release-plugin/
  script:
    - git checkout -B "$CI_COMMIT_REF_NAME"
    - mvn -B release:prepare ...

More info about variables: https://docs.gitlab.com/ee/ci/variables/

like image 21
S. Toledano Avatar answered Nov 02 '22 23:11

S. Toledano