Skip to main content

Jenkins CI Setup: Connect Jenkins to Bitbucket, Create and Run a Test-Job; Connect Jenkins to Artifactory, Build Example Project from GitHub and Push Artifacts to Artifactory Maven Repository

1171 words·
Jenkins CI Pipeline Bitbucket Artifactory Maven CoreDNS Kubernetes
Table of Contents

Prerequisites
#

Kubernetes: CoreDNS
#

Add Hosts Entry for BitBucket
#

Crate CoreDNS host entries for the Bitbucket and Artifactory servers:

  • 192.168.30.60 bitbucket.jklug.work

  • 192.168.30.23 artifactory.jklug.work

# Create a backup of the CoreDNS configmap
kubectl get cm coredns -n kube-system -o yaml > coredns-configmap-backup.yaml
# Adopt the CoreDNS ConfigMap
kubectl edit cm coredns -n kube-system
apiVersion: v1
data:
  Corefile: |
    .:53 {
        errors {
        }
        health {
            lameduck 5s
        }
        ready
        kubernetes jkw-k8s.local in-addr.arpa ip6.arpa {
          pods insecure
          fallthrough in-addr.arpa ip6.arpa
        }
        hosts {
            192.168.30.60 bitbucket.jklug.work
            192.168.30.23 artifactory.jklug.work
            fallthrough
        }
        prometheus :9153
        forward . /etc/resolv.conf {
          prefer_udp
          max_concurrent 1000
        }
        cache 30

        loop
        reload
        loadbalance
    }    
# Restart CoreDNS
kubectl rollout restart deployment coredns -n kube-system

Verify DNS Resolution
#

# Exec container terminal
kubectl exec -it jenkins-0 -n jenkins -- /bin/bash
# Curl Bitbucket
curl -I https://bitbucket.jklug.work

# Shell output:
HTTP/2 302
cache-control: no-cache
cache-control: no-store
content-language: en-US
date: Fri, 14 Jun 2024 14:22:12 GMT
expires: Thu, 01 Jan 1970 00:00:00 GMT
location: https://bitbucket.jklug.work/dashboard
pragma: no-cache
x-arequestid: @IMBKUCx862x249x0
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block

Jenkins Settings
#

Set Number of Executors
#

The default number of executors is “0” and needs to be increased, otherwise jobs will fail:

  • Go to: “Dashboard” > “Manage Jenkins” > “Nodes”

  • Select the node “Build-In Node”

  • Select the tab “Configure”

  • Set “Number of executors” to “10”


Artifactory
#

Create the following Maven repositories for the artifacts:
maven-local, maven-snapshot

Set Up Artifactory Repository
#

  • Go to: “Administration” > Repositories > Local

  • Click “Add Repositories” > “Local Repository”

  • Select Package Type: “Maven”

  • Repository Key: maven-local (Define a name for the repo)

  • Repository Layout: “maven-2-default”

  • Click “Create Local Repository”



Setup: Connect Jenkins to Bitbucket
#

In this section I’m connection my Jenkins instance to a local Bitbucket Data Center instance. To test the connection I’m using an example jobs that checks out files from the following Bitbucket repository https://bitbucket.jklug.work/scm/ex/example-repo.git and copy it’s files to a deployment host via SSH.

Jenkins Settings
#

Install Required Plugins
#

  • Go to: “Dashboard” > “Manage Jenkins” > “System Configuration”: “Plugins”

  • Select the tab “Available plugins”

  • Install the following plugins:

  1. Bitbucket Branch Source Plugin

  2. Publish Over SSH


Bitbucket Credentials
#

Go to: “Dashboard” > “Manage Jenkins” > “Security”: “Credentials”

  • Select “Stores scoped to Jenkins”: “System”

  • Select “Global credentials (unrestricted)”

  • Click “Add Credentials”

  • Select “Kind”: “Username with password”

  • Define “Username” and “Password” for the Bitbucket instance

Note: This setup is just for homelab testing purposes only and to verify if the setup actually works. For production purposes us a SSH key.


Deployment Host Settings
#

In this step I’m setting up my Debian 12 server with the IP 192.168.30.60 as host for deploymentens, so that I can test if Jenkins is able to deploy some resources.

SSH Key on Deployment Host
#

Create a SSH key on the deployment host, that is used by jenkins to connect to the host:

# Create a SSH key
ssh-keygen -t rsa -b 2048

# Add the public key to known_hosts
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

Copy the private SSH key ~/.ssh/id_rsa and add it to Jenkins.

Add Host & SSH Key to Jenkins
#

  • Go to: “Manage Jenkins” > “System”

  • Scroll down to the “Publish over SSH” section

  • Click SSH Servers: “Add”

Key: Paste the private SSH key

SSH Server

  • Name: “192.168.30.60”

  • Hostname: “192.168.30.60”

  • Username: “debian”

  • Remote Directory: “/home/debian”

  • Click “Test Configuration”


Example Job for Testing
#

Create a New Job
#

  • Go to: “Dashboard”

  • Click “New Item”

  • Select “Freestyle project”

  • “Enter an item name”: “Bitbucket-Test”

  • Click “OK”

