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:

!!! example “.circleci/config.yml”

  1. ```yaml
  2. workflows:
  3. version: 2
  4. build:
  5. jobs:
  6. - crystal/test
  7. orbs:
  8. crystal: manastech/crystal@1.0
  9. version: 2.1
  10. ```

Yeah! That was simple! With Orbs an abstraction layer is built so that the configuration file is more readable and intuitive.

In case we are wondering what the job crystal/test does, we always may see the source code.

Using nightly

Using nightly Crystal release is as easy as:

!!! example “.circleci/config.yml”

  1. ```yaml
  2. workflows:
  3. version: 2
  4. build:
  5. jobs:
  6. - crystal/test:
  7. name: test-on-nightly
  8. executor:
  9. name: crystal/default
  10. tag: nightly
  11. orbs:
  12. crystal: manastech/crystal@1.0
  13. version: 2.1
  14. ```

Using a specific Crystal release

!!! example “.circleci/config.yml”

  1. ```yaml
  2. workflows:
  3. version: 2
  4. build:
  5. jobs:
  6. - crystal/test:
  7. name: test-on-0.30
  8. executor:
  9. name: crystal/default
  10. tag: 0.30.0
  11. orbs:
  12. crystal: manastech/crystal@1.0
  13. version: 2.1
  14. ```

Installing shards packages

You need not worry about it since the crystal/test job runs the crystal/shard-install orb command.

Installing binary dependencies

Our application or maybe some shards may require libraries and packages. This binary dependencies may be installed using the Apt command.

Here is an example installing the libsqlite3 development package:

!!! example “.circleci/config.yml”

  1. ```yaml
  2. workflows:
  3. version: 2
  4. build:
  5. jobs:
  6. - crystal/test:
  7. pre-steps:
  8. - run: apt-get update && apt-get install -y libsqlite3-dev
  9. orbs:
  10. crystal: manastech/crystal@1.0
  11. version: 2.1
  12. ```

Using services

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

!!! example “.circleci/config.yml”

  1. ```yaml
  2. executors:
  3. crystal_mysql:
  4. docker:
  5. - image: 'crystallang/crystal:latest'
  6. environment:
  7. DATABASE_URL: 'mysql://root@localhost/db'
  8. - image: 'mysql:5.7'
  9. environment:
  10. MYSQL_DATABASE: db
  11. MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
  12. workflows:
  13. version: 2
  14. build:
  15. jobs:
  16. - crystal/test:
  17. executor: crystal_mysql
  18. pre-steps:
  19. - run:
  20. name: Waiting for service to start (check dockerize)
  21. command: sleep 1m
  22. - checkout
  23. - run:
  24. name: Install MySQL CLI; Import dummy data
  25. command: |
  26. apt-get update && apt-get install -y mysql-client
  27. mysql -h 127.0.0.1 -u root --password="" db < test-data/setup.sql
  28. orbs:
  29. crystal: manastech/crystal@1.0
  30. version: 2.1
  31. ```

!!! 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