Skip to content

Instantly share code, notes, and snippets.

@helenhash
Last active November 6, 2024 10:19
Show Gist options
  • Save helenhash/b08d663cfd18b41f31cf7575f9454470 to your computer and use it in GitHub Desktop.
Save helenhash/b08d663cfd18b41f31cf7575f9454470 to your computer and use it in GitHub Desktop.

Jenkins Setup (Jenkins with GitLab, Java Maven Project)

Prerequisite: For testing purpose, we can setup Jenkins, GitLab server with Docker. Ref: Jenkin Install With Gitlab using Docker

Install Plugins/ Tools

Plugin

Go to Manage Jenkins > Manage Plugins.

  • GitLab Plugin
  • Git plugin (if not already installed)

Install tools (JDK, Maven):

Manage Jenkins -> Tools : Select Install automatically - We can install one or more JDK version

JDK(s) https://jdk.java.net/archive/

Ex: JDK17 https://download.java.net/java/GA/jdk17.0.2/dfd4a8d0985749f896bed50d7138ee7f/8/GPL/openjdk-17.0.2_linux-x64_bin.tar.gz

image

Other option: We can install JDK directly on Jenkins machine (but this way is not convenient because it will apply for all jenkins jobs). Ex: sudo apt install openjdk-8-jdk

image

Maven setup: Manage Jenkins -> Tools -> Go to Maven section

image

Connect Jenkins and Gitlab

  • Create a new Access Token on Gitlab -> Copy it
  • On Jenkins
    • Manage Jenkins -> Credentials -> Create a new credentials (type: Secret text)
    • Create Gitlab connection on Jenkins (using above credential): Manage Jenkins -> System -> Find GitLab section

Demo with Jenkins Freestyle Job

Setup Credential to connect to Git to fetch source code

Manage Jenkins -> Credentials -> Select Kind as Username and Password -> Input username and password from Gitlab

(Note: We can setup ssh in jenkins machine and put it to Gitlab, then config ssh credential instead)

Create a new Job

image

Note: Url Repository (like container.network_name), not using expose port because we connect from jenkin machine to gitlab server with same network (inside docker)

For Build Step

image

Select Maven version you already setup and command to build your project (depend on your project)

image

Finish for freestyle project

Pipeline Job

https://www.jenkins.io/doc/book/pipeline/

Jenkins Pipeline (or simply "Pipeline" with a capital "P") is a suite of plugins which supports implementing and integrating continuous delivery pipelines into Jenkins.

Pipeline domain-specific language (DSL) syntax.

"While the syntax for defining a Pipeline, either in the web UI or with a Jenkinsfile is the same, it is generally considered best practice to define the Pipeline in a Jenkinsfile and check that in to source control."

Config

Creating a Jenkinsfile and committing it to source control Create and configure a Pipeline project through a Jenkinsfile or through Blue Ocean

  • Add cred

https://www.jenkins.io/doc/book/using/using-credentials/

  • Pipeline

Jenkins is, fundamentally, an automation engine which supports a number of automation patterns.

  • Create Jenskinfile

text file that contains the definition of a Jenkins Pipeline and is checked into source control

https://www.jenkins.io/doc/book/pipeline/jenkinsfile/

https://github.com/giaule91/spring-boot-demo/blob/main/simple-demo/Jenkinsfile

https://devopspilot.com/content/jenkins/tutorials/pipeline/02-how-to-write-jenkinsfile.html

    agent any

    stages {
        stage('Build') {
            steps {
                mvn compile jib:build
            }
        }
        stage('Test') {
            steps {
                echo 'Testing..'
            }
        }
        stage('Deploy') {
            steps {
                ssh r
                remote 126.55.255.33
                docker run <dsdds>
            }
        }
    }
}

Config in Jenskin

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment