Gitlab-CI with Nexus

Sudipt Sharma
Sep 10 · 6 min read

Strategy to Setup Pipeline

Step-1: Setting up a GitLab Repository.

$    cd spring3hibernateapp
$ ls
pom.xml pom.xml~ src
$    git remote -v
origin git@gitlab.com:<your_gitlab_id>/spring3hibernateapp.git (fetch)
origin git@gitlab.com:<your_gitlab_id>/spring3hibernateapp.git (push)
$    git add -A
$    git commit -m "[Master][Add] Adding the code "
$    git push origin master

Step-2: Install GitLab Runner manually on GNU/Linux

$    sudo wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-386
$    sudo chmod +x /usr/local/bin/gitlab-runner
$    curl -sSL https://get.docker.com/ | sh
$    sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
$    sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
$ sudo gitlab-runner start
$     sudo gitlab-runner register
Runtime platform arch=amd64 os=linux pid=1742 revision=3afdaba6 version=11.5.0
Running in system-mode.
https://gitlab.com/
****8kmMfx_RMr****
[gitlab-runner]: spring3hibernate
build
Registering runner... succeeded runner=ZP3TrPCd
docker
maven       
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
[Unit]
Description=GitLab Runner
After=syslog.target network.target
ConditionFileIsExecutable=/usr/local/bin/gitlab-runner
[Service]
StartLimitInterval=5
StartLimitBurst=10
ExecStart=/usr/local/bin/gitlab-runner "run" "--working-directory" "/home/gitlab-runner" "--config" "/etc/gitlab-runner/config.toml" "--service" "gitlab-runner" "--syslog" "--user" "gitlab-runner"
Restart=always
RestartSec=120
[Install]
WantedBy=multi-user.target
$    curl -sSL https://get.docker.com/ | sh
$    docker run -d -p 8081:8081 --name nexus sonatype/nexus:oss

Step-5: Configure the NEXUS deployment

$    cd spring3hibernateapp/
$    mkdir .m2
$    touch .m2/settings.xml
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd"
xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<servers>
<server>
<id>central</id>
<username>${env.NEXUS_REPO_USER}</username>
<password>${env.NEXUS_REPO_PASS}</password>
</server>
<server>
<id>snapshots</id>
<username>${env.NEXUS_REPO_USER}</username>
<password>${env.NEXUS_REPO_PASS}</password>
</server>
</servers>
</settings>
<repository>
<id>central</id>
<name>Central</name>
<url>${env.NEXUS_REPO_URL}central/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Snapshots</name>
<url>${env.NEXUS_REPO_URL}snapshots/</url>
</snapshotRepository>

Step-6: Configure GitLab CI/CD for simple maven deployment.

image: mavenvariables:
MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
cache:
paths:
- .m2/repository/
- target/
stages:
- build
- test
- package
- deploy
codebuild:
tags:
- build
stage: build
script:
- mvn compile
codetest:
tags:
- build
stage: test
script:
- mvn $MAVEN_CLI_OPTS test
- echo "The code has been tested"
Codepackage:
tags:
- build
stage: package
script:
- mvn $MAVEN_CLI_OPTS package -Dmaven.test.skip=true
- echo "Packaging the code"
artifacts:
paths:
- target/*.war
only:
- master
Codedeploy:
tags:
- build
stage: deploy
script:
- mvn $MAVEN_CLI_OPTS deploy -Dmaven.test.skip=true
- echo "installing the package in local repository"
only:
- master

Sudipt Sharma

Written by

DevOps | Seeker | Learner | Thinker | Observer | https://www.linkedin.com/in/sudipt-sharma-aab46661/

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade