Gitlab-CI: Build & Deploy Laravel5 ke Server Production dengan SSH
Pada tulisan sebelumnya, saya telah melakukan instalasi Laravel5 menggunakan Stack LEMP. Laravel5 tersebut akan saya pasang di gitlab untuk kemudian di-cloning di Server Production melalui Gitlab-ci. Langkah demi langkahnya sebagai berikut
- Buat repository project di GitLab. Project saya namananya laravel ada di sini: https://gitlab.com/tohir212/laravel
- Buat SSH-Keygen di Laptop/PC, langkahnya sbb:
$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/tohir/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: The key fingerprint is: SHA256:dZheNV+blablalba/xQ tohir@ubnt The key's randomart image is: +---[RSA 2048]----+ | oo*&BB| | oo*+B*| | + +.+EB| | o o o .*| | S . . +| | o .o| | o....| | ++o+| | o.oB=| +----[SHA256]-----+
- Tambahkan SSH Keys di Laptop/PC ke Server Gitlab.com. Caranya sbb, di Laptop/PC:
$ cat ~/.ssh/id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAblablablabla tohir@ubnt
Copy rsa dari
ssh-rsa
sampaitohir@ubnt
, kemudian paste diSSH Keys
gitlab, menunya ada di Klik poto profil akun Gitlab yang letaknya di pojok kanan atas » klik menu SSH Keys - Tambahkan SSH_PRIVATE_KEY di Laptop/PC ke Server direktori project kita di Gitlab.com. Caranya adalah sbb, di Laptop/PC:
$ cat ~/.ssh/id_rsa -----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEAxFXN19qDkGrwO7tSHY8xh0XjDguTYwT/X3r+3hy5oIrmZiu1 blablablablablablablablablablablablablablablablablablablablablaa -----END RSA PRIVATE KEY-----
Copy kemudian paste di gitlab, menunya ada di pilih project kita (project yang saya buat namanya laravel) » sorot/klik menu Settings (ada di pojok kiri bawah) » klik sub menu CI/CD » expand Secret variables » isi kolom variables key dengan nama SSH_PRIVATE_KEY dan paste RSA di kolom Input variables value
- Setup ssh Passwordless laptop/PC kita ke Server Production
$ ssh-copy-id -i ~/.ssh/id_rsa.pub '-p 2222' tohir@myserverproduction.com
- Buat gitlab-ci.yml dan push ke alamat repository project
image: ubuntu
# Stages
stages:
- test
- build
- deploy
test:
stage: test
only:
- master
# Select image from https://hub.docker.com/_/php/
image: php:7.1
variables:
# Configure mysql environment variables (https://hub.docker.com/r/_/mysql/)
#MYSQL_ROOT_PASSWORD: 1234
MYSQL_DATABASE: laravel
services:
- name: mariadb:10.2
alias: mysql
before_script:
# Update apt
- apt-get update
# install git & unzip for composer
- apt-get install -qqy git unzip zlib1g-dev libcurl3-dev
# install php extensions
- docker-php-ext-install mbstring pdo_mysql json zip curl
# Install composer
- curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Install all project dependencies
- composer install
# Copy .env
- cp /builds/tohir212/laravel/.env.example /builds/tohir212/laravel/.env
# Migration
- php artisan migrate --seed --force"
script:
- echo 'untuk test unit & functional kedepan bisa pakai vendor/bin/codecept run unit,functional'
build:
stage: build
image: ilyasemenov/gitlab-ci-git-push
only:
- master
script:
- git checkout -b 1.0
- git-push git@gitlab.com:tohir212/laravel.git 1.0
deploy:
stage: deploy
only:
- master
before_script:
# Install ssh-agent, diperlukan utk Docker
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
# Run ssh-agent
- eval $(ssh-agent -s)
# Tambahakn SSH key, lihat SSH_PRIVATE_KEY
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
# Hak akses
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
# Scan server, bisa dibuat banyak sesuai banyaknya server, server saya pakai port 2222
- ssh-keyscan -p 2222 laravel.myserverproduction.com >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
# Atur git, opsional
- git config --global user.email 'tohir@gmail.com'
- git config --global user.name 'Tohir'
script:
- ssh -t -p 2222 tohir@laravel.myserverproduction.com 'cd /usr/share/nginx/html/project && git clone git@gitlab.com:tohir212/laravel.git '
when: manual
Selesai. Berikut penampakan pipelines & job CD/CI-nya.
Kita cek untuk memastikan bahwa di Server Production telah tercloning project laravel kita.
tohir@myserverproduction:~$ ls -lht /usr/share/nginx/html/project/
total 4.0K
drwxrwxr-x 12 tohir tohir 4.0K Mar 5 09:29 laravel
Tinggalkan komentar