EN VI

Step by step to install git CI/CD on centos laravel project

* On server centos:

yum -y install acl
sudo adduser deployer
sudo setfacl -R -m u:deployer:rwx /var/www

- generate ssh key

ssh-keygen -t rsa -C "deployer" -f ~/.ssh/id_rsa -P ""


- As the deployer user on server and run command below:
  + Copy the content of public key to authorized_keys
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
  + Copy the private key text block
cat ~/.ssh/id_rsa


 - On git:
   + setting ci_cd, Add variable:
   + key: SSH_PRIVATE_KEY
   + value: private key has been copy


 - On sever:

cat ~/.ssh/id_rsa.pub


 - On git:
settings/repository add Deploy keys
key: private key has been copy


 - In source code:

composer require laravel/envoy --dev


  + add file Envoy.blade.php

@servers(['web' => 'deployer@{your ip host}'])

@setup
    $repository = '[email protected]'; // your repository
    $releases_dir = '/var/www/html/project'; // your dir on host
    $app_dir = '/var/www/html';
    $release = date('YmdHis');
    $new_release_dir = $releases_dir . '/' . $release;
@endsetup

@story('deploy')
    pull_repository
    run_composer
    migrate
@endstory

@task('pull_repository')
    echo 'Pull repository...'
    cd {{ $releases_dir }}
    echo 'Reset hard head...'
    git reset --hard HEAD
    echo 'Pulling...'
    git pull origin master
@endtask

@task('run_composer')
    echo "Starting deployment..."
    cd {{ $releases_dir }}
    composer install --ignore-platform-reqs
@endtask

{{-- migrate app database --}}
@task('migrate')
    echo 'Migrating...'
    cd {{ $releases_dir }}
    php artisan migrate --force
@endtask


  + add file .gitlab-ci.yml

image: registry.gitlab.com/1206dev/blog:latest

stages:
  - deploy_dev

deploy_dev:
  stage: deploy_dev
  script:
    # Add the private SSH key to the build environment
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

    # Run Envoy
    - ~/.composer/vendor/bin/envoy run deploy --commit="$CI_COMMIT_SHA"
  rules:
    - if: $CI_COMMIT_BRANCH == "master" && $CI_PIPELINE_SOURCE == "push"
    - if: $CI_COMMIT_MESSAGE =~ /\[build\]/
      when: always
    - when: never


  + add Dockerfile

# Set the base image for subsequent instructions
FROM php:7.4

# Update packages
RUN apt-get update

# Install PHP and composer dependencies
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip

# Clear out the local repository of retrieved package files
RUN apt-get clean

# Install needed extensions
# Here you can install any other extension that you need during the test and deployment process
RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Install Composer
RUN curl --silent --show-error "https://getcomposer.org/installer" | php -- --install-dir=/usr/local/bin --filename=composer

# Install Laravel Envoy
RUN composer global require "laravel/envoy=~1.0"


*In command location source code:
 - Create Container Registry

docker login registry.gitlab.com
docker build -t registry.gitlab.com/1206dev/blog .
docker push registry.gitlab.com/1206dev/blog


 - Install git runner:

docker run --rm -it -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register
    Enter the registration token from CI/CD Settings/Runners


 - Config gitlab-ci

printf "my_username:my_password" | openssl base64 -A


 - On git lab: Settings/ CI/CD / Variables/ Add
   key: DOCKER_AUTH_CONFIG
   and Value:

{
    "auths": {
        "registry.your_regis": {
            "auth": "(Base64 content from above)"
        }
    }
}


Fix error: envoy deployer@: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password)

chmod 700 .ssh
chmod 600 .ssh/authorized_keys
restorecon -r -vv .ssh/authorized_keys



Rating: 10 (1 Votes)
Comment

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login