Gitlab-Ci untuk multiple database Laravel di satu Image Docker

2 menit waktu baca

Berhubung sedang tidak mood ngasih pengantar, langsung saja: saya lampirkan .env.gitlab, .env.gitlab2 dan .gitlab-ci.yml

.env.gitlab

APP_NAME="Project-Riset"
APP_ENV=local
APP_KEY=base64:/QNANSDFFsskdfkfSsdidnfnaSdfoauMYY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://127.0.0.1:8000

DB_CONNECTION_CENTER=center
DB_HOST_CENTER=mysql
DB_PORT_CENTER=3306
DB_DATABASE_CENTER=database_1_riset
DB_USERNAME_CENTER=root
DB_PASSWORD_CENTER=1234

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=database_1_riset
DB_USERNAME=root
DB_PASSWORD=1234

BROADCAST_DRIVER=log
CACHE_DRIVER=array
SESSION_DRIVER=file
QUEUE_DRIVER=sync

.env.gitlab2

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=database_2_riset
DB_USERNAME=root
DB_PASSWORD=1234
CACHE_DRIVER=array

.gitlab-ci.yml

image: alpine:latest

stages:
  - test
  - build
  - deploy

test:
  stage: test
  tags:
    - php
  only:
    - master

  image: infokes/php7.1-epus
  
  variables:
    WITH_XDEBUG: "1"
    DOCKER_DRIVER: overlay2
    MYSQL_USER: root
    MYSQL_ROOT_PASSWORD: 1234
    MYSQL_DATABASES: database_1_riset

  services:
  - name: mariadb:10.3
    alias: mysql

  before_script:
  - apk update && apk add mariadb-client
  - echo "SET GLOBAL sql_mode='NO_ENGINE_SUBSTITUTION';" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql
  - echo "CREATE DATABASE IF NOT EXISTS database_1_riset DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql
  - echo "CREATE DATABASE IF NOT EXISTS database_2_riset DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql
  - echo "GRANT ALL PRIVILEGES ON *.* TO '${MYSQL_USER}'@'%' WITH GRANT OPTION;FLUSH PRIVILEGES;" | mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql
  
  # Install all project dependencies
  - composer install

  # Copy .env
  - cp /builds/tohir/project_laravel_riset_multidb/.env.gitlab /builds/tohir/project_laravel_riset_multidb/.env
  - cp /builds/tohir/project_laravel_riset_multidb/.env.gitlab /builds/tohir/project_laravel_riset_multidb/.env.testing
  - cp /builds/tohir/project_laravel_riset_multidb/.env.gitlab2 /builds/tohir/project_laravel_riset_multidb/.env.testing2

  # Migration
  - php artisan migrate --seed --force
  - php artisan migrate --seed --force --env=testing2

  script:
    - vendor/bin/codecept run unit,api,functional

build:
  stage: build
  
  tags:
    - gitpush
    
  image: infokes/git-push-ci

  only: 
    - master
    
  before_script:
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan 10.10.10.10 >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - git config --global user.email "tohirin07@gmail.com"
    - git config --global user.name "Tohir"
  

  script:
    - git checkout 5.0
    - git-push git@10.10.10.10:tohir/project_laravel_riset_multidb.git 5.0

deploy:2dev:
  stage: deploy

  tags:
    - gitpull
    - migrate

  image: infokes/git-update-apl-ci
  
  only:
    - "5.0"
    
  before_script:
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan -p 212 200.200.x.x >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - git config --global user.email "tohirin07@gmail.com"
    - git config --global user.name "Tohir"
  
  script:
    - ssh -t -p 212 tohir@200.xxx.xxx.xxx '/home/tohir/sh/dev.sh'
    
deploy:2dev-bpjs:
  stage: deploy

  tags:
    - gitpull
    - migrate

  image: infokes/git-update-apl-ci
  
  only:
    - bpjs-v3
    
  before_script:
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - mkdir -p ~/.ssh
    - chmod 700 ~/.ssh
    - ssh-keyscan -p 212 200.xxx.xxx.xxx >> ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - git config --global user.email "tohirin07@gmail.com"
    - git config --global user.name "Tohir"
  
  script:
    - ssh -t -p 212 tohir@200.200.x.x '/home/tohir/sh/dev-bpjs.sh'

Sekian, semoga bisa menjadi rujukan.

Label:

Kategori:

Diperbaharui:

Tinggalkan komentar