Jenkins - Technological watch

Learn what is Jenkins in less than 5 minutes !
Thursday, November 16, 2023

Jenkins is an open-source automation server widely used for continuous integration and continuous delivery (CI/CD). It helps automate various stages of software development, including building, testing, and deploying applications1. Jenkins is highly extensible, with hundreds of plugins available to integrate with various tools and platforms. A key feature of Jenkins is the Jenkinsfile, a text file that defines the pipeline for a project. This file can be written using either Declarative or Scripted Pipeline syntax, both of which are based on Groovy, a powerful scripting language for the Java platform. The Jenkinsfile allows for version control of the pipeline code, making it easier to review, iterate, and maintain. Groovy scripts within Jenkinsfiles enable complex automation tasks, providing flexibility and control over the CI/CD process.

Problematics

  1. Manual Deployment Hassles: Jenkins streamlines the deployment process by automating manual, error-prone tasks. From code integration to deployment, Jenkins ensures a seamless pipeline, saving developers precious time and reducing the likelihood of human errors.

  2. Inconsistent Development Environments: Ensuring consistency across different development environments can be a challenge. Jenkins tackles this problem by enabling the creation of reproducible and standardized build environments, promoting a reliable and uniform development process.

  3. Lack of Visibility and Monitoring: Jenkins provides real-time insights into the status of builds and deployments. With detailed logs and reports, developers gain valuable visibility, allowing them to identify and address issues promptly.

  4. Slow Feedback Loops: Jenkins accelerates the feedback loop by automating the testing process. Developers receive rapid feedback on the success or failure of their code changes, facilitating quicker iterations and improvements.

Examples

Simple example

Create a Jenkinsfile and put it in the root directory of your Git project.

Jenkinsfile
pipeline {
agent {
// Specify the agent (where the build runs)
// e.g., 'docker', 'node', 'label', 'any', etc.
}
// Or write: "agent any"
// The pipeline can take parameters like this:
parameters {
string(name: 'APP_NAME', defaultValue: 'app-1234', description: 'Name of the application')
choice(name: 'ENV', choices: ['dev', 'hom', 'prd'], description: 'Environment name')
}
stages {
// Define your build stages here
stage('Build') {
steps {
// Define steps for the 'Build' stage
}
}
stage('Test') {
steps {
// Define steps for the 'Test' stage
}
}
stage('Deploy') {
steps {
// Your deployment steps here
echo "You selected: ${params.ENV}"
}
}
}
post {
// Define post-build actions, such as notifications or cleanup
}
}

Shared libraries example

Groovy allows the creation of shared libraries, enhancing code reuse.

/vars/helloWorld.groovy
def call(Map config = [:]) {
echo "This is a shared library function"
sh "echo Hello ${config.name}"
}

Here is a second example:

/vars/getSomeToken.groovy
def call(Map config = [:]) {
// Multi-lines sh script
sh """
export APP_TOKEN="Some token"
# Pass an sh variable to Groovy script
echo \$APP_TOKEN > APP_TOKEN.txt
"""
def APP_TOKEN = readFile("APP_TOKEN.txt")
if (APP_TOKEN == null || APP_TOKEN.trim() == '') {
error("APP_TOKEN is not defined")
}
return APP_TOKEN
}

Then, you can reuse this code like this:

Jenkinsfile
@Library('my-shared-library') _
// ...
stages {
stage("Init") {
steps {
script {
env.token = getSomeToken()
}
}
}
}

Use credentials example

First, define credentials for the projects. Then you can use withCredentials() helper.

stages {
stage("Init") {
steps {
script {
env.VAULT_DOMAIN = vaultGetDomain()
}
withCredentials([usernamePassword(credentialsId: 'VAULT_CREDS', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
script {
env.VAULT_TOKEN = vaultGetUserToken(VAULT_DOMAIN: env.VAULT_DOMAIN, USERNAME: USERNAME, PASSWORD: PASSWORD)
}
}
}
}
}

Conclusion

In the ever-evolving landscape of DevOps, Jenkins remains a lighthouse, guiding development teams towards efficient and reliable CI/CD practices. By addressing the aforementioned problematics and leveraging the power of Groovy, Jenkins stands as a testament to the transformative impact of automation in modern software development. As organizations continue to navigate the complex waters of software delivery, Jenkins remains a steadfast companion, ensuring smooth sailing and successful software releases.


Recommended articles