Report this

What is the reason for this report?

Jenkins Build Shell Command

Posted on March 13, 2021

Hi, I want to setup CI to my project. I have installed Jenkins and Plesk on my server. I have integrated bitbucket to the Build. But don’t know how to write the shell commands to pull the Laravel code from the stage branch. Can you configure it. I will share the IP Address of the server



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.

Hello there,

I would suggest that you check out this step by step tutorial on How To Set Up Continuous Integration Pipelines in Jenkins:https:

https://www.digitalocean.com/community/tutorials/how-to-set-up-continuous-integration-pipelines-in-jenkins-on-ubuntu-16-04

Hope that this helps!

To set up Continuous Integration (CI) with Jenkins for your Laravel project and pull code from the stage branch of your Bitbucket repository, follow these steps:

1. Set Up a Jenkins Job for Laravel

  1. Log in to Jenkins.
  2. Create a new job:
    • Click “New Item”.
    • Enter a name for the job (e.g., “Laravel CI - Stage”).
    • Select Freestyle Project and click OK.

2. Configure Source Code Management

  1. In the job configuration page, scroll to Source Code Management and select Git.
  2. Add your Bitbucket repository URL:
https://bitbucket.org/your_username/your_repository.git

Provide your Bitbucket credentials (Jenkins will prompt you to add them). Specify the branch to build: stage

3. Add Build Steps

Scroll to Build and select Execute Shell to add shell commands.

4. Shell Script to Pull Laravel Code

Use the following script as an example. Replace placeholders (like /var/www/your_laravel_project) with your actual Laravel project directory:

#!/bin/bash

# Define variables
DEPLOY_DIR="/var/www/your_laravel_project"

# Ensure the deployment directory exists
if [ ! -d "$DEPLOY_DIR" ]; then
    echo "Deployment directory does not exist. Exiting."
    exit 1
fi

# Navigate to the deployment directory
cd $DEPLOY_DIR

# Stash local changes, if any
git stash

# Pull the latest changes from the stage branch
git checkout stage
git pull origin stage

# Set proper permissions (optional, adjust based on your server setup)
chown -R www-data:www-data $DEPLOY_DIR
chmod -R 775 $DEPLOY_DIR/storage $DEPLOY_DIR/bootstrap/cache

# Install/update dependencies
composer install --no-dev --optimize-autoloader

# Run database migrations (optional)
php artisan migrate --force

# Clear and cache configurations
php artisan config:clear
php artisan config:cache
php artisan route:clear
php artisan route:cache
php artisan view:clear
php artisan view:cache

# Restart queues (if applicable)
php artisan queue:restart

echo "Deployment completed successfully."

5. Save and Test

  1. Save the Jenkins job configuration.
  2. Trigger a manual build to test the setup.
  3. Verify that the code is pulled and deployed correctly to your server.

6. Automate Builds on Push

  1. In Bitbucket, go to Repository Settings > Webhooks.
  2. Add a new webhook:
    • URL: http://<JENKINS_SERVER_IP>:8080/bitbucket-hook/
    • Trigger: Push events.

7. Verify the Workflow

  • Push a change to the stage branch in Bitbucket.
  • Confirm that Jenkins triggers a build automatically and deploys the Laravel project.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.