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!
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Prerequisites:
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:
https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-compose-on-ubuntu-22-04
Configure Jenkins Job
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.Write the Jenkins Pipeline Script
In the “Pipeline” section, add the following Groovy script to the “Script Path” field:
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
, andyour-deployment-server
) with your own values.Test the Pipeline
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.
Conclusion
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.
very helpful!