Jenkins - Technological watch

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

In the fast-paced realm of DevOps, continuous integration and continuous delivery (CI/CD) have become the cornerstone of efficient software development. One tool that has been steering this ship towards success is Jenkins. In this technological watch, we’ll delve into the significance of Jenkins, explore the key problems it resolves, showcase some powerful Groovy files examples, and conclude with insights into its enduring impact on modern software development.

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