Jenkins Declarative Pipeline Setup on Ubuntu (GCP)

2025-01-11

Setting up a Continuous Integration and Continuous Deployment (CI/CD) pipeline with Jenkins provides automated builds, testing, and deployments for your applications. In this guide, we’ll configure Jenkins on an Ubuntu (GCP) server, integrate it with GitHub, and set up declarative pipelines for React, Laravel, and CodeIgniter projects.

Using a Jenkinsfile makes the pipeline version-controlled, flexible, and easy to maintain.

Prerequisites

Before proceeding, make sure you have:

  1. A GCP VM instance running Ubuntu
  2. Root or sudo access to the server
  3. GitHub repository with your project code
  4. Basic understanding of CI/CD concepts
  5. SSH key setup for secure GitHub integration

Step 1: Install Jenkins on Ubuntu (GCP)

Update your server and install dependencies:

sudo apt update && sudo apt upgrade -y
sudo apt install openjdk-17-jdk -y

Add Jenkins repository and install Jenkins:

wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo tee /etc/apt/trusted.gpg.d/jenkins.asc
echo "deb http://pkg.jenkins.io/debian-stable binary/" | sudo tee /etc/apt/sources.list.d/jenkins.list
sudo apt update
sudo apt install jenkins -y

Enable and start Jenkins:

sudo systemctl enable --now jenkins
sudo systemctl status jenkins

Step 2: Configure Firewall for Jenkins

If your firewall blocks port 8080, open it:

sudo ufw allow 8080/tcp
sudo ufw enable

Access Jenkins in your browser:

http://<your-server-ip>:8080

Retrieve the admin password:

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

Paste the key in the UI to unlock Jenkins and complete the setup.

Step 3: Install Required Plugins

Go to Manage Jenkins → Manage Plugins and install:

  • Pipeline
  • Git
  • GitHub Integration
  • Deploy to SSH (for remote deployments)
  • NodeJS (for React apps)
  • PHP (for Laravel/CodeIgniter apps)

Step 4: Connect Jenkins with GitHub

Generate an SSH key for Jenkins:

sudo su - jenkins
ssh-keygen -t rsa -b 4096 -C "jenkins@yourdomain.com"
cat ~/.ssh/id_rsa.pub
exit
  • Add this key to GitHub → Repo Settings → Deploy Keys
  • In Jenkins: Manage Jenkins → Manage Credentials → Global Credentials
    Add the private key (id_rsa)

Optionally, create a GitHub Personal Access Token if using webhooks.

Step 5: Create a Jenkins Pipeline Job

  1. Go to Jenkins Dashboard → New Item
  2. Choose Pipeline
  3. Under Pipeline Definition, select Pipeline script from SCM
  4. Configure:
    • SCM: Git
    • Repository URL: git@github.com:your/repo.git
    • Branch: main or master
    • Script Path: Jenkinsfile

Step 6: Add Jenkinsfile to Your Repo

Create a Jenkinsfile in your project repository.

Example for Laravel/CodeIgniter

pipeline {
    agent any
    environment {
        DEPLOY_DIR = "/var/www/html"
    }
    stages {
        stage('Clone Repository') {
            steps {
                git branch: 'main', credentialsId: 'your-credentials-id', url: 'git@github.com:your/repo.git'
            }
        }
        stage('Install Dependencies') {
            steps {
                sh 'composer install --no-dev --prefer-dist'
                sh 'php artisan migrate --force'
            }
        }
        stage('Deploy') {
            steps {
                sh 'cp -r * $DEPLOY_DIR'
                sh 'chmod -R 775 $DEPLOY_DIR'
                sh 'php artisan config:cache'
                sh 'php artisan queue:restart'
            }
        }
    }
}

Example for React

pipeline {
    agent any
    stages {
        stage('Clone Repository') {
            steps {
                git branch: 'main', credentialsId: 'your-credentials-id', url: 'git@github.com:your/repo.git'
            }
        }
        stage('Install Dependencies') {
            steps {
                sh 'npm install'
            }
        }
        stage('Build React App') {
            steps {
                sh 'npm run build'
            }
        }
        stage('Deploy') {
            steps {
                sh 'rsync -avz build/ user@your-server:/var/www/react-app'
            }
        }
    }
}

Step 7: Run the Pipeline

From Jenkins dashboard:

  1. Select your pipeline job
  2. Click Build Now
  3. Jenkins will:
    • Pull code from GitHub
    • Install dependencies
    • Build and deploy your app

Step 8: Automate Deployment with Webhooks (Optional)

Set up GitHub webhook for automatic deployment on push:

  1. Go to GitHub Repo → Settings → Webhooks
  2. Add webhook URL:
    http://your-server-ip:8080/github-webhook/
    
  3. Select Just the push event

Conclusion

By following this guide, you’ve set up a Jenkins Declarative Pipeline on Ubuntu (GCP) that integrates with GitHub and supports deployment for Laravel, CodeIgniter, and React projects.

This CI/CD setup ensures:

  • Version-controlled pipelines (Jenkinsfile)
  • Automated deployments
  • Flexible multi-framework support

With webhooks, deployments become fully automated whenever new code is pushed to GitHub.