DeveloperBreeze

DevSecOps is a natural evolution of the DevOps practice, integrating security directly into the continuous integration and continuous deployment (CI/CD) pipelines. This ensures that security is a shared responsibility across all stages of development rather than being an afterthought. Jenkins, an open-source automation server, plays a pivotal role in this setup by helping to automate CI/CD workflows.

In this tutorial, we’ll walk you through setting up a secure CI/CD pipeline with Jenkins, incorporating DevSecOps practices to ensure code integrity, vulnerability scanning, and automated security testing.

What is DevSecOps?

DevSecOps stands for Development, Security, and Operations. It builds on the principles of DevOps but emphasizes security as a fundamental component of the software development life cycle. In DevSecOps, security is not just the responsibility of security experts; developers, operations, and even testers play a role in ensuring that security best practices are followed throughout the CI/CD pipeline.

Why Jenkins for DevSecOps?

Jenkins is one of the most widely used CI/CD tools. Its versatility, combined with a large number of plugins, makes it a powerful choice for automating the integration of security checks at every stage of the development process. Jenkins integrates easily with tools for static code analysis, vulnerability scanning, and compliance testing, making it an ideal platform for DevSecOps.


Prerequisites:

  • Basic knowledge of Jenkins and DevOps.
  • Jenkins installed and running on your local machine or a cloud instance.
  • GitHub or GitLab repository to host your code.
  • Docker installed (optional but recommended for containerized security tools).

Step 1: Setting Up Jenkins

  1. Install Jenkins:

If you haven’t installed Jenkins yet, follow the instructions on Jenkins' official website. Once installed, start Jenkins and log into the Jenkins dashboard at http://localhost:8080.

  1. Create a New Jenkins Job:

From the dashboard, click New Item. Choose Pipeline and give it a descriptive name, such as Secure-CI-CD-Pipeline. This will help you organize jobs within Jenkins.

  1. Install Security Plugins:

Jenkins has numerous plugins that integrate security checks into your pipeline. Key plugins include:

  • OWASP Dependency-Check: Scans for known vulnerabilities in project dependencies.
  • SonarQube: Performs static code analysis to identify bugs, code smells, and security vulnerabilities.
  • Aqua Security Microscanner or Trivy: Tools to scan Docker images for vulnerabilities.

To install plugins, go to Manage Jenkins > Manage Plugins and search for these tools under the Available tab.


Step 2: Creating a Secure CI/CD Pipeline in Jenkins

1. Basic Pipeline Setup:

We will use a Jenkinsfile to define the pipeline as code. The Jenkinsfile is stored in your version control system (e.g., GitHub or GitLab) and tells Jenkins how to run your CI/CD pipeline.

pipeline {
    agent any

    stages {
        stage('Clone Repository') {
            steps {
                // Pull code from your GitHub or GitLab repository
                git url: 'https://github.com/your-repository.git'
            }
        }

        stage('Build') {
            steps {
                // Build the application (e.g., using Maven or Gradle)
                sh 'mvn clean package'
            }
        }
    }
}

This basic pipeline pulls code from a Git repository and builds it using Maven.

2. Adding Security to the Pipeline:

Step 1: Static Code Analysis with SonarQube

SonarQube can be integrated into your Jenkins pipeline to perform static analysis for code quality and security issues.

  • Install SonarQube locally or use a cloud instance.
  • Add SonarQube to your Jenkins pipeline:
stage('Static Code Analysis') {
    steps {
        // Run SonarQube scan
        withSonarQubeEnv('SonarQube') { // Name must match your SonarQube configuration in Jenkins
            sh 'mvn sonar:sonar'
        }
    }
}
Step 2: Vulnerability Scanning with OWASP Dependency-Check

OWASP Dependency-Check scans your dependencies for known vulnerabilities.

  • First, install the OWASP Dependency-Check plugin in Jenkins.
  • Add the following to your pipeline to integrate dependency scanning:
stage('Dependency Vulnerability Scan') {
    steps {
        // Run OWASP Dependency-Check
        dependencyCheck additionalArguments: '--format XML', odcInstallation: 'Dependency-Check'
    }
}
Step 3: Container Vulnerability Scanning (Optional)

If you’re using Docker containers, scanning them for vulnerabilities is critical. You can integrate Docker image scanning tools like Trivy or Aqua Security into Jenkins.

  • Install Docker on the Jenkins server.
  • Add container scanning to your pipeline:
stage('Container Security Scan') {
    steps {
        // Scan Docker image for vulnerabilities
        sh 'docker build -t my-app .'
        sh 'trivy image my-app'
    }
}
Step 4: Unit Tests and Security Testing

Security unit tests and API tests should also be part of your CI/CD pipeline.

stage('Run Unit and Security Tests') {
    steps {
        // Run unit tests
        sh 'mvn test'

        // You can also integrate security testing tools like ZAP or Gauntlt here
    }
}

Step 3: Notifications and Reporting

After the security tests and scans are completed, it’s important to set up notifications in case of any issues.

  1. Configure Email Notifications:

Jenkins can be configured to send notifications when a pipeline fails. Go to Manage Jenkins > Configure System and set up an email server.

  1. Slack Integration (optional):

If your team uses Slack, you can integrate Jenkins with Slack to receive real-time notifications about your pipeline status.

  1. Security Reports:

Tools like OWASP Dependency-Check and SonarQube generate security reports that can be visualized on the Jenkins dashboard or sent via email.


Step 4: Implementing Automated Rollback (Optional)

If a critical vulnerability is detected, you might want to automate a rollback or stop deployment. You can add conditional checks in the pipeline to handle this:

