【Rails7】将Rails应用程序Docker化 + 使用GitHub Actions设置CI

首先

我为只使用Rails创建应用程序并不知道如何将其挂载到Docker上,以及如何使用GitHub Actions进行CI设置的人提供了一个使用实操方式概括流程的指南。

Rails应用开发环境

    • Ruby 3.2.2

 

    • Ruby on Rails 7.0.8

 

    • PostgreSQL

 

    esbuild(バンドラー)

创建一个Rails应用

rails new mount_docker -j esbuild -d postgresql
cd mount_docker

Docker的配置

在当前工作目录下创建一个Dockerfile。

touch Dockerfile
FROM ruby:3.2.2

RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /app
WORKDIR /app

COPY Gemfile /app/Gemfile
COPY Gemfile.lock /app/Gemfile.lock

RUN bundle install
COPY . /app

COPY entrypoint.sh /usr/bin/

RUN chmod +x /usr/bin/entrypoint.sh

ENTRYPOINT ["entrypoint.sh"]

EXPOSE 3000

CMD ["rails", "server", "-b", "0.0.0.0"]

为了在服务器启动时采取预防措施,同样将entrypoint.sh创建在工作目录的根目录下。

touch entrypoint.sh
#!/bin/bash
set -e

# Remove a potentially pre-existing server.pid for Rails.
rm -f /myapp/tmp/pids/server.pid

# Then exec the container's main process (what's set as CMD in the Dockerfile).
exec "$@"

接下来创建docker-compose.yml文件。

touch docker-compose.yml
version: '3'
services:
  db:
    image: postgres
    volumes:
      - postgresql-data:/var/lib/postgresql/data
    environment:
      POSTGRES_USER: ${DATABASE_USERNAME}
      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
    ports:
      - '5433:5433'
  web:
    build: .

    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 8000 -b '0.0.0.0'"
    volumes:
      - .:/app
    ports:
      - '8000:8000'

    depends_on:
      - db
volumes:
  postgresql-data:
    driver: local

隐藏环境变量及数据库配置文件的设置

group :development, :test do
  gem 'dotenv-rails'
end
touch .env
DATABASE_USERNAME=xxxxxxxx
DATABASE_PASSWORD=xxxxxxxx
default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: <%= ENV['DATABASE_USERNAME'] %>
  password: <%= ENV['DATABASE_PASSWORD'] %>
  pool: 5

development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test
  
# 本番の設定は適宜変更してください。

启动Docker并启动服务器

docker-compose build
docker-compose up -d
docker-compose exec web bash
rails db:create
localhost:8000でブラウザ表示を確認
スクリーンショット 2023-12-08 1.10.19.png

使用GitHubActions配置Rspec和Rubocop的CI设置。

我們將在工作目錄的根目錄下創建用於CI的文件。

mkdir -p .github/workflows && touch .github/workflows/ci.yml
name: Continuous Integration

on:
  push:

jobs:
  rspec:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: .
    services:
      postgres:
        image: postgres:13
        ports:
          - 5432:5432
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: test

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.2.2
          bundler-cache: true

      - name: Cache node modules
        uses: actions/cache@v3
        with:
          path: node_modules
          key: ${{ runner.os }}-node-${{ hashFiles('**/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-node-

      - name: Bundler and gem install
        run: |
          gem install bundler
          bundle install

      - name: Database create and migrate
        run: |
          cp config/database.yml.ci config/database.yml
          bundle exec rails db:create RAILS_ENV=test
          bundle exec rails db:migrate RAILS_ENV=test

      - name: Run rspec
        run: bundle exec rspec

  rubocop:
    runs-on: ubuntu-latest
    defaults:
      run:
        working-directory: .
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: 3.2.2
          bundler-cache: true

      - name: Bundler and gem install
        run: |
          gem install bundler
          bundle install

      - name: Run rubocop
        run: bundle exec rubocop

我会创建一个适用于CI的yml文件。

touch config/database.yml.ci
test:
  adapter: postgresql
  encoding: unicode
  database: test
  username: postgres
  password: postgres
  host: localhost
  port: 5432

Gem的安装

group :development, :test do
  gem 'rspec-rails'
  gem 'rubocop'
end
bundle install

创建一个用于RSpec的文件。

rails g rspec:install

为了确认一下,我们会检查一下命令。

root@dd6579770655:/app# rspec
No examples found.


Finished in 0.0006 seconds (files took 0.13951 seconds to load)
0 examples, 0 failures

Rubocop,为了通过测试,我将排除一些情况。请根据需要进行相应修改。

touch .rubocop.yml
Style/Documentation:
  Enabled: false

Metrics/CyclomaticComplexity:
  Enabled: false

Metrics/MethodLength:
  Enabled: false

Metrics/PerceivedComplexity:
  Enabled: false

Layout/LineLength:
  Max: 200

Metrics/BlockLength:
  Enabled: false

我要执行 Rubocop。

rubocop -A

Inspecting 33 files
.................................

33 files inspected, no offenses detected

我们将重新构建。

docker-compose build

在Github上创建一个仓库并推送(push),就会触发CI自动执行。

您可以在”Actions”选项卡中确认CI已通过,就像图片中显示的那样。
如果CI未通过,请进行相应的修正。

スクリーンショット 2023-12-08 2.45.59.png

总结

我们一起利用Docker和GitHub Actions来高效地进行开发吧!

广告
将在 10 秒后关闭
bannerAds