Jenkins pipeline to define and automate a complete CI/CD process for a web application

Jenkins pipeline to define and automate a complete CI/CD process for a web application


5 min read

Step 1: Set up a Jenkins Server

  • Start by setting up a Jenkins server. You can install Jenkins on your own server or use a cloud-based Jenkins service. Follow the documentation for installation instructions.

      sudo apt update
      sudo apt install openjdk-17-jre
      java -version
      curl -fsSL | sudo tee \
        /usr/share/keyrings/jenkins-keyring.asc > /dev/null
      echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \ binary/ | sudo tee \
        /etc/apt/sources.list.d/jenkins.list > /dev/null
      sudo apt-get update
      sudo apt-get install jenkins
  • Edit the inbound rules for that instance to allow traffic on port 8080.

Unlock Jenkins:

  • Open your web browser and navigate to the Jenkins server's URL (e.g., localhost:8080).

  • You should see the "Unlock Jenkins" page that prompts you to enter the initial admin password.

      sudo cat /var/lib/jenkins/secrets/initialAdminPassword

  • Paste the password you copied from the file into the provided field.

  • Click the "Continue" button.

Install Docker and docker-compose:

  •     sudo apt install
        sudo apt-get install docker-compose
      docker --version

Provide permission for docker:

  • This command adds your current user to the "docker" group, granting them permission to use Docker without needing root privileges:

      sudo usermod -aG docker $USER
  • This command adds the Jenkins user (assuming it's named "jenkins") to the "docker" group, allowing Jenkins to interact with Docker:

      sudo usermod -aG docker jenkins
  • After adding users to the "docker" group, it's a good idea to reboot the system to ensure all changes are applied:

      sudo reboot
  • If you've successfully installed Docker and you're running it on your system, you can open a terminal and run the following command:

      docker ps
  • To log in to Jenkins and provide credentials for DockerHub, follow these steps:

    1. Dashboard: Once you're logged in, you'll be on the Jenkins dashboard.

    2. Navigate to Credentials: Click on "Manage Jenkins" in the left-hand sidebar, then select "Manage Credentials."

    3. Add Credentials: Click on "Global credentials (unrestricted)" and then click on "Add Credentials" on the left side.

    4. Select Credential Type: For DockerHub, you'll want to choose "Username with password."

    5. Enter Credentials: Fill in the following information:

    6. Username: Your DockerHub username

    7. Password: Your DockerHub password or access token (for security reasons, it's recommended to use an access token instead of your actual password)

    8. ID and Description: You can leave the "ID" field as is or provide a meaningful ID for the credentials. In the "Description" field, you can add a brief description of the credentials for reference.

    9. Save Credentials: Click the "OK" button to save the credentials.

  • Use the Jenkinsfile to define the pipeline stages and steps.

    create a New Pipeline: From the Jenkins dashboard, click on "New Item" on the left-hand sidebar to create a new pipeline.

    Enter Pipeline Name: In the "Enter an item name" field, provide a name for your pipeline (e.g., "MyWebAppPipeline").

Enter Pipeline Script: In the text area provided, you can define your pipeline script using the declarative pipeline syntax or the scripted pipeline syntax.

pipeline {
    agent any
    stages {
        stage("Code") {
            steps {
                echo "clone the code"
                git url: "", branch: "main"
        stage("Build") {
            steps {
                echo "build the image"
                sh "docker build -t notes-app ."
        stage("Test") {
            steps {
                echo "running basic tests"
                sh "docker run --rm notes-app python test"
        stage("Push to Docker Hub") {
            steps {
                echo "push the image to dockerhub"
                withCredentials([usernamePassword(credentialsId: "dockerhub", passwordVariable: "dockerhubPass", usernameVariable: "dockerhubUser")]) {
                    sh "docker tag notes-app ${dockerhubUser}/my-note-app:latest"
                    sh "docker login -u ${dockerhubUser} -p ${dockerhubPass}"
                    sh "docker push ${dockerhubUser}/my-note-app:latest"
        stage("Deploy") {
            steps {
                echo "deploy the container"
                sh "docker-compose down && docker-compose up -d"

Once you've entered your pipeline script, click the "Save" button to save the pipeline configuration. Then, you can manually trigger the pipeline by clicking the "Build Now" button on the pipeline's dashboard page.

If the website loads without any issues, it's likely up and running.

  • Use the Jenkinsfile to define the pipeline stages and steps.

    1. Create a Jenkinsfile in the root of your project repository. The Jenkinsfile defines the entire pipeline, including stages, steps, and configurations. Copy the following Jenkins script and paste it into your GitHub repository. After adding the Jenkinsfile, provide the GitHub repository URL to enable automated CI/CD.

Set up notifications to be sent when the build, test, or deployment process fails

  1. In the Jenkins job configuration page, scroll down to the "Post-build Actions" section.

  2. Click on "Add post-build action" and select "Editable Email Notification."

  3. Configure the email settings:

Here's a general outline of how you can set up webhooks:

  1. Access Repository Settings:

    • Open your GitHub repository.

    • Click on the "Settings" tab, usually located on the right-hand side of the repository navigation.

  2. Webhooks Section:

    • In the left sidebar of the Settings page, click on "Webhooks".
  3. Add a New Webhook:

    • Click the "Add webhook" button to create a new webhook.
  4. Configure Webhook:

    • In the "Payload URL" field, provide the URL of the endpoint you want GitHub to send payload.

    • Optionally, you can set a secret token for securing the communication between GitHub and your endpoint. This adds a layer of security to ensure that incoming webhook requests are legitimate.

    • Configure any additional settings that the webhook requires based on your use case.

  5. Test and Save:

    • Most webhook setups allow you to send a test payload to the configured URL to ensure that everything is working correctly.

    • Observe if the webhook triggers your Jenkins pipeline. You can monitor the pipeline's progress in the Jenkins dashboard.

Congratulations! We have successfully set up a complete CI/CD pipeline for your web application using Jenkins. This pipeline will automate the build, testing, and deployment process, ensuring faster and more reliable software delivery.

Happy coding!

Did you find this article valuable?

Support Edvin Dsouza by becoming a sponsor. Any amount is appreciated!