Build Docker Laravel App with Jenkins

Hi everyone, this article takes you through building a simple react static app using Jenkins. 




Step 1 : I create a folder with name : laravel-app

composer create-project laravel/laravel laravel-app

Step 2 : I create a file Dockfile in folder laravel-app

cd laravel-app
nano Dockerfile


FROM php:8.1-fpm

# Set working directory
WORKDIR /var/www

# Add docker php ext repo
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

# Install php extensions
RUN chmod +x /usr/local/bin/install-php-extensions && sync && \
install-php-extensions mbstring pdo_mysql zip exif pcntl gd memcached

# Install dependencies
RUN apt-get update && apt-get install -y \
build-essential \
libpng-dev \
libjpeg62-turbo-dev \
libfreetype6-dev \
locales \
zip \
jpegoptim optipng pngquant gifsicle \
unzip \
git \
curl \
lua-zlib-dev \
libmemcached-dev \
nginx

# Install supervisor
RUN apt-get install -y supervisor

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy code to /var/www
COPY --chown=www:www-data . /var/www

# add root to www group
RUN chmod -R ug+w /var/www/storage

# Copy nginx/php/supervisor configs
RUN cp docker/supervisor.conf /etc/supervisord.conf
RUN cp docker/php.ini /usr/local/etc/php/conf.d/app.ini
RUN cp docker/nginx.conf /etc/nginx/sites-enabled/default

# PHP Error Log Files
RUN mkdir /var/log/php
RUN touch /var/log/php/errors.log && chmod 777 /var/log/php/errors.log

# Deployment steps
RUN composer install --optimize-autoloader --no-dev
RUN chmod +x /var/www/docker/run.sh

EXPOSE 80
ENTRYPOINT ["/var/www/docker/run.sh"]





Step 3 : I create a file Jenkinsfile

nano Jenkinsfile


pipeline {
agent any
environment {
DOCKERHUB_CREDENTIALS = credentials('dockerhub')
}
stages {
stage('Clone') {
steps {
git branch: 'develop', credentialsId: 'github', url: 'https://github.com/khaitk/laravel-docker-jenkins.git'
}
}

stage('Build Docker Image') {
steps{
sh 'docker build -t khraiteka/laravel-jenkins-docker:$BUILD_NUMBER .'
echo 'Build Image Completed'
}
}

stage('Login') {
steps {
sh 'echo $DOCKERHUB_CREDENTIALS_PSW | docker login -u $DOCKERHUB_CREDENTIALS_USR --password-stdin'
}
}
stage('Push') {
steps {
sh 'docker push khraiteka/laravel-jenkins-docker:$BUILD_NUMBER'
echo 'Push Image Completed'
}
}
}
post {
failure {
mail to: 'khaitkdev@gmail.com',
subject: "FAILED: Build ${env.JOB_NAME}",
body: "Build failed ${env.JOB_NAME} build no: ${env.BUILD_NUMBER}.\n\nView the log at:\n ${env.BUILD_URL}\n\nURL:\n${env.RUN_DISPLAY_URL}"
}
success{
mail to: 'khaitkdev@gmail.com',
subject: "SUCCESSFUL: Build ${env.JOB_NAME}",
body: "Build Successful ${env.JOB_NAME} build no: ${env.BUILD_NUMBER}\n\nView the log at:\n ${env.BUILD_URL}\n\nURL:\n${env.RUN_DISPLAY_URL}"
}
aborted{
mail to: 'khaitkdev@gmail.com',
subject: "ABORTED: Build ${env.JOB_NAME}",
body: "Build was aborted ${env.JOB_NAME} build no: ${env.BUILD_NUMBER}\n\nView the log at:\n ${env.BUILD_URL}\n\nURL:\n${env.RUN_DISPLAY_URL}"
}
}
}



Step 4 : Push source code to github

Step 5 : Config account github and dockerhub at Jenkins UI
Dir : Dashboard => Manage Jenkins => Credentials => System => Global Credential => Add Credentials



Link get token:


Step 6 : Create a Pipeline on Jenkins UI

- You select New Item



- Filter name for your pipeline => click Pipeline => click OK



- Fill url your repository and click check box Github hook trigger for GITScm polling



- Fill in the information like the image below


Step 7 : Click button Build Now, and this's result




Good luck!

Previous Post Next Post