How to release maven project from Gitlab without Snapshot? - maven

.gitlab.yml has following
stages:
- test
- publish
- deploy
- manual
# Test all commits regardless of branch in a WildFly 10 instance
test:wf10:
stage: test
script:
- mvn clean test -U -Dprofile.wildfly10 -Dwildfly10.servergroup=masterdata-web
tags:
- test
# Publish tagged release to Artifactory (WildFly 10)
publish-release:wf10:
stage: publish
only:
- /^v-.*$/
script:
- mvn clean deploy -B -U -P wildfly10 -Dmaven.test.skip=true
tags:
- test
# Manually deploy any build to WildFly 10 servergroup Masterdata-web
deploy-test:wf10:
stage: manual
environment:
name: test
script:
- mvn clean package wildfly:undeploy wildfly:deploy -B -U -P wildfly10 -Dmaven.test.skip=true -Dwildfly10.servergroup=masterdata-web
tags:
- test
when: manual
# Manually deploy tagged release to WildFly 10 servergroup Masterdata-web
deploy-qa:wf10:
stage: manual
environment:
name: qa
script:
- mvn clean package wildfly:undeploy wildfly:deploy -B -U -P wildfly10 -Dmaven.test.skip=true -Dwildfly10.servergroup=masterdata-web
tags:
- qa
when: manual
# Automatically deploy build staged for release to WildFly 10 servergroup Masterdata-web
deploy-release-qa:wf10:
stage: deploy
environment:
name: qa
only:
- master
except:
- /^v-.*$/
script:
- mvn clean package wildfly:undeploy wildfly:deploy -B -U -P wildfly10 -Dmaven.test.skip=true -Dwildfly10.servergroup=masterdata-web
tags:
- qa
when: manual
# Automaticaly deploy tagged release to WildFly 10 servergroup Masterdata-web
deploy-release-prod:wf10:
stage: deploy
environment:
name: prod
only:
- /^v-.*$/
script:
- mvn clean package wildfly:undeploy wildfly:deploy -B -U -P wildfly10 -Dmaven.test.skip=true -Dwildfly10.servergroup=terp
tags:
- prod
# Manually generate project report for WildFly 10, report is stored for 2 weeks
generate-report:wf10:
stage: manual
script:
- mvn clean test site:site -B -U -P wildfly10
- mv target/site ./
artifacts:
name: report-wf10
expire_in: 2 weeks
paths:
- site
tags:
- test
when: manual
# Manual deploy tagged release to WildFly 10 servergroup servicedesk in PROD
deploy-prod:wf10:
stage: deploy
environment:
name: prod
#only:
# - /^v-.*$/
script:
- mvn clean package -B -U wildfly:undeploy wildfly:deploy -Dprofile.wildfly10 -Dmaven.test.skip=true -Dwildfly10.servergroup=terp
tags:
- prod
when: manual
For release every time, I need to run following commands by check out branch:
mvn release:prepare -Darguments=-Dprofile.wildfly10 -DignoreSnapshots=true
mvn release:clean
git push --follow-tags
Then it relaeases without SNAPSHOT
If i create manual branch it always append SNAPSHOT.
Can you suggest how to create manual pipeline which deploys without SNapshot and from Gitlab only?

Maven is doing exactly what you have ordered it to do. The command like:
mvn clean deploy -B -U -P wildfly10 -Dmaven.test.skip=true
orders maven to compile, package and deploy artifacts to remote repository, and when doing that maven picks up target version from pom.xml, thus to build and deploy artifacts with specific version you need to tell maven what version to use explicitly or implicitly, for example, when you are doing:
mvn release:prepare -Darguments=-Dprofile.wildfly10 -DignoreSnapshots=true
git push --follow-tags
maven-release-plugin creates two commits in SCM: the first one with "release" version in pom.xml (accompanied by tag/branch), the second one with "snapshot" version, and the first commit triggers your publish job. That explains why creating branch manually does not makes sense.
There are a couple of options to implement release management in maven project using GitLab:
create pipeline forms in GitLab to request information about releaseVersion and developmentVersion, in that case maven release command would be something like mvn --batch-mode -Prelease release:clean release:prepare -DreleaseVersion=${releaseVersion} DdevelopmentVersion=${developmentVersion} (most likely extra setup will required to allow maven to push changes)
let maven to choose versions automatically (or implement version policies), alternatively you make take advantage of using my extension: version-policy-extension
use Maven CI Friendly Versions - in that case you will need to pass ${revision} from GitLab
implement external tools (scripts, docker images) to automate that

Related

Gitlab Runner download the maven dependency For each build , Don't get from cache!=

