# PHP

# Configuration sample

workspace: true

stages:
  - build
  - test

jobs:
- name: build
  stage: build
  image: sunasteriskrnd/php-workspace:7.4
  services:
  - image: postgres:12-alpine
    name: postgres-test
    environment:
      POSTGRES_DB: xxx
      POSTGRES_USER: xxx
      POSTGRES_PASSWORD: xxx
  environment:
    APP_ENV: testing
  cache:
  - key: composer_vendor_$CI_BRANCH
    paths:
      - vendor
  before_script:
  - cp .env.example .env.testing
  - composer install
  - php artisan key:generate
  - php artisan migrate
  script:
  - composer sniff
  - composer test
  after_script:
  - echo "Finish job"
  only:
    branches:
    - master
    events:
    - pull_request
  except:
    branches:
    - develop
    events:
    - push
  artifacts:
    paths:
    - app/
    expires_in: 3 days

- name: test:node
  stage: test
  image: node:12-alpine
  script:
  - yarn
  - yarn lint

# Auto deploy with Deployer

You can specify a job to automate deploy project whenever target branch has new code version, follow below steps:

  • First, go to Project Secrets and add new secret variable, whose name SSH_PRIVATE_KEY and value is your SSH Private Key that used to access your server, then pass to your key to job container.
  • Add deploy job to .sun-ci.yml then push it to target branch:
workspace: true

stages:
  - build
  - deploy

jobs:
- name: build
  stage: build
  image: sunasteriskrnd/php-workspace:7.4
  services:
  - image: postgres:12-alpine
    name: postgres-test
    environment:
      POSTGRES_DB: xxx
      POSTGRES_USER: xxx
      POSTGRES_PASSWORD: xxx
  environment:
    APP_ENV: testing
  cache:
  - key: composer_vendor_$CI_BRANCH
    paths:
      - vendor
  before_script:
  - cp .env.example .env.testing
  - composer install
  - php artisan key:generate
  - php artisan migrate
  script:
  - composer sniff
  - composer test
  after_script:
  - echo "Finish job"
  only:
    branches:
    - master
    events:
    - pull_request
  except:
    branches:
    - develop
    events:
    - push

- name: deploy:production
  stage: deploy
  image: sunasteriskrnd/php-workspace:7.4
  before_script:
  - composer require deployer/deployer --dev
  - apk add openssh
  - mkdir /root/.ssh
  - echo "$SSH_PRIVATE_KEY" > /root/.ssh/id_rsa
  - chmod 700 -R /root/.ssh
  - chmod 600 -R /root/.ssh/*
  - echo "Host *\n\tStrictHostKeyChecking no\n\tIdentityFile /root/.ssh/id_rsa\n\tAddKeysToAgent yes\n" >> /root/.ssh/config
  - ssh-keyscan -H $SERVER_IP >> ~/.ssh/known_hosts
  script:
  - php vendor/bin/dep deploy production -vv
  only:
    branches:
    - master
  except:
    events:
    - pull_request

Note: Two jobs above only run when you pushed to branch staging or master.

# Reference