# Ruby

# Configuration sample

workspace: true

stages:
- build
- test

jobs:
- name: build:prepare
  stage: build
  image: sunci/ruby:2.5.1
  script:
  - cp database-ci.yml config/database.yml
  - bundle _2.1.4_ install --path vendor/bundle
  cache:
  - key: vendor_$CI_BRANCH
    paths:
    - vendor/bundle

- name: test:rspec
  stage: test
  image: sunci/ruby:2.5.1
  services:
  - redis:alpine
  - image: postgres:12-alpine
    name: postgresql
    environment:
    - POSTGRES_DB=xxx
    - POSTGRES_USER=xxx
    - POSTGRES_PASSWORD=secret
  before_script:
  - bundle _2.1.4_ install --path vendor/bundle
  - mkdir .sun-ci
  script:
  - bundle _2.1.4_ exec rspec --format html --out .sun-ci/rspec.html spec/
  only:
    branches:
    - milestone1
  artifacts:
    name: rspec_report
    paths:
    - .sun-ci
    expires_in: 3 days

- name: test:bundle_audit
  stage: test
  image: sunci/ruby:2.5.1
  before_script:
  - bundle _2.1.4_ install --path vendor/bundle
  script:
  - bundle-audit check --update
  only:
    branches:
    - milestone1

Note: Because some gem connect to external lib in system, you must run bundle install --path vendor/bundle before execute main script. If you used shared workspace (workspace: true) and cached last vendor/bundle, it just takes 1 second to restore installed library.

# Auto deploy with Capistrano

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. Sun* CI automates pass your key to job container.
  • Add deploy job to .sun-ci.yml then push it to target branch:
workspace: true

stages:
- build
- test
- deploy

jobs:
- name: deploy:staging
  stage: deploy
  image: sunci/ruby:2.5.1
  before_script:
  - bundle _2.1.4_ install --path vendor/bundle
  script:
  - bundle _2.1.4_ exec cap staging deploy
  only:
    branches:
    - staging
  except:
    events:
    - pull_request

- name: deploy:production
  stage: deploy
  image: sunci/ruby:2.5.1
  before_script:
  - bundle _2.1.4_ install --path vendor/bundle
  script:
  - bundle _2.1.4_ exec cap production deploy
  only:
    branches:
    - master
  except:
    events:
    - pull_request

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

# Reference