Configure the Job
#

Post-build Actions
#

  • Select “Send build artifacts over SSH”

  • Select the SSH Server “192.168.30.60”

  • Transfer Set / Source files: Select files from the repository to should be copied to the deployment host

  • Save the job

Manually Trigger the Job
#

  • Go to: “Dashboard”

  • Select the job “Bitbucket-Test”

  • Click “Build Now”

Check Build History
#

In the “Build History” section of the “Bitbucket-Test” job should now appear a green check mark.



Setup: Connect Jenkins to Artifactory
#

Add Jenkins Plugins:
#

  • Go to: “Dashboard” > “Manage Jenkins” > “Plugins”

  • Click “Available plugins”

  • Install the Plugins:

  1. Artifactory

  2. Maven Integration plugin

Configure Maven Plugin
#

  • Go to: “Dashboard” > “Manage Jenkins” > “Tools”

  • Scroll down to the “Maven installations” section

  • Click “Add Maven”

  • Install from Apache, Version: “3.9.7” (Select the latest version)

  • Name: “MAVEN_3.9.7” (This name is used when you define Jenkins pipeline scripts or job configurations that specify which Maven installation to use)

  • Click “Save”

Configure Artifactory Plugin
#

  • Go to: “Dashboard” > “Manage Jenkins” > “System”

  • Scroll down to “JFrog” section

  • Click “Add JFrog Platform Instance”

JFrog instance details:

  • Instance ID: artifactory-jklug-work

  • Server ID: https://artifactory.jklug.work

Credentials:

  • Username: admin

  • Password: password

  • Click “Test connection”

# Shell output:
Found JFrog Artifactory 7.84.14 at https://artifactory.jklug.work/artifactory
JFrog Distribution not found at https://artifactory.jklug.work/distribution
  • Click “Apply” & “Save”

Jenkins Test-CI-Job
#

Overview
#

I’m using the following GitHub project with an example java maven application for this pipeline:
https://github.com/jueklu/simple-java-maven-app.git

Create Job
#

  • Go to: “Dashboard” > “New Item”

  • Enter an item name: artifactory-ci-java-maven-app

  • Select “Pipeline”

  • Click “OK”

Pipeline
#

  • Select Definition: “Pipeline script”

  • Select “Use Groovy Sandbox”

node {
    // Define variables at the top
    def artifactoryServerId = 'artifactory-jklug-work'
    def mavenToolVersion = 'MAVEN_3.9.7'
    def releaseRepository = 'maven-local'
    def snapshotRepository = 'maven-snapshot'
    def virReleaseRepository = 'vir-maven-1'
    def virSnapshotRepository = 'vir-maven-2'

    def server
    def rtMaven = Artifactory.newMavenBuild()
    def buildInfo

    stage ('Clone') {
        git url: 'https://github.com/jueklu/simple-java-maven-app.git'
    }

    stage ('Artifactory configuration') {
        // Obtain an Artifactory server instance, defined in Jenkins --> Manage Jenkins --> Configure System:
        server = Artifactory.server(artifactoryServerId)

        // Set the Maven tool and deployer/resolver configuration
        rtMaven.tool = mavenToolVersion
        rtMaven.deployer releaseRepo: releaseRepository, snapshotRepo: snapshotRepository, server: server
        rtMaven.resolver releaseRepo: virReleaseRepository, snapshotRepo: virSnapshotRepository, server: server
        buildInfo = Artifactory.newBuildInfo()
    }

    stage ('Exec Maven') {
        rtMaven.run pom: 'pom.xml', goals: 'clean install', buildInfo: buildInfo
    }

    stage ('Publish build info') {
        server.publishBuildInfo(buildInfo)
    }
}
  • Click “Apply” & “Save”

Run the Job
#

  • Click “Build Now”

Check the Job Logs
#

Go to: “Dashboard” > “artifactory-ci-java-maven-app” > “#1” > “Console Output”

Or open the following link:
https://jenkins.jklug.work/job/artifactory-ci-java-maven-app/1/console

The logs should look like this:

[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - ------------------------------------------------------------------------
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - BUILD SUCCESS
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - ------------------------------------------------------------------------
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - Total time:  5.967 s
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - Finished at: 2024-06-16T12:43:50Z
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - ------------------------------------------------------------------------
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Publish build info)
[Pipeline] publishBuildInfo
Executing command: /bin/sh -c git log --pretty=format:%s -1
Deploying build info...
Build-info successfully deployed. Browse it in Artifactory under https://artifactory.jklug.work/ui/builds/artifactory-ci-java-maven-app/1/1718541820733/published
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Verify the Artefacts at Artifactory
#

Verify that the artifacts from the Jenkins job have been successfully deployed to Artifactory:

  • Open Artefactory: https://artifactory.jklug.work

  • Go to: “Application” > “Artifactory” > “Artifacts”

  • Select “maven-local”

  • Eventually it’s necessary to refresh the browser

Or just open the following link:

# Verify the artifacts in artifactory
https://artifactory.jklug.work/ui/repos/tree/General/maven-local

You should be able the expand the folder tree in find the artifacts.