By KFSys
System Administrator
Hey all,
Today, I’ll talk about how to automate your Laravel Application to be deployed with Docker by CI/CD - Jenkins on Github PR merges.
Laravel is a popular PHP web application framework, and Docker is a platform that simplifies application deployment using containers. Combining these two technologies with Jenkins can create a powerful CI/CD pipeline. In this mini-tutorial, we will guide you through setting up a Jenkins job that deploys your Laravel application with Docker whenever a pull request (PR) is merged into your Git repository.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Accepted Answer
In this mini-tutorial, I’ll assume you have a Laravel Application and Jenkins server configured however if you don’t have anything, I’ll recommend checking our MarketPlace for a Laravel ready Droplet:
https://marketplace.digitalocean.com/apps/laravel
Additionally, if you want to have your Database Managed, you can check the following:
https://docs.digitalocean.com/products/databases/mysql/
For Jenkins, you can follow this tutorial:
https://www.digitalocean.com/community/tutorials/how-to-install-jenkins-on-ubuntu-20-04
Lastly, to configure Docker if needed, you can follow this tutorial:
Navigate to the Jenkins dashboard and create a new job. Choose “Pipeline” and provide a unique name for the job.
Navigate to the “Pipeline” section and select “Pipeline script from SCM” in the “Definition” field. Choose “Git” as your SCM and provide your repository URL and credentials as needed.
To trigger the build on PR merge commits, you can use the “GitHub hook trigger for GITScm polling” option. First, install the “GitHub Integration” plugin in Jenkins, then navigate to the “Build Triggers” section and check the “GitHub hook trigger for GITScm polling” box.
Next, configure a webhook in your GitHub repository. In your repository settings, go to “Webhooks” and click on “Add webhook.” Set the “Payload URL” to http://your-jenkins-url/github-webhook/
and select “Let me select individual events.” Check the “Pull requests” event and save the webhook.
In the “Pipeline” section, add the following Groovy script to the “Script Path” field:
pipeline {
agent any
environment {
GIT_COMMIT_SHORT = "${sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()}"
}
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'your-repository-url'
}
}
stage('Create Dockerfile') {
steps {
writeFile file: 'Dockerfile', text: '''
FROM php:7.4-fpm
# Install required extensions and dependencies
RUN apt-get update && apt-get install -y \
libpng-dev \
libjpeg-dev \
libfreetype6-dev \
libzip-dev \
unzip \
git \
&& docker-php-ext-configure gd --with-freetype --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install pdo_mysql zip
# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Set working directory
WORKDIR /var/www/html
# Copy application files
COPY . /var/www/html
# Install dependencies
RUN composer install --no-interaction --no-dev --optimize-autoloader
# Set permissions
RUN chown -R www-data:www-data /var/www/html/storage
RUN chown -R www-data:www-data /var/www/html/bootstrap/cache
'''
}
}
stage('Build Docker Image') {
steps {
sh "docker build -t your-dockerhub-username/your-image-name:${env.GIT_COMMIT_SHORT} ."
}
}
stage('Push Docker Image') {
steps {
withCredentials([string(credentialsId: 'your-dockerhub-credentials-id', variable: 'DOCKERHUB_TOKEN')]) {
sh "echo '${DOCKERHUB_TOKEN}' | docker login -u your-dockerhub-username --password-stdin"
sh "docker push your-dockerhub-username/your-image-name:${env.GIT_COMMIT_SHORT}"
}
}
}
stage('Deploy Laravel Application') {
steps {
// Replace 'ssh-credentials-id' with your SSH credentials ID in Jenkins
sshagent(['ssh-credentials-id']) {
// Replace 'your-deployment-server' with your server's IP or domain
sh "ssh your-deployment-server 'docker pull your-dockerhub-username/your-image-name:${env.GIT_COMMIT_SHORT}'"
sh "ssh your-deployment-server 'docker-compose down && docker-compose up -d --force-recreate'"
}
}
}
}
post {
failure {
// Configure notifications if required, e.g., email, Slack, etc.
}
}
}
This script defines a pipeline with the following stages:
docker-compose
to recreate the services.Make sure to replace the placeholders (e.g., your-repository-url
, your-dockerhub-username
, your-image-name
, your-dockerhub-credentials-id
, ssh-credentials-id
, and your-deployment-server
) with your own values.
Create a pull request and merge it in your Git repository. The Jenkins job should trigger automatically and execute the pipeline, deploying your Laravel application using Docker.
You have now successfully created a Jenkins job that deploys your Laravel application with Docker on Git PR merge commits. With this CI/CD pipeline, your development team can streamline the deployment process and ensure that your application stays up-to-date with the latest changes.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.