AWS

rails5.0のRSpecをAWS CodeBuildで実行するためにnode, MySQL5.7, yarnをインストールしたときのメモ

buildspec.ymlファイルに以下を記述

buildspec.yml
version: 0.2

env:
  variables:
    RAILS_ENV: "test"
    DB_ROOT_PASSWORD: "password"
    DB_HOST: "localhost"
    DB_PASSWORD: "password"
    DB_SOCKET: "/var/run/mysqld/mysqld.sock"

phases:
  install:
    commands:
      - rm -rf db/seeds/test
      - cp -rf db/seeds/development db/seeds/test
      - gem install bundler
      - bundle install
      - curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
      - sudo apt-get install -y nodejs
      - apt-get install -y gnupg procps
      - export DEBIAN_FRONTEND=noninteractive
      - echo "mysql-server mysql-server/root_password password ${DB_ROOT_PASSWORD}" |debconf-set-selections
      - echo "mysql-server mysql-server/root_password_again password ${DB_ROOT_PASSWORD}" |debconf-set-selections
      - apt-key adv --keyserver ha.pool.sks-keyservers.net --recv-keys 5072E1F5
      - echo "deb http://repo.mysql.com/apt/ubuntu/ trusty mysql-5.7" | tee /etc/apt/sources.list.d/mysql.list
      - sudo apt-get update -y
      - sudo apt-get install -y mysql-server
      - service mysql start
      - mysql --version
      - mysql -hlocalhost -uroot -ppassword -P3306 -e "show databases;"
      - mysql -hlocalhost -uroot -ppassword -P3306 -e "show variables like '%sock%';"
      - mysql -hlocalhost -uroot -ppassword -P3306 -e "GRANT ALL PRIVILEGES on *.* to 'root'@'localhost' IDENTIFIED BY '${DB_ROOT_PASSWORD}'; FLUSH PRIVILEGES;"
      - curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
      - echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
      - sudo apt-get update && sudo apt-get install yarn
      - yarn install
  pre_build:
    commands:
      - bundle exec rails db:setup
  build:
    commands:
      - bundle exec rspec
  post_build:
    commands:
      - echo 'Test completed!'

artifacts:
  files:
    - '**/*'

参考URL
- https://dev.mysql.com/doc/mysql-apt-repo-quick-guide/en/#repo-qg-apt-repo-manual-setup
- http://inokara.hateblo.jp/entry/2017/09/12/080818