Setting Up CI/CD Pipelines with Jenkins on Linux
Introduction
Automating continuous integration and continuous delivery (CI/CD) processes is essential for improving software development efficiency and quality. This article solves the problem of how to configure CI/CD pipelines using Jenkins in a Linux environment, providing practical examples of code and commands.
Installing Jenkins on Linux
The first step is to install Jenkins on your Linux machine. The commands vary depending on the distribution you are using.
Ubuntu/Debian
# Adicionar a chave GPG do Jenkins
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
# Adicionar o repositório do Jenkins
sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
# Atualizar e instalar o Jenkins
sudo apt update
sudo apt install jenkins -yCentOS/RHEL
# Adicionar o repositório do Jenkins
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
# Importar a chave GPG do Jenkins
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
# Instalar o Jenkins
sudo yum install jenkins -yConfiguring Jenkins
After installation, you need to start and enable the Jenkins service.
# Iniciar o Jenkins
sudo systemctl start jenkins
# Habilitar o Jenkins para iniciar na inicialização
sudo systemctl enable jenkinsAccess http://seu_ip:8080 to complete the initial Jenkins configuration.
Creating a Basic Pipeline
Let's create a basic pipeline for a simple Python project.
Repository Configuration
First, configure your Git repository. For example, create a repository on GitHub and add a Jenkinsfile at the project root.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
sh 'python3 -m pytest'
}
}
stage('Test') {
steps {
echo 'Testing...'
sh 'python3 -m pytest'
}
}
}
}Jenkins Configuration
In Jenkins, create a new item and select "Pipeline". Configure the pipeline to point to the Git repository.
Advanced Example: Pipeline with Stage Environments
Let's create a more complex pipeline that includes stage environments (stage and production).
pipeline {
agent any
environment {
AWS_ACCESS_KEY_ID = credentials('aws-access-key-id')
AWS_SECRET_ACCESS_KEY = credentials('aws-secret-access-key')
}
stages {
stage('Build') {
steps {
echo 'Building...'
sh 'python3 -m pytest'
}
}
stage('Deploy to Stage') {
environment {
ENV ='stage'
}
steps {
echo 'Deploying to Stage...'
sh 'ansible-playbook deploy.yml -e env=${ENV}'
}
}
stage('Deploy to Production') {
environment {
ENV = 'production'
}
when {
branch 'main'
}
steps {
echo 'Deploying to Production...'
sh 'ansible-playbook deploy.yml -e env=${ENV}'
}
}
}
}Why This Matters / When to Use
Configuring CI/CD pipelines with Jenkins is crucial for automating the development, testing, and deploy of applications. This reduces delivery time, improves software quality, and allows greater collaboration between teams. Use this method when you need robust and scalable automation for your development projects.
Connection to LPI/CompTIA Certifications
Mastering CI/CD pipeline configuration with Jenkins is a valuable skill for IT professionals seeking certifications such as LPI and CompTIA. These certifications value practical knowledge and the ability to automate processes, which is directly applicable to using Jenkins in Linux environments.
That was just one piece. The course teaches the whole path — from your first command to exam day.