Cloudbees Jenkins is the most popular open source software orchestration tool on the market due to its wealth of plugins and easy set-up of infrastructure as code. Yet where does one begin using the Jenkinsfile for setting up new project and DevOps pipeline?
Let Blue Ocean take the hassle of setting up a Jenkinsfile from scratch by providing an intuitive, modern coat of paint on Jenkins user interface. With its modern design and intuitive features, Blue Ocean is here to facilitate a quick and easy setup of new Jenkins pipeline with minimal hassle.
In this playground, we will see how easy it is to set up a new Jenkins maven Job using the Blue Ocean plugin and the intuitive feedback it provides through its modern design.
In a separate tab, navigate to http://<IP>:8080 and confirm Jenkins is up.
In a separate tab, navigate to http://<IP>:8081 and confirm Artifactory is up.
(If you wish to login, default username and password is admin/password)
In a separate tab, navigate to http://<IP>:8081 and confirm SonarQube is up.
(If you wish to login, default username and password is admin/password)
-
Log into GitHub
-
Fork the JPetstore repository
Updates to the JPetstore pom file have been known to build errors. To pre-empt one such error, please remove -SNAPSHOT from the version field of the parent.
-
Go to Artifactory and login as admin; admin/password
-
Go to the admin section on the left of the interface and click local
-
Create 2 new maven repositories with the repository Key field as libs-snapshot-local and libs-release-local respectively. You may leave the rest of the fields as is.
-
In the Jenkins tab, click Manage Jenkins
-
Click Configure Systems and scroll to the Artifactory subsection
-
Fill in the fields as follow:
-
Server ID: artifactory (all lowercase)
-
URL: http://:8081/artifactory
-
Username: admin
-
Password: password
-
Return back to Manage Jenkins page, click the Global Tool Configuration and scroll to the Maven subsection.
-
Fill in the fields as follow:
- Name: maven (all lowercase)
- MAVEN_HOME: /var/lib/apache-maven-3.6.0
-
Click Open Blue ocean
-
Click login on the top right with the credentials admin/password
-
Click Create a new pipeline
-
Select GitHub and click 'create an access token here'
- You may need to log in to your GitHub account again
-
Fill in an appropriate description such as 'Playground_token'. You may review the permissions this token permits. Click Generate token when done
-
Copy the newly created token to the clipboard and paste it into Jenkins
-
After clicking connect, select the organisation you forked the JPetstore repository into.
-
Select the JPetstore repository
-
Click Create Pipeline and wait for Jenkins to create the Jenkinsfile
-
Click the plus sign and name the stage 'Build'
-
Click Add steps to create the steps for the stages
-
Select 'shellscript' and copy the following command:
mvn clean install -Dlicense.skip=true
-
Create a new stage called 'Testing' with the following 'shellscript' step command:
mvn sonar:sonar -Dsonar.host.url=http://<IP address>:8081 -Dlicense.skip=true
-
To introduce parallel stages, click the plus sign beneath the testing stage
-
Before continuing, click the dot representing testing and rename it to 'SonarQube Test'
-
Select the newly created parallel stage and name it 'Print Tester Credentials' with steps 'Print message' with
The tester is ${TESTER}
and 10s sleep. -
Create another parallel stage called 'Print build number', with steps 'Print message' with
This is build number ${BUILD_ID}
and 20s sleep. -
Bring up the pipeline editor by pressing ctrl+s for window users and command+s for MacOS users.
-
Replace the single quotes of the previous 2 print steps, seen as echo in the Jenkinsfile, with double quotes
- Without this step, Jenkins will be unable to inject Environmental values into the Build.
-
Add the maven tool definition before the final curly brace in the Jenkinsfile;
tools {maven 'maven'}
-
Click Update to close the editor
-
Create an environmental value using the interface on the right with the fields for 'Name' and 'Value' as 'TESTER' and 'your name' respectively
-
Create a new stage called JFrog push and search for the 'Run arbitrary Pipeline script’ step and insert the following code
def server = Artifactory.server "artifactory" def buildInfo = Artifactory.newBuildInfo() def rtMaven = Artifactory.newMavenBuild() rtMaven.tool = 'maven' rtMaven.deployer server: server, releaseRepo: 'libs-release-local', snapshotRepo: 'libs-snapshot-local' buildInfo = rtMaven.run pom: 'pom.xml', goals: "clean install -Dlicense.skip=true" buildInfo.env.capture = true buildInfo.name = 'jpetstore-6' server.publishBuildInfo buildInfo
-
Add a new stage called 'Deploy prompt', selecting the 'Wait for interactive input' as the step, with the message field filled as 'Deploy to Production?'
-
For the final stage, name it Deployment and add placeholder 'Print message' steps.
-
To save and upload the Jenkinsfile to the GitHub repository, click Save on the top right, with an appropriate description for the change like 'initial Jenkinsfile creation'.
-
Click save and run to upload the file and trigger the Jenkins Build.
-
When completed, you may go to the respective URLs for Artifactory and SonarQube to see the Artefacts and test results respectively.
In this playground we had an environment containing a build server (Jenkins) and a code review platform (SonarQube), along with another server containing a package repository (Artifactory). We built a CI pipeline using the Blue Ocean plugin and saved it as code; pipeline as code. We also saw how to inject custom and pre-determined Jenkins Environmental values into the script.
There are plenty of options available for building on top of the playground such as:
- Preparing a proper Deployment stage to automate the deployment process of the Jenkins Project.
- Setting up email notifications that automatically gets triggered and sent when a job succeeds or fails.
- Implement other automated testing stages to run in parallel to the SonarQube test.
- Run the build processes in other Operating systems and using other Java versions in parallel.
If you would like to further your knowledge with the Jenkins product suite. Consider booking a training course with one of ECS Digital certified Jenkins trainers.
For more information check out the link below:
https://ecs-digital.co.uk/what-we-do/training
Product Details https://www.terraform.io/
Documentation https://www.terraform.io/docs/index.html
Product Details https://jenkins.io/
Documentation https://jenkins.io/doc/
Product Details https://jenkins.io/projects/blueocean/
Documentaion https://jenkins.io/doc/book/blueocean/
Product Details https://jfrog.com/artifactory/
Documentation https://www.jfrog.com/confluence/pages/viewpage.action?pageId=46107472
Product Details https://www.sonarqube.org/
Documentation https://docs.sonarqube.org/latest/
Product Details https://maven.apache.org/
Documentation https://maven.apache.org/guides/index.html
Ensure you have the latest version of terraform installed.
Set the following environment variables:
$export AWS_ACCESS_KEY_ID=<YOUR KEY ID>
$export AWS_SECRET_ACCESS_KEY=<YOUR ACCESS KEY>
Navigate to the _setup directory and execute:
$terraform init
Then execute:
$terraform plan
Finally, apply the plan:
$terraform apply
Navigate to the _setup directory and execute:
$terraform destroy
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Initiating maven build'
sh 'mvn clean install -Dlicense.skip=true'
echo 'Maven build complete'
}
}
stage('Testing') {
parallel {
stage('SonarQube Test') {
steps {
echo 'Initiating SonarQube test'
sh 'mvn sonar:sonar -Dsonar.host.url=http://<IP address>:8081 -Dlicense.skip=true'
echo 'SonarQube test Complete'
}
}
stage('Print Tester credentials') {
steps {
sleep 10
echo "The tester is ${TESTER}"
}
}
stage('Print Build Number') {
steps {
sleep 20
echo "This is build number ${BUILD_ID}"
}
}
}
}
stage('JFrog Push') {
steps {
echo 'Starting JFrog push'
script {
def server = Artifactory.server "artifactory"
def buildInfo = Artifactory.newBuildInfo()
def rtMaven = Artifactory.newMavenBuild()
rtMaven.tool = 'maven'
rtMaven.deployer server: server, releaseRepo: 'libs-release-local', snapshotRepo: 'libs-snapshot-local'
buildInfo = rtMaven.run pom: 'pom.xml', goals: "clean install -Dlicense.skip=true"
buildInfo.env.capture = true
buildInfo.name = 'jpetstore-6'
server.publishBuildInfo buildInfo
}
echo 'JFrog push complete'
}
}
stage('Deploy prompt') {
steps {
input 'Deploy to Production?'
}
}
stage('Deploy') {
steps {
echo 'Initiating Deployment'
echo 'Deployment Complete'
}
}
}
tools {
maven 'maven'
}
environment {
TESTER = 'placeholder'
}
}