stage('Conditional Deployment') {
    steps {
        script {
            // Example: if vulnerability scan fails, skip deployment
            def scanResult = sh(script: 'check-vulnerability.sh', returnStatus: true)
            if (scanResult != 0) {
                error('Vulnerability scan failed. Aborting deployment.')
            } else {
                // Proceed with deployment
                sh 'kubectl apply -f deployment.yaml'
            }
        }
    }
}

Conclusion

In this tutorial, we set up a secure CI/CD pipeline with Jenkins, integrating static code analysis, dependency scanning, and container vulnerability assessments. By incorporating DevSecOps practices, you ensure that security is embedded into every step of the development process, from code submission to deployment.

The next step is to scale your DevSecOps practice, ensuring that security tools and automation continue to evolve with your applications and infrastructure.

Continue Reading

Discover more amazing content handpicked just for you

Tutorial

How to Stop SSH From Timing Out

On your local machine, edit or create:

nano ~/.ssh/config

Aug 21, 2025
Read More
Tutorial

🛡️ Protect Your Forms Like a Pro: Anti-Spam Techniques That Actually Work

Most humans take a few seconds to fill a form. Bots fill and submit instantly.

  • Track the time between when the form is rendered and when it’s submitted.
  • Reject if submitted too fast (e.g., < 3 seconds).

Apr 04, 2025
Read More
Tutorial

How To view tables and the number of records in each table in a PostgreSQL database,

  • Right-click on each table, select View/Edit Data > All Rows to see the records.
  • Or, run the above SQL query in the Query Tool to see all tables with row counts.

These methods will help you view tables and get a record count for each in your PostgreSQL database.

Nov 06, 2024
Read More
Tutorial

How to install PostgreSQL

Go to the PostgreSQL website and download the installer.

  • Launch the installer and follow the instructions.
  • Select Command Line Tools during the installation process.

Nov 06, 2024
Read More
Tutorial

How to view tables on a PostgreSQL database hosted on Heroku

Once connected to the database, you can list all tables with:

   \dt

Nov 06, 2024
Read More
Tutorial

How to view your Heroku Apps

To view your Heroku apps, you can use either the Heroku CLI or the Heroku Dashboard.

Make sure you’re logged in to the Heroku CLI by running:

Nov 06, 2024
Read More
Tutorial

Clearing Unnecessary Logs on Ubuntu

Identify which log files are taking up the most space by using the du command:

du -h /var/log/

Oct 24, 2024
Read More
Tutorial
bash

How to Install and Configure Apache on Ubuntu

sudo tail -f /var/log/apache2/error.log

Congratulations! You have successfully installed and configured Apache on your Ubuntu server. You can now host your websites and further customize your Apache setup according to your needs.

Oct 21, 2024
Read More
Tutorial
bash

How to Create SSL for a Website on Ubuntu

Save the file and restart Nginx:

sudo systemctl restart nginx

Oct 21, 2024
Read More
Tutorial
bash

How to Install MongoDB Shell (mongosh) on Ubuntu

tar -xzvf mongosh-1.10.1-linux-x64.tgz

Move the mongosh binary to a directory included in your system’s PATH, such as /usr/local/bin/:

Oct 18, 2024
Read More
Tutorial

How to Install MongoDB on Ubuntu

You should see an output indicating that MongoDB is active and running. You can also connect to the MongoDB shell to ensure it's working:

mongosh

Oct 18, 2024
Read More
Cheatsheet

PM2 Cheatsheet

  • -i max starts as many instances as there are CPU cores.
pm2 start app.js --watch

Oct 14, 2024
Read More
Tutorial
bash

How to Update Node.js and npm on Ubuntu

sudo apt install -y nodejs

This installs Node.js and the latest version of npm that comes with it.

Oct 03, 2024
Read More
Tutorial
bash

How to Grant MySQL Root Privileges for 127.0.0.1

Inside the MySQL shell, run the following command:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' WITH GRANT OPTION;

Oct 03, 2024
Read More
Tutorial
bash

How to Reset the MySQL Root Password Using DROP USER

This ensures that all changes you've made to users and permissions take effect immediately.

After resetting the password and recreating the root user, you need to revert the changes made to the MySQL configuration.

Oct 03, 2024
Read More
Tutorial
bash

How to Use OpenVPN on Ubuntu

Once the installation is complete, you can verify the installation by checking the version of OpenVPN:

openvpn --version

Sep 15, 2024
Read More
Tutorial
bash

Recovering Your Linux System with GRUB Rescue Commands

GRUB rescue mode can be daunting, but with the right commands and knowledge, you can recover your Linux system and restore it to full functionality. By understanding how to navigate the GRUB rescue prompt, identify the correct boot partition, and manually boot into your system, you can troubleshoot and fix many common boot issues. Always remember to back up your system and GRUB configuration to avoid these situations in the future.

  • Official GRUB Documentation: A comprehensive guide to GRUB and its commands.
  • Linux Boot Process Explained: Learn more about how Linux boots and where GRUB fits in.

Sep 01, 2024
Read More
Tutorial
bash

How to Generate and Use SSH Keys with ssh-keygen

Output:

After successfully generating the keys, you’ll see a message similar to this:

Aug 29, 2024
Read More
Tutorial

How to Install an AppImage on Linux

  • The ./ tells the terminal to execute the file in the current directory.

Some AppImages offer the option to integrate with your desktop environment, meaning they can add menu entries, icons, and MIME type associations.

Aug 21, 2024
Read More
Tutorial

Adding a Subdomain on an Apache Server

   sudo apachectl configtest
   sudo systemctl reload apache2

Aug 21, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!