๐Ÿš€ Setting Up a CI/CD Pipeline with Jenkins on Ubuntu

Written by:

A CI/CD (Continuous Integration and Continuous Deployment) pipeline automates software development, testing, and deployment. Jenkins is a popular open-source automation server used for CI/CD.



1. Introduction to CI/CD and Jenkins

  • Continuous Integration (CI): Automates code integration, testing, and validation.
  • Continuous Deployment (CD): Automates software release and deployment.
  • Jenkins: An automation server that facilitates CI/CD pipelines.

2. Prerequisites

Before starting, ensure you have:

  • โœ… Ubuntu 20.04 or later
  • โœ… sudo (administrator) access
  • โœ… Java (JDK) installed (required for Jenkins)

Install Java (JDK 11):

sudo apt update
sudo apt install -y openjdk-11-jdk

3. Installing Jenkins on Ubuntu

1. Update the package index:

sudo apt update

2. Add Jenkins repository and GPG key:

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee /etc/apt/keyrings/jenkins.asc > /dev/null
echo "deb [signed-by=/etc/apt/keyrings/jenkins.asc] https://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null

3. Install Jenkins:

sudo apt update
sudo apt install -y jenkins

4. Start and enable Jenkins:

sudo systemctl start jenkins
sudo systemctl enable jenkins

4. Access Jenkins Web Interface ๐ŸŒ

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

Follow the setup wizard to install recommended plugins and create an admin user.


5. Installing Essential Jenkins Plugins

  • ๐Ÿ”น Git Plugin: Allows Jenkins to pull code from GitHub/GitLab.
  • ๐Ÿ”น Pipeline Plugin: Enables declarative pipelines.
  • ๐Ÿ”น Docker Pipeline Plugin: Required for Docker integration.

6. Creating Your First Jenkins Pipeline

1. Go to Jenkins Dashboard โ†’ “New Item” โ†’ Select “Pipeline”.

2. Enter a name and click OK.

3. Under the “Pipeline” section, paste this script:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the application...'
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests...'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying the application...'
            }
        }
    }
}

7. Integrating GitHub/GitLab with Jenkins

Modify your pipeline script to fetch code from a Git repository:

pipeline {
    agent any
    stages {
        stage('Checkout Code') {
            steps {
                git 'https://github.com/your-repo.git'
            }
        }
    }
}

8. Automating Build Triggers โš™๏ธ

  • Enable Poll SCM to check for repository changes at intervals.
  • Use GitHub Webhooks to trigger builds on new commits.

9. Deploying a Sample Web App ๐ŸŒ

1. Install Docker:

sudo apt install -y docker.io
sudo systemctl start docker
sudo systemctl enable docker

2. Modify pipeline script to deploy an Nginx container:

pipeline {
    agent any
    stages {
        stage('Deploy') {
            steps {
                sh 'docker run -d -p 8080:80 nginx'
            }
        }
    }
}

After running the pipeline, visit http://localhost:8080 to see Nginx running.

๐Ÿš€ Troubleshooting Common Issues

Jenkins not starting?

sudo systemctl restart jenkins

Permission issues?

sudo usermod -aG jenkins $USER

Jenkins build failing?

sudo journalctl -u jenkins --no-pager

๐ŸŽฏ Conclusion

  • โœ… You now have a fully functional CI/CD pipeline using Jenkins.
  • โœ… Jenkins automates testing, builds, and deployments.
  • โœ… Next Steps: Add Kubernetes, AWS, or Terraform integrations.

๐Ÿ“Œ Reference Links



Discover more from DIYLABHub.com

Subscribe to get the latest posts sent to your email.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

google.com, pub-5998895780889630, DIRECT, f08c47fec0942fa0