LoginSignup
0
0

More than 3 years have passed since last update.

Railsアプリを作成してGitHubにSSHでPush

Last updated at Posted at 2020-06-02

VPSのセットアップしてcapistranoでデプロイする流れで、Railsアプリを作成する必要があったので簡単にまとめます。情報量の関係上、Rails5です。安全に行きたいので。

環境

Rails 5.2.2
Ruby 2.4.1
MySQL 5.7

You're on Railsを表示

$ mkdir sample_app
$ cd sample_app
$ touch Dockerfile docker-compose.yml Gemfile Gemfile.lock
Dockerfile
FROM ruby:2.4.1

RUN apt-get update -qq && \
  apt-get install -y build-essential \ 
  libpq-dev \        
  nodejs           

RUN mkdir /app_name 
ENV APP_ROOT /app_name 
WORKDIR $APP_ROOT

COPY ./Gemfile $APP_ROOT/Gemfile
COPY ./Gemfile.lock $APP_ROOT/Gemfile.lock

RUN bundle install
COPY . $APP_ROOT
docker-compose.yml
version: "3"
services:
  db:
    image: mysql:5.7
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: root
    ports:
      - "3306:3306"
  web:
    build: .
    command: /bin/sh -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
    volumes:
      - .:/app_name
    ports:
      - "3000:3000"
    links:
      - db

Gemfile
source 'https://rubygems.org'
gem 'rails', '5.2.2'
Gemfile.lock
#空
$ docker-compose run web rails new . --force --database=mysql --skip-bundle
$ docker-compose build --no-cache #--no-cacheオプションが無いと、bundle installが実行されないことがある
config/database.yml
default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password: password
  host: db

development:
  <<: *default
  database: app_name_development

test:
  <<: *default
  database: app_name_test

production:
  <<: *default
  database: app_name_production
  username: app_name
  password: <%= ENV['APP_NAME_DATABASE_PASSWORD'] %>
$ docker-compose up
$ docker-compose run web rails db:create

http://localhost:3000 にアクセスしてYay!You're on Rails!を確認

適当なページを作成

$ docker-compose run web rails g controller tests index
config/routes.rb
Rails.application.routes.draw do
  get 'tests/index'
  root to: 'tests#index'
end

http://localhost:3000 にアクセスして表示を確認

sshキーペアを作成しGitHubに登録

すでに登録済みの場合はとばしてください。

$ mkdir -p ~/.ssh/github
$ cd ~/.ssh/github
$ ssh-keygen -f id_rsa
$ vim ~/.ssh/config
~/.ssh/config
Host github.com #ここが「github」だと動かないので注意
  HostName github.com
  Port 22
  IdentityFile ~/.ssh/github/id_rsa
  User git
$ pbcopy < id_rsa.pub #公開鍵をクリップボードにコピー

その後以下の手順でGitHubに登録

  • GitHubにログイン
  • 右上のアイコン > Settings > SSH and GPG keys に移動
  • New SSH keyをクリック
  • クリップボードにコピーした公開鍵をペーストして保存

GitHubにリポジトリを作成

  • GitHubにログイン
  • 画面右上のアイコン > Your repositoriesをクリック
  • Newをクリック
  • リポジトリ名を入力し Create repository をクリック

Push

$ git add -A
$ git commit -m "First commit"
$ git remote add origin git@github.com:<githubのアカウント名>/<リポジトリ名>.git
$ git push -u origin master
0
0
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
0
0