简体   繁体   中英

How to separate test and build stage with maven in gitlab-ci?

I want to separate the maven stages to build , test and deploy .

Question: am I over complicating things here? Should I maybe just use a mvn clean package stage, because compile and test are executed implicit by maven during package phase?

.gitlab-ci.yml :

stages:
  - build
  - test
  - deploy

build:
  stage: build
  script: mvn clean compile

test:
  stage: test
  script: mvn clean test

deploy:
  stage: deploy
  script: mvn clean package -Dmaven.test.skip=true
  #...continue with docker deployment...

There are two questions in the post.

How to separate test and build stage with maven in gitlab-ci?

This is possible and can be done like this, as mentioned in GitLab maven example :

image: maven:latest

variables:
  MAVEN_OPTS: >-
    -Dhttps.protocols=TLSv1.2
    -Dmaven.repo.local=.m2/repository
    -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN
    -Dorg.slf4j.simpleLogger.showDateTime=true
    -Djava.awt.headless=true

  MAVEN_CLI_OPTS: >-
    -s .gitlab-ci_settings.xml
    --batch-mode
    --errors
    --fail-at-end
    --show-version
    -DinstallAtEnd=true
    -DdeployAtEnd=true
    -Dstyle.color=always

cache:
  paths:
    - .m2/repository/
    - target/

mvn-compile-job:
  stage: mvn-compile
  script:
    - mvn $MAVEN_CLI_OPTS compile

mvn-test-job:
  stage: mvn-test
  script:
    - mvn $MAVEN_CLI_OPTS test

mvn-deploy-job:
  stage: mvn-deploy
  script:
    - mvn $MAVEN_CLI_OPTS deploy
  only:
    - master

Question: am I over complicating things here?

This really depends on what has to be done and what would you like to achieve. For example, this is great solution if you would like to run different phases on different branches (in the example above we run mvn deploy only on master branch).

However, on a single branch running separate stages comparing to just mvn deploy from the beginning takes more time on docker runners, since every GitLab job run requires image pulling. This is just something to consider when configuring CI/CD for maven repositories.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM