CircleCI

In this section we are going to use CircleCI as our continuous-integration service. In a few words CircleCI automates your software builds, tests, and deployments. It supports different programming languages and for our particular case, it supports the Crystal language.

In this section we are going to present some configuration examples to see how CircleCI implements some continuous integration concepts.

CircleCI orbs

Before showing some examples, it’s worth mentioning CircleCI orbs. As defined in the official docs:

Orbs define reusable commands, executors, and jobs so that commonly used pieces of configuration can be condensed into a single line of code.

In our case, we are going to use Crystal’s Orb

Build and run specs

Simple example using latest

Let’s start with a simple example. We are going to run the tests using latest Crystal release:

```yaml title=”.circleci/config.yml” workflows: version: 2 build: jobs:

  1. - crystal/test

orbs: crystal: manastech/crystal@1.0 version: 2.1

  1. Yeah! That was simple! With Orbs an abstraction layer is built so that the configuration file is more readable and intuitive.
  2. In case we are wondering what the job [crystal/test](https://circleci.com/orbs/registry/orb/manastech/crystal#jobs-test) does, we always may see the source code.
  3. ### Using `nightly`
  4. Using nightly Crystal release is as easy as:
  5. ```yaml title=".circleci/config.yml"
  6. workflows:
  7. version: 2
  8. build:
  9. jobs:
  10. - crystal/test:
  11. name: test-on-nightly
  12. executor:
  13. name: crystal/default
  14. tag: nightly
  15. orbs:
  16. crystal: manastech/crystal@1.0
  17. version: 2.1

Using a specific Crystal release

```yaml title=”.circleci/config.yml” workflows: version: 2 build: jobs:

  1. - crystal/test:
  2. name: test-on-0.30
  3. executor:
  4. name: crystal/default
  5. tag: 0.30.0

orbs: crystal: manastech/crystal@1.0 version: 2.1

  1. ## Installing shards packages
  2. You need not worry about it since the `crystal/test` job runs the `crystal/shard-install` orb command.
  3. ## Installing binary dependencies
  4. Our application or maybe some shards may require libraries and packages. This binary dependencies may be installed using the [Apt](https://help.ubuntu.com/lts/serverguide/apt.html) command.
  5. Here is an example installing the `libsqlite3` development package:
  6. ```yaml title=".circleci/config.yml"
  7. workflows:
  8. version: 2
  9. build:
  10. jobs:
  11. - crystal/test:
  12. pre-steps:
  13. - run: apt-get update && apt-get install -y libsqlite3-dev
  14. orbs:
  15. crystal: manastech/crystal@1.0
  16. version: 2.1

Using services

Now, let’s run specs using an external service (for example MySQL):

```yaml title=”.circleci/config.yml” executors: crystal_mysql: docker:

  1. - image: 'crystallang/crystal:latest'
  2. environment:
  3. DATABASE_URL: 'mysql://root@localhost/db'
  4. - image: 'mysql:5.7'
  5. environment:
  6. MYSQL_DATABASE: db
  7. MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'

workflows: version: 2 build: jobs:

  1. - crystal/test:
  2. executor: crystal_mysql
  3. pre-steps:
  4. - run:
  5. name: Waiting for service to start (check dockerize)
  6. command: sleep 1m
  7. - checkout
  8. - run:
  9. name: Install MySQL CLI; Import dummy data
  10. command: |
  11. apt-get update && apt-get install -y mysql-client
  12. mysql -h 127.0.0.1 -u root --password="" db < test-data/setup.sql

orbs: crystal: manastech/crystal@1.0 version: 2.1 ```

note The explicit checkout in the pre-steps is to have the test-data/setup.sql file available.

Caching

Caching is enabled by default when using the job crystal/test, because internally it uses the command with-shards-cache