Skip to content

Instantly share code, notes, and snippets.

@nhattan
Created March 19, 2020 08:15
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nhattan/53fe407adf503cf2bd928629c7e8eee7 to your computer and use it in GitHub Desktop.
Save nhattan/53fe407adf503cf2bd928629c7e8eee7 to your computer and use it in GitHub Desktop.
Gitlab CI/CD for Rails, Postgres, Rspec, Brakeman
stages:
- build
- test
- deploy
.base:
image: ruby:2.7.0
cache:
key: gems_and_packages
paths:
- apt-cache
- vendor/ruby
- .yarn-cache
policy: pull
before_script:
- gem install bundler --no-document
- bundle install --jobs $(nproc) "${FLAGS[@]}" --path=vendor/ruby
.base_db:
extends: .base
services:
- postgres:latest
variables:
POSTGRES_DB: test_db
POSTGRES_USER: runner
POSTGRES_PASSWORD: ""
POSTGRES_HOST_AUTH_METHOD: trust
before_script:
# install yarn & dependencies
- export APT_CACHE_DIR=`pwd`/apt-cache && mkdir -pv $APT_CACHE_DIR
- wget -q -O - https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
- echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list
- apt-get update -qq && apt-get -o dir::cache::archives="$APT_CACHE_DIR" install -y yarn
- yarn config set cache-folder .yarn-cache
- yarn install
- gem install bundler --no-document
- bundle install --jobs $(nproc) "${FLAGS[@]}" --path=vendor/ruby
# Setup test database
- cp config/database.yml.ci config/database.yml
- RAILS_ENV=test bundle exec rails db:create db:migrate db:schema:load
build:rubocop:
extends: .base
stage: build
cache:
policy: pull-push
script:
- bundle exec rubocop
build:assets_precompile:
extends: .base_db
stage: build
script:
- bundle exec rails assets:precompile
test:rspec:
extends: .base_db
stage: test
script:
- bundle exec rspec
test:brakeman:
extends: .base
stage: test
allow_failure: true
script:
- bundle exec brakeman
deploy:staging:
extends: .base
stage: deploy
only:
- staging
script:
- echo "Deploying to Staging..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment