人生就是不斷學習,調整與更新持續前進。

Rails Kama 開發部署工作流 v2

延續上一篇:

Step3 rails + Kamal 設定文件

 

設定檔案位置: config/deploy.yml

 

kamal 常用指令:

kamal setup

kamal deploy

kamal app stop

kamal app remove

kamal app exec 'env | grep DATABASE'

kamal envify

kamal app exec 'cat /rails/.env' 2>/dev/null || echo "No .env file in container"
kamal app exec -i bash

# 先啟動資料庫(如果還沒啟動)
kamal accessory boot db

# push新的環境變數
kamal env push

# 重新部署應用
kamal deploy

kamal app exec 'env | grep DATABASE_URL'

 

redis-server + sidekiq設定:

gem "redis"
gem "sidekiq"

# 啟動 Redis
bin/kamal accessory boot redis

# 啟動 Sidekiq
bin/kamal accessory boot sidekiq

bin/kamal accessory boot all

docker ps

bin/kamal accessory logs sidekiq -f

 

deploy.yml 重點資訊名詞整理: for rails 8.x~ deploy.yml設置架構

# Name of your application. Used to uniquely configure containers.
service: spiritual_compass

# Name of the container image.
image: docker_hub_account/docker_project_name

# Deploy to these servers.
servers:
  web: 
    # For local Docker deployment
    - ip-addr

# Enable SSL auto certification via Let's Encrypt and allow for multiple apps on a single web server.
# Remove this section when using multiple web servers and ensure you terminate SSL at your load balancer.
#
# Note: If using Cloudflare, set encryption mode in SSL/TLS setting to "Full" to enable CF-to-app encryption.
proxy:
  ssl: false
  host: your_dns

# Credentials for your image host.
registry:
  # Use local registry (no authentication required)
  server: docker.io
  username: docker_account
  password:
    - DOCKER_PASSWORD

# Inject ENV variables into containers (secrets come from .kamal/secrets).
env:
  secret:
    - RAILS_MASTER_KEY
    - POSTGRES_PASSWORD
  clear:
    # Run the Solid Queue Supervisor inside the web server's Puma process to do jobs.
    # When you start using multiple servers, you should split out job processing to a dedicated machine.
    RAILS_ENV: production
    # SOLID_QUEUE_IN_PUMA: true
    # Ensure Rails resolves the DB host via Kamal's internal network
    REDIS_URL: redis://your-project-redis:6379/0
    DB_HOST: your_project-db
    DB_NAME: your_project_production
    DB_USER: postgres

    # Set number of processes dedicated to Solid Queue (default: 1)
    # JOB_CONCURRENCY: 3

    # Set number of cores available to the application on each server (default: 1).
    # WEB_CONCURRENCY: 2

    # Match this to any external database server to configure Active Record correctly
    # Use spiritual_compass-db for a db accessory server on same machine via local kamal docker network.
    # DB_HOST: 192.168.0.2

    # Log everything from Rails
    # RAILS_LOG_LEVEL: debug

# Aliases are triggered with "bin/kamal <alias>". You can overwrite arguments on invocation:
# "bin/kamal logs -r job" will tail logs from the first server in the job section.
aliases:
  console: app exec --interactive --reuse "bin/rails console"
  shell: app exec --interactive --reuse "bash"
  logs: app logs -f
  dbc: app exec --interactive --reuse "bin/rails dbconsole"


# Use a persistent storage volume for sqlite database files and local Active Storage files.
# Recommended to change this to a mounted volume path that is backed up off server.
volumes:
  - "your_project_storage:/rails/storage"


# Bridge fingerprinted assets, like JS and CSS, between versions to avoid
# hitting 404 on in-flight requests. Combines all files from new and old
# version inside the asset_path.
asset_path: /rails/public/assets

# Configure the image builder.
builder:
  arch: amd64

  # # Build image via remote server (useful for faster amd64 builds on arm64 computers)
  # remote: ssh://docker@docker-builder-server
  #
  # # Pass arguments and secrets to the Docker build process
  # args:
  #   RUBY_VERSION: ruby-3.3.0
  # secrets:
  #   - GITHUB_TOKEN
  #   - RAILS_MASTER_KEY

# Use a different ssh user than root
ssh:
  user: server_user_name

# Use accessory services (secrets come from .kamal/secrets).
accessories:
  redis:
    image: redis:7-alpine
    host: ip-addr
    port: "127.0.0.1:6379:6379"
    directories:
      - data:/data
    cmd: redis-server --appendonly yes
  
  db:
    image: postgres:16
    host: ip-addr
    # Only listen on loopback of the host; the app connects over the internal Docker network
    port: "127.0.0.1:5432:5432"
    env:
      clear:
        POSTGRES_DB: your_project_production
        POSTGRES_USER: postgres
      secret:
        - POSTGRES_PASSWORD
        - DATABASE_URL
    directories:
      - data:/var/lib/postgresql/data
  
  sidekiq:
    image: docker_hub_account/docker_project_name:latest
    host: ip-addr
    cmd: bundle exec sidekiq
    env:
      clear:
        RAILS_ENV: production
        REDIS_URL: redis://your-project-redis:6379/0
        DB_HOST: you-project-db
        DB_NAME: your_project_production
        DB_USER: postgres
      secret:
        - RAILS_MASTER_KEY
        - POSTGRES_PASSWORD

 
.env & secrets 環境變數要設定謹慎、設置好.

# 從索引移除(保留本機檔案)
git rm --cached .kamal/secrets

# 確認 .gitignore 有這一行(檔案在 repo 根目錄)
# /.kamal/secrets

git add .gitignore
git commit -m "Stop tracking .kamal/secrets and ignore it"
git push