LoginSignup
12
7

More than 3 years have passed since last update.

circleci コンテナの立ち上げ失敗&ビルドがキャンセルされる?

Last updated at Posted at 2020-07-09

ビルドすることに苦戦しすぎているので、備忘録メモ。まだ完璧に解決できていないが、一歩進んだので投稿

本題の前に一言

circleciのエラー文って不親切やし、めっちゃ難しい。dockerをちゃんと勉強しろってことなのか。
改めて現役エンジニアを尊敬する。

参考URL

登場ファイル

  1. .circleci/cofing.yml
  2. database.yml
  3. docker-comopose.yml

今回の抱えている問題

・MySQL、コンテナの立ち上げ失敗←今回で解決できていないが、前進した。
・コンテナの立ち上げに失敗しているからdb:createも失敗
・rubocopを実行できていない(db:createをできていないから?)
・/tmp/test-resultsがあるのに、Not Foundになってしまう←今回で解決できていない。

ファイルの中身(エラー発生時)

circleci/cofing.yml
version: 2.1
orbs:
  ruby: circleci/ruby@0.1.2

jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/ruby:2.6.6-stretch-node
        environment:
          - RAILS_ENV: 'test'
      - image: circleci/mysql:8.0
        name: "db"
        command: mysqld --default-authentication-plugin=mysql_native_password
        environment:
          - MYSQL_PASSWORD: "password"
          - MYSQL_ROOT_HOST: '%'

      # Specify service dependencies here if necessary
      # CircleCI maintains a library of pre-built images
      # documented at https://circleci.com/docs/2.0/circleci-images/
      # - image: circleci/postgres:9.4

    working_directory: ~/アプリ名

    steps:
      - checkout

      # Download and cache dependencies
      - restore_cache:
          keys:
            - v1-dependencies-{{ checksum "Gemfile.lock" }}
            # fallback to using the latest cache if no exact match is found
            - v1-dependencies-

      - run: bundle install --jobs=4 --retry=3 --path vendor/bundle
      - save_cache:
          paths:
            - ./vendor/bundle
          key: v1-dependencies-{{ checksum "Gemfile.lock" }}

      # Database setup
      - run: bundle exec rake db:create
      - run: bundle exec rake db:schema:load

# rubocop。
      - run:
          name: Rubocop
          command: bundle exec rubocop -a

# rspec
      # run tests!
      - run:
          name: run tests
          command: |
            mkdir /tmp/test-results
            TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \
              circleci tests split --split-by=timings)"


            bundle exec rspec \
              --format progress \
              --format RspecJunitFormatter \
              --out /tmp/test-results/rspec.xml \
              --format progress \
              $TEST_FILES

      # collect reports
      - store_test_results:
          path: /tmp/test-results
      - store_artifacts:
          path: /tmp/test-results
          destination: test-results
database.yml
default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: <%= ENV.fetch("MYSQL_USERNAME", "root") %>
  password: <%= ENV.fetch("MYSQL_PASSWORD", "password") %>
  host: <%= ENV.fetch("MYSQL_HOST", "db") %>

development:
  <<: *default
  database: アプリ名_development

test:
  <<: *default
  database: アプリ名_test

production:
  <<: *default
  database: <%= ENV['DB_DATABASE'] %>
  adapter: mysql2
  encoding: utf8mb4
  charset: utf8mb4
  collation: utf8mb4_general_ci
  host: <%= ENV['DB_HOST'] %>
  username: <%= ENV['DB_USERNAME'] %>
  password: <%= ENV['DB_PASSWORD'] %>


docker-comopose.yml
version: '3'
services:
  db:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: password
    ports:
      - '3306:3306'
    command: --default-authentication-plugin=mysql_native_password
    volumes:
      - mysql-data:/var/lib/mysql
  web:
    build: .
    command: bundle exec puma -C config/puma.rb
    environment:
      RAILS_ENV: development
    volumes:
      - .:/アプリ名
      - bundle:/usr/local/bundle
      - /app/vendor
      - /app/log
      - /app/.git
    ports:
      - "3000:3000"
    depends_on:
      - db
    tty: true
    stdin_open: true
  nginx:
    build:
      context: .
      dockerfile: ./nginx/Dockerfile
    ports:
      - '80:80'
    depends_on:
      - web
  chrome:
    image: selenium/standalone-chrome
    ports:
      - "4444:4444"
    shm_size: "2g"


volumes:
  mysql-data:
    driver: local
  bundle:
    driver: local

やったこと

開発でMySQLのコンテナの立ち上げに成功しているから、docker-comose.ymlに書いてあることを.circleci/cofing.ymlでも真似すればいけるんじゃないかと考えて、.circleci/cofing.ymlを以下のように修正

circleci/cofing.yml
version: 2.1
orbs:
  ruby: circleci/ruby@0.1.2

jobs:
  build:
    docker:
      # specify the version you desire here
      - image: circleci/ruby:2.6.6-stretch-node
        environment:
          - RAILS_ENV: 'test'
      - image: circleci/mysql:8.0
        name: "db"
        command: mysqld --default-authentication-plugin=mysql_native_password
        environment:
+         - MYSQL_ROOT_PASSWORD: password    追記
-         - MYSQL_PASSWORD: "password"    削除
-         - MYSQL_ROOT_HOST: '%'        削除


結果

コンテナの作成のところで「!」マークだったのが、「ー」マークに変わった!!(ただしエラーは解決できていないっぽい。。。)「!」と「ー」の違いは何なんやろ?
それでもrubocopは実行できるようになったので、前進はできた。
スクリーンショット 2020-07-09 8.34.04.png

追記(2020/07/12)

buildがキャンセルされる理由をcircleciに直接質問してみました。
以下が回答。

qiita.rb

"Build was canceled"と出ているのは、テストが終了し、
それに伴いMySQLのコンテナを止めるときに出力されるステータスです。

そして、"mbind: Operation not permitted"と出ていますが、
このオペレーションをDockerがセキュリティのためブロックしています。
普段の使い方だと特に支障はないと思います。

-」記号はキャンセルされた状態を示します。

どうやら問題ないみたい。

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