Skip to main content

Jenkins CI Pipeline: Retrieve Code from Bitbucket Repository, Executes Maven Build, Deployes Artifacts to Artifactory Maven Repository

703 words·
Jenkins CI Pipeline Bitbucket Artifactory Maven

Bitbucket Repository
#

Create Project
#

  • Click “Create Project”

  • Project name: jenkins-ci

  • Project key: JEN

  • Click “Create project”

Create Repository
#

  • Click “Create repository”

  • Name: maven-example

  • Default branch name: main

# Repository URL: HTTPS
https://bitbucket.jklug.work/scm/jen/maven-example.git

# SSH version
ssh://git@bitbucket.jklug.work:7999/jen/maven-example.git

Push to Repository
#

Hosts Entry
#

Make sure the host that pushes into the Bitbucket repository is able to resolve the Bitbucket DNS name:

# Create hosts entry
192.168.30.60 bitbucket.jklug.work

SSH Key
#

Create a SSH Key on the host:

# Create a SSH key on the host that pushes to Bitbucket
ssh-keygen -t rsa -b 4096 -C "juergen@jklug.work" -f ~/.ssh/id_rsa_bitbucket

Adopt SSH configuration:

# Open the SSH configuration
vi ~/.ssh/config

# Add the following lines:
Host bitbucket.jklug.work
  HostName bitbucket.jklug.work
  User git
  IdentityFile ~/.ssh/id_rsa_bitbucket

Start SSH agent (Should not be necessary):

# Start the SSH agent
eval "$(ssh-agent -s)"

# Add SSH key to the SSH agent
ssh-add ~/.ssh/id_rsa_bitbucket

Add the SSH key to Bitbucket:

  • Go to: “Profile” > “Manage Account” > “SSH keys”

  • Click “Add key”

  • “Key”: Paste the public SSH key

  • Click save

Configure Git
#

# Configure Git
git config --global user.email "juergen@jklug.work"
git config --global user.name "juergen"

Clone GitHub Example Repository
#

Clone the example repository from GitHub:

# Clone the java maven example repository
git clone https://github.com/jueklu/simple-java-maven-app.git && cd simple-java-maven-app

Push into the Bitbucket Repository
#

# Change the remote URL from GitHub to the Bitbucket repository URL: SSH version
git remote set-url origin ssh://git@bitbucket.jklug.work:7999/jen/maven-example.git

# Change the remote URL from GitHub to the Bitbucket repository URL: HTTPS version
git remote set-url origin https://bitbucket.jklug.work/scm/jen/maven-example.git
# Verify the current branch
git branch

# Shell output:
* master

# Push the local master branch to the remote main branch
git push origin master:main

Open the Bitbucket webinterface, go to the “maven-example” repository and set the default branch to main.



Jenkins Build & Push
#

Overview
#

Create a Jenkins Pipeline that retrieves the code from the Bitbucket repository, executes a Maven build to compile the Java project, and deployes the artifacts to the Artifactory repository.

Create Job
#

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

  • Enter an item name: bitbucket-ci

  • Select “Pipeline”

  • Click “OK”

Jenkins 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') {
        // Use the HTTPS URL for the Bitbucket repository
        // Ensure you have the correct credentialsId for HTTPS authentication
        git credentialsId: 'bitbucket', url: 'https://bitbucket.jklug.work/scm/jen/maven-example.git', branch: 'main'
    }

    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') {
        // Execute Maven goals
        rtMaven.run pom: 'pom.xml', goals: 'clean install', buildInfo: buildInfo
    }

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

Run the Job
#

  • Click “Build Now”

Check the Job Logs
#

Go to: “Dashboard” > “bitbucket-ci” > “#1” > “Console Output”

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:  2.923 s
[main] INFO org.apache.maven.cli.event.ExecutionEventLogger - Finished at: 2024-06-16T14:23:32Z
[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/bitbucket-ci/3/1718547807547/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.