I configurd the .gitlab-ci.yml, it is a java project and needs to maven repository, my local repository is nexus, I downloading the dependency from nexux successfully , but For each build it still downloads all the dependencies every time from nexus and it don't caching.
.gitlab-ci.yml configuration is :
image: nexus.local.ir:8095/maven:3.6.3-jdk-8
######## run image #########
stages:
- build
cache:
key: maven-cache
paths:
- .m2/repository
######### cache ############
build:
stage: build
script:
- mvn package -s config/settings.xml -Dmaven.test.skip=true

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.

Maven build in github continuous integration action

I am using Github actions to set up a continuous integration workflow for a Maven app. The Maven app depends on some external dependencies as well as a couple other repositories that we built. In my Github action's .yaml file, I've included the line "mvn install". When I make a commit, the action begins to run the .yaml file as expected. The build mostly works, and can find external maven dependencies from maven central. However, the build cannot find the two other private repositories. These repositories are saved on the same github account as the repo. for this app.
Downloading from central: https://repo.maven.apache.org/maven2/com/<repo>/1.3/<repoName>-1.3.pom
[WARNING] The POM for com.<repo>/<reponame>.core:jar:1.3 is missing, no dependency information available
Here is my github/workflow action's yaml file...
name: Build1
on:
push:
branches:
- '*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Run a one-line script
run: echo Hello, world!
- name: Run a multi-line script
run: |
echo Add other actions to build,
echo test, and deploy your project.
- name: Build
run: mvn install

How to setup a java 11 based spring boot pipeline on bitbucket?

Build fails with "invalid target release: 11" message.
After a commit to bitbucket, a pipeline job starts automaticly but fails with
Full Error Message:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project sms-commons: Fatal error compiling: invalid target release: 11 ->
How to enable java 11 support?
image: maven:3.3.9
pipelines:
default:
- step:
caches:
- maven
script: # Modify the commands below to build your repository.
- mvn -B verify # -B batch mode makes Maven less verbose
- mvn clean install
I expect a successfull build, but as I wrote the build job fails.
You need to use a newer version of the maven Compiler Plugin
org.apache.maven.plugins
maven-compiler-plugin
3.8.0
And you need a maven Docker image that is providing java 11 for example maven:3-jdk-11
I use: ehsaniara/bitbucket-util as Docker Image.
example:
options:
docker: true
pipelines:
branches:
master:
- step:
name: Production Build on Master
image: ehsaniara/bitbucket-util:latest
trigger: automatic
script:
- mvn clean install
You need maven image which supports jdk 11, so update your "image" value like below:
image: maven:3.8.3-jdk-11
pipelines:
default:
- step:
caches:
- maven
script: # Modify the commands below to build your repository.
- mvn -B verify # -B batch mode makes Maven less verbose
- mvn clean install

Gitlab CICD - Run a mvn clean install before building a docker container

We are running a spring-boot application that requires you to run a mvn clean install -Dmaven.test.skip=true before you can run a docker build. Is there any images with both maven and docker installed?
I have tried running a before script with a maven:3.6.0-jdk-8-slim image then specifying a separate image in the job.
image: maven:3.6.0-jdk-8-slim
before_script:
- mvn clean install -Dmaven.test.skip=true
build_archivist_elasticsearch:
image: docker:latest
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay
script:
- cd archivist
- docker build -t xxxx/xxxxx:development
Need the mvn clean install to build and create the artifacts then run the docker build -t and docker push to our repo.
The solution is to utilize artifacts in the gitlab-ci.yml file to store the dependencies needed for the docker build (ie. *.jar and *.zip files)
image: docker:latest
stages:
- build
- deploy
build_artifacts:
image: maven:3.6.0-jdk-8-slim
stage: build
script:
- mvn clean install -Dmaven.test.skip=true
artifacts:
paths:
- archivist/target/*.jar
- elasticsearch/*.zip
build_archivist_elasticsearch:
stage: deploy
image: docker:latest
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay
script:
- cd archivist
- docker build -t xxxxx/xxxxxx:development .
`
https://docs.gitlab.com/ee/ci/yaml/README.html#artifacts
We had similar problem to build spring boot image using spotify docker-maven-plugin
Here is our final configuration :
stages:
- build
variables:
MVN_OPTS: "-Dmaven.repo.local=/cache/maven.repository"
build:
image: maven:3.6.0-jdk-8-slim
stage: build
script:
- mvn $MVN_OPTS clean test -Duser.timezone=CET
tags:
- docker
With docker tags, we select a kubernetes gitlab-runner configured to trigger containers based on docker:latest

Resources