Jenkins is, fundamentally, an automation engine which supports a number of automation patterns. Pipeline adds a powerful set of automation tools onto Jenkins, supporting use cases that span from simple continuous integration to comprehensive CD pipelines. By modeling a series of related tasks, users can take advantage of the many features of Pipeline:
Code: Pipelines are implemented in code and typically checked into source control, giving teams the ability to edit, review, and iterate upon their delivery pipeline.
Durable: Pipelines can survive both planned and unplanned restarts of the Jenkins master.
Pausable: Pipelines can optionally stop and wait for human input or approval before continuing the Pipeline run.
Versatile: Pipelines support complex real-world CD requirements, including the ability to fork/join, loop, and perform work in parallel.
Extensible: The Pipeline plugin supports custom extensions to its DSL [1] and multiple options for integration with other plugins.
The aim of this lab is to demonstrate how to you provisining environment and application deployment in a Continuous Integration/Continuous Delivery (CI/CD) process using docker containers and Jenkins Pipeline respectively.
The tools used to create the examples for this lab are:
- Docker to create infrastructure for this lab
- Maven to set and deploy the Java project.
- GIT for source code management and control
- JFrog Artifactory is the repository for artifact binaries and docker images.
- Jenkins to orchestrate the CI/CD pipeline flow.
Now let's explore the CI/CD pipeline flow built for this lab.
You can skip this section if you are using your own Jenkins Dashboard.
- Add your Github Credentials
- Credentials > Add 'github' Credentials
- Add your JFrog Artifactory credentials 2. Manage Jenkins > Configure > Artifactory
- Add Docker Server 3. Manage Jenkins > Configure > Docker
- Configure Maven 4. Manage Jenkins > Global Configuration Tools > Maven
Navigate to left panel and click on New Item. Enter a unique job name and select Pipeline to create a pipeline job. Hit OK!
Next it will take to you to job configuration page, you need to configure your job with SCM.
- Copy your GitHub Repository link or if you are using this lab use: https://github.com/abha10/playground-pune-01.git
- Go back to your Job and under Pipeline configuration do the following steps
-
Definition: select Pipeline script from SCM
-
SCM: Select Git
- Repositories URL:
- Credentials: Select your SCM credentials
- Additional Behaviour: Checkout to specific branch
- Type 'master' in it.
- Make sure you have your Script Path as Jenkinsfile
- Save all the changes
-
Build your job
-
This stage consists of following steps
- Build project with Maven Build and create snapshot artifact
- Create a project Snapshot
steps {
sh 'mvn clean package'
script{
def snap_image = docker.build("digitaldemo-docker-snapshot-images.jfrog.io/${JOB_NAME}:SNAPSHOT")
}
}
This stage consists of following steps
- Deloy the snapshot image in Test Envirnoment
steps {
script {
app = docker.image("digitaldemo-docker-snapshot-images.jfrog.io/${JOB_NAME}:SNAPSHOT").withRun('-p 9000:9000 --name snapshot') {
sh 'sleep 10'
sh 'curl https://localhost:9000 || exit 1'
}
}
}
This stage consists of following steps
- Once approved Prepare the snapshot for release
- And push relesease artifact and image into JFrog Artifactory
when {
expression {
params.RELEASE
}
}
steps {
script {
releasedVersion = getReleasedVersion()
withCredentials([usernamePassword(credentialsId: 'github', passwordVariable: 'password', usernameVariable: 'username')]) {
sh "git config user.email [email protected] && git config user.name Jenkins"
sh "mvn release:prepare release:perform -Dusername=${username} -Dpassword=${password}"
}
def release_image = docker.build("digitaldemo-docker-release-images.jfrog.io/${JOB_NAME}:${releasedVersion}")
}
}
when {
expression {
params.RELEASE
}
}
steps {
script {
// Create an Artifactory server instance:
def server = Artifactory.server('abhaya-docker-artifactory')
def uploadSpec = """{ "files": [{
"pattern": "**/*.jar",
"target": "ext-release-local/"
}]
}"""
server.upload(uploadSpec)
// Create an Artifactory Docker instance. The instance stores the Artifactory credentials and the Docker daemon host address:
def rtDocker = Artifactory.docker server: server, host: "tcp:https://localhost:2375"
// Push a docker image to Artifactory (here we're pushing hello-world:latest). The push method also expects
// Artifactory repository name (<target-artifactory-repository>).
def buildInfo = rtDocker.push "digitaldemo-docker-release-images.jfrog.io/${JOB_NAME}:${releasedVersion}", 'docker-release-images'
//Publish the build-info to Artifactory:
server.publishBuildInfo buildInfo
}
}
This stage consists of following steps
- Finally deploy your release into production environment
when {
expression {
params.RELEASE
}
}
steps {
script {
docker.image("digitaldemo-docker-release-images.jfrog.io/${JOB_NAME}:${releasedVersion}").run('-i -t -p 9000:9000 --name production')
}
}
}