Deploy jar to Tomcat using GitLab CI Pipeline

In this article we are going to cover Deploy jar to Tomcat using GitLab CI Pipeline | GitLab CI CD Deploy to Tomcat Server.

#1:Install Gitlab-Runner on Ubuntu EC2 Instance

Install GitLab Runner using below command to install latest GitLab Runner visit official GitLab Runner page.

Add the official GitLab repository using below command

curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash

To Install the latest version of GitLab Runner on Ubuntu EC2 Instance

sudo apt-get install gitlab-runner

#2:Grant sudo Permission to GitLab Runner User

After install GitLab Runner you will see gitlab-runner user in /home directory

cd /home
ls

Output:

gitlab-runner ubuntu

Add the gitlab-runner user in sudoers group and set NOPASSWD as shown below

sudo visudo
gitlab-runner ALL=(ALL:ALL) ALL
ubuntu ALL=(ALL:ALL) ALL
GitLab CI Deploy to EC2 using SSH 1
root

#3:Register GitLab Runner to GitLab on Ubuntu

1. First login to GitLab Server with Username and Password.

login to gitlab server with username and password

2. Click on your project and select Settings

goto gitlab project settings

3. Navigate to Settings and click on CI/CD inside this click on Expand of Runners section

goto gitlab CICD and expand

4. Copy GitLab server URL and Registration Token as shown below.

copy the gitlab server url and token

5. Paste GitLab Server URL and Token in registration command as below

$ gitlab-runner register --name project-name-runner --url https://gitlab.fosstechnix.com --registration-token  Uc7yzxTMzsXhXJx3zgM

OR

sudo gitlab-runner register

OR

if you want to register gitlab-runner with docker executor then use below command

sudo gitlab-runner register \
  --non-interactive \
  --url "GITLAB_SERVER_URL" \
  --registration-token "REGISTRATION_TOKEN" \
  --description "docker-runner" \
  --executor "docker" \
  --docker-image ubuntu:latest

6. Copy above command. Login to GitLab runner server through SSH and paste & run copied command on command line.

Follow the default options until which agent you need to register and then select agent as shown below

register gitlab runner using ssh

7. Once you registered Runner for project then you will get runner as below,

gitlab runner activated

Successfully, We have performed GitLab Runner Registration.

Error: This job is stuck because the project doesn’t have any runners online assigned to it. Go to Runners page.

Solution:

You have added tags while registering GitLab Runner however you have not added tags for your Job.

Follow below steps to add tags.

1. Navigate to Settings and click on CI/CD inside this click on Expand of Runners section

2. Go to Runners and check on Indicates whether this runner can pick jobs without tags Box

How to Install GitLab Runner on Ubuntu 20.04 LTS 1

#4:Add SSH server details as GitLab Variable

Add EC2 instance or SSH server details like SSH Private key, EC2 IP Address and SSH in GitLab CI CD Variables. To add variables in GitLab follow below steps.

Go to settings<<CI/CD<<variables<<Expand it

#Add private key in $SSH_PRIVATE_KEY variable

#Add Server Ip in $EC2_IPADDRESS variable

#Add server user in $SSH_USER variable

GitLab CI Deploy to EC2 using SSH 2
add variables

When creating variables you have to remove protect variable flag

Deploy war to Tomcat using GitLab CI Pipeline 1

#5:Deploy jar to Tomcat using GitLab CI Pipeline

Below is gitlab-ci.yml to Deploy jar to Tomcat using GitLab CI Pipeline.

stages:
  - build
  - deploy


maven-build:
  stage: build
  script: "mvn package -B"
  artifacts:
    paths:
      - target/*.jar

Deploy: 
  stage: deploy
  before_script:
  - 'command -v ssh-agent >/dev/null || ( apk add --update openssh )' 
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - ssh-keyscan $EC2_IPADDRESS >> ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts
  script:
    - scp -o StrictHostKeyChecking=no /home/gitlab-runner/builds/d1Qcnb9E/0/devopshint/tomcat-maven/target/tomcat-maven-0.0.1-SNAPSHOT.jar [email protected]:/opt/tomcat/webapps

If you get error permission denied /opt/tomcat/webapps so run the below command for permission to html

sudo chmod -R 777  /opt/tomcat/webapps

Conclusion:

In this article we have covered Deploy jar to Tomcat using GitLab CI Pipeline | GitLab CI CD Deploy to Tomcat Server.

Related Articles:

Reference:

Using SSH keys with GitLab CI/CD

FOSS TechNix

FOSS TechNix (Free,Open Source Software's and Technology Nix*) founded in 2019 is a community platform where you can find How-to Guides, articles for DevOps Tools,Linux and Databases.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap