LoginSignup
3
5

More than 5 years have passed since last update.

CircleCIでServerspecやawspecを定期実行する

Last updated at Posted at 2018-01-05

CircleCIではv2からWorkflowsの機能を使って、定期実行を実現することができます。

具体的なコードは以下のような .circleci/config.yml になります。

AWS環境やサーバ構成のテストとPHPアプリケーションのコードが同じリポジトリあるイメージです。

AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEY やサーバへの接続のためのSSH秘密鍵はCircleCI側に設定します。

version: 2
jobs:
  phptest:
    docker:
      - image: someone/php:7
      - image: mysql:5.6
        environment:
            MYSQL_ROOT_PASSWORD: ******
            MYSQL_USER: testuser
            MYSQL_PASSWORD: ******
            MYSQL_DATABASE: database_test
    steps:
      - checkout
      - run:
          name: composer install
          command: composer install
      - run:
          name: Test
          command: ./vendor/bin/phpunit
  infratest:
      docker:
      - image: circleci/ruby:2.4
    environment:
      - AWS_REGION: ap-northeast-1
    steps:
      - checkout
      - run:
          name: Pre
          command: sudo apt-get install libpcap-dev
      - run:
          name: bundle install
          command: |
              cd server/spec/
              bundle install
      - run:
          name: Test
          command: |
            cd server/spec/
            bundle exec rake spec
workflows:
  version: 2
  normal_workflow:
    jobs:
      - phptest
  scheduled_workflow:
    triggers:
      - schedule:
          cron: "0 1 * * *"
          filters:
            branches:
              only:
                - master
    jobs:
      - infratest

jobs:phptestinfratest をそれぞれ定義しています
workflows: で通常時( normal_workflow )と定期実行( scheduled_workflow )の定義をしています。

上記の設定では

  • normal_workflow では phptest が実行
  • scheduled_workflow では infratest が定期実行 ( 毎日1:00 UTC に masterブランチのコードで実行 )

されることになります。

これで一度書いたServerspecやawspecのコードで外形監視どころか症状監視までできるようになります。

最高ですね!

3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5