This is a simple example of how to use Jenkins to deploy a Laravel application to a server.
- Requirements
- Install Jenkins
- Setup Laravel Application in Local
- Install Jenkins, DIND, and SonarQube
- Setup Laravel Pipeline in Jenkins
- Configure Credentials Container Registry in Jenkins
- Configure Credentials Server Deployment in Jenkins
- Setup SonarQube in Jenkins
- Setup SonarQube
- Configure Credentials SonarQube in Jenkins
- Configure Webhook in GitHub
- Clone this repository
git clone https://github.com/jawaracloud/laravel-cicd
- Install dependencies
composer install
npm install
- Create .env file and configure the database
cp .env.example .env
- Generate application key
php artisan key:generate
- Running Docker Compose MySQL
docker compose -f mysql-docker-compose.yml up -d
- Running Laravel Migration
php artisan migrate
- Running Laravel Seed
php artisan db:seed
- Running Laravel Application
php artisan serve
- Create filament user
php artisan filament:user
- Login to Laravel Application Open browser and go to http://localhost:8000/admin email: admin@laravel.com password: 123
- Clone this repository
git clone https://github.com/jawaracloud/jenkins-casc
- Run docker-compose
docker compose up -d
- Login to Jenkins http://localhost:8080 username: admin password: nimda
- Login to SonarQube http://localhost:9000 username: admin password: admin
- Create a new item
- Choose Pipeline
- Configure the pipeline
- Enter name item: laravel-cicd
- Enter description: Laravel CI/CD
- Checklist: GitHub Hook Trigger for GITScm polling for Automatic Build when push to GitHub
- Set Pipeline definition: Pipeline script from SCM
- SCM: Git
- Set Repository URL use SSH or HTTPS ( This is base on your repository credential )
- Set Credentials, we can use username and password or SSH key. If you use SSH key, you must add the Private SSH key to the Jenkins credential. If you use username and password, you must add the username and password to the Jenkins credential.
- Click advanced and set the Refspec:
+refs/tags/*:refs/remotes/origin/tags/*
. This is to fetch the tag from the repository. - Set Branch Specifier:
*/main
. This is to fetch the main branch from the repository. - Add Additional Behaviours and Choose Advanced clone behaviours, set the shallow clone depth to 1 and checklist the Fetch tags, the goal is to fetch the latest tag from the repository.
- Set Script Path: Jenkinsfile
- Checklist Lightweight checkout
- Save
- Configure Container Registry Credentials
- Go to Jenkins > Manage Jenkins > Manage Credentials
- Click on the global domain
- Click Add Credentials
- Choose Kind: Username with password
- Set the username and password for the container registry
- Set credentials ID: container_registry
- Click OK
- Configure Server Deployment Credentials
- Go to Jenkins > Manage Jenkins > Manage Credentials
- Click on the global domain
- Click Add Credentials
- Choose Kind: SSH Username with private key
- Set the username and private key for the server deployment
- Set credentials ID: server_deployment
- Click OK
- Go to Jenkins > Manage Jenkins > System Configuration > System
- Scroll down to the SonarQube servers section
- Click on Add SonarQube
- Check the Enable injection of SonarQube server configuration as build environment variables
- Set the Name: SonarQube
- Set the Server URL: http://sonarqube:9000
- Set the Server authentication token: sonarqube
- Click Save
- Go to SonarQube http://localhost:9000
- Login with username: admin and password: admin
- Click create new project choose manualy and set the project key, name, and branch
- Generate the token and copy the token and save it
- Configure SonarQube Credentials
- Go to Jenkins > Manage Jenkins > Manage Credentials
- Click on the global domain
- Click Add Credentials
- Choose Kind: Secret text
- Set the secret text for the SonarQube token
- Set credentials ID: sonarqube
- Click OK
- Go to GitHub
- Go to the repository
- Go to Settings
- Go to Webhooks
- Click Add webhook
- Set the Payload URL: http://jenkins:8080/github-webhook/ or if use public IP or domain set the Payload URL: http://your-ip-or-domain:8080/github-webhook/
- Set the Content type: application/json
- Set the Secret: leave it blank
- Set the SSL verification: check if you use SSL
- Set the Which events would you like to trigger this webhook?: Let me select individual events, checklist Pushes and Branch or tag creation.
- Click Add webhook
- Go to Jenkins > Manage Jenkins > Manage Credentials
- Click on the global domain
- Click Add Credentials
- Choose Kind: SSH Username with private key, you can create a new key or use an existing key:
ssh-keygen -t rsa -b 4096 -C -m PEM -f ~/.ssh/jenkins_key
- Set the username and private key for the server deployment
- Set credentials ID: server_deployment
- Click Add Credentials ( For IP or Domain Server Deployment )
- Choose Secret Text
- Set the secret text for the server deployment IP or Domain
- Set credentials ID: ip_server_deployment
- Click OK
- Clone the repository
- Create some changes
- Push the changes to the repository with the tag
- Check the Jenkins pipeline
- Check the SonarQube
- Check the Docker Container Registry
- Check the Server Deployment