Skip to content

Instantly share code, notes, and snippets.

@hzchirs
Last active June 25, 2018 05:49
Show Gist options
  • Save hzchirs/1040282e5b58df509155613c1d8dbee6 to your computer and use it in GitHub Desktop.
Save hzchirs/1040282e5b58df509155613c1d8dbee6 to your computer and use it in GitHub Desktop.
A CircleCI2.0 workflow config
version: 2.0
references:
container_config: &container_config
docker:
- image: circleci/ruby:2.3.7-node-browsers
environment:
BUNDLE_JOBS: 3
BUNDLE_RETRY: 3
BUNDLE_PATH: vendor/bundle
RAILS_ENV: test
- image: circleci/postgres:9.6-alpine
environment:
POSTGRES_USER: postgres
POSTGRES_DB: pg
POSTGRES_PASSWORD: 12345678
working_directory: ~/statementdog
restore_repo: &restore_repo
restore_cache:
key: v1-repo-{{ .Environment.CIRCLE_SHA1 }} # CIRCLE_SHA1: The SHA1 hash of the last commit of the current build.
restore_bundle: &restore_bundle
restore_cache:
# 照 key 的順序去找 cache,如果第一個找不到,則用第二個 key
# 第二個 key 沒有指定 checksum,circleCI 會去找最近且開頭是 v1-gem- 的 cache
keys:
- v1-gem-{{ checksum "Gemfile.lock" }}
- v1-gem-
restore_yarn: &restore_yarn
restore_cache:
keys:
- yarn-{{ checksum "yarn.lock" }}
- yarn-
wait_for_db: &wait_for_db
run:
name: Wait for DB
command: dockerize -wait tcp://localhost:5432 -timeout 1m
db_setup: &db_setup
run:
name: Database setup
command: bundle exec rails db:schema:load --trace
jobs:
checkout_code:
<<: *container_config
steps:
- *restore_repo
- checkout
- run:
name: Which bundler?
command: bundle -v
- save_cache:
key: v1-repo-{{ .Environment.CIRCLE_SHA1 }}
paths:
- ~/statementdog
bundle_dependencies:
<<: *container_config
steps:
- *restore_repo
- *restore_bundle
- run:
name: Bundle Install
command: bundle check || bundle install
- save_cache:
key: v1-gem-{{ checksum "Gemfile.lock" }}
paths:
- vendor/bundle
yarn_dependencies:
<<: *container_config
steps:
- *restore_repo
- *restore_yarn
- run:
name: Yarn Install
command: yarn install --cache-folder ~/.cache/yarn
- save_cache:
key: yarn-{{ checksum "yarn.lock" }}
paths:
- ~/.cache/yarn
run_unit_tests:
<<: *container_config
steps:
- *restore_repo
- *restore_bundle
- *wait_for_db
- *db_setup
- run:
name: Check syntax
command: 'script/syntax_check.sh'
- run: mkdir ~/rspec
- run: mkdir /tmp/test-results
- run:
name: Run Unit tests
environment:
CIRCLE_TEST_REPORTS: /tmp/test-results
command: |
bundle exec rspec --color \
--profile 10 \
--format progress \
--format RspecJunitFormatter \
--out ~/rspec/rspec.xml \
$(circleci tests glob "spec/{models,helpers,controllers,views,queries,uploaders,services,workers,work_dispatchers,lib,libs,mailers}/**/*_spec.rb")
- store_test_results:
path: ~/rspec
- store_artifacts:
path: /tmp/test-results
run_integration_tests:
<<: *container_config
steps:
- *restore_repo
- *restore_bundle
- *wait_for_db
- *db_setup
- run: mkdir ~/rspec
- run: mkdir /tmp/test-results
- run:
name: Run Integration tests
command: |
bundle exec rspec --color \
--profile 10 \
--format progress \
--format RspecJunitFormatter \
--out ~/rspec/rspec.xml \
$(circleci tests glob "spec/features/**/*_spec.rb" "spec/requests/**/*_spec.rb")
- store_test_results:
path: ~/rspec
deploy:
<<: *container_config
steps:
- *restore_repo
- *restore_bundle
- run:
name: Deploy Master to AWS
command: bundle exec cap production deploy
workflows:
version: 2
build_and_test:
jobs:
- checkout_code
- bundle_dependencies:
requires:
- checkout_code
- yarn_dependencies:
requires:
- checkout_code
- run_unit_tests:
requires:
- bundle_dependencies
- run_integration_tests:
requires:
- yarn_dependencies
- bundle_dependencies
- deploy:
requires:
- run_unit_tests
- run_integration_tests
filters:
branches:
only: master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment