Correct way to deploy a laravel application on docker compose with GitLab CI/CD throught local gitlab runner?

41 Views Asked by At

I am currently working on a Laravel project deployed using a docker-compose file. This file deploys a php-fpm container and an nginx container. The objective is to utilize GitLab CI/CD to automate the build and deployment process.

I have installed a GitLab runner instance on a local machine that runs Debian 12, and I'm using it with a shell. I'm attempting to execute various stages (build, test, and deploy) on the GitLab runner to deploy the project on another machine that will host the containers. I've reached the 'docker compose up' job, but I wonder if I'm proceeding correctly.

For the final step in my gitlab-ci file, I use scp to send the docker-compose file to the hosting machine. Then, I use ssh to execute the 'docker compose up' command to deploy it.

stages:
  - build_dependencies
  - test
  - deploy_production

cache:
  paths:
    - vendor/

build_composer:
  stage: build_dependencies
  script:
    - composer install -n
  artifacts:
    paths:
      - ./vendor

pest_test:
  stage: test
  script:
    - ./vendor/bin/pest

larastan_test:
  stage: test
  script:
    - ./vendor/bin/phpstan analyse --memory-limit=2G --no-progress

build_docker_image:
  stage: deploy_production
  dependencies: []
  before_script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
  script:
    - docker build -t $CI_REGISTRY_IMAGE:latest
      -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA -f ./.docker/Dockerfile .
    - docker push $CI_REGISTRY_IMAGE --all-tags
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: on_success

deploy_containers:
  stage: deploy_production

  script:
    - scp /path/to/file username@a:/path/to/destination
    - ssh username@IP "docker compose up -d"
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: manual

Also, in case you would be wondering, my php-fpm container is from a custom image where i get the whole code project inside it and i install composer dependencies without dev ones :

FROM composer:2.7.2 AS composer
WORKDIR /var/www/

COPY . ./

RUN composer install --ignore-platform-reqs --prefer-dist --no-scripts --no-progress --no-interaction --no-dev \
    --no-autoloader

FROM php:8.2.8-fpm

ENV USER=www
ENV GROUP=www
ENV ACCEPT_EULA=Y

# Install dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    libzip-dev \
    zip \
    unzip \
    gnupg \
    gnupg2 \
    nano

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

# Install PHP extensions
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd zip

RUN docker-php-ext-enable zip

#DRIVER SQL SRV
RUN apt-get update

RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add -
RUN curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list
RUN apt-get update
RUN ACCEPT_EULA=Y apt-get install -y msodbcsql18
RUN ACCEPT_EULA=Y apt-get install -y mssql-tools18
RUN echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc
RUN  apt-get install -y unixodbc-dev
RUN apt-get -y install unixodbc-dev
RUN pecl install sqlsrv pdo_sqlsrv

RUN docker-php-ext-enable sqlsrv pdo_sqlsrv
#DRIVER SQL SRV

# Get the build app
COPY --from=composer /var/www/ /var/www/

# Setup working directory
WORKDIR /var/www/

# Create User and Group
RUN groupadd -g 1000 ${GROUP} && useradd -u 1000 -ms /bin/bash -g ${GROUP} ${USER}

# Grant Permissions
RUN chown -R ${USER} /var/www

EXPOSE 9000

CMD ["php-fpm"]

The gitlab-ci file is not finished yet since I need to manage .env files, releases, and some more, but you get the main idea. What do you think about it? Is there any best other way in your opinion?

0

There are 0 best solutions below