All guides
Intermediate-AdvancedSelf-pacedFree GuideSection 24/33

Déploiement sur Heroku

Learn to build and check a web application with development tools and AI assistants. Practise design, coding and validation.

The full lessons for this course are currently available in French. Use the section menu to navigate the French course.

💡

Free content, available without an account. Tools and models change: use the current model guide to check their versions. Models and sources

Section 13.5.5 : Déploiement sur Heroku

🎯 Objectif pédagogique

Déployer une application Rails sur Heroku, configurer les variables d'environnement, la base de données PostgreSQL, et mettre en place un pipeline CI/CD basique.


Heroku : la plateforme de déploiement Rails

Heroku est une PaaS (Platform as a Service) qui simplifie le déploiement. Un simple git push déploie votre application. C'est la plateforme historique de la communauté Rails.

Préparation du projet

# Gemfile modifications pour production
group :production do
  gem 'pg'            # PostgreSQL en production
end

group :development, :test do
  gem 'sqlite3'       # SQLite en dev
end
# Procfile (à la racine du projet)
web: bundle exec puma -C config/puma.rb
worker: bundle exec sidekiq
release: bundle exec rails db:migrate

Déploiement initial

# 1. Installer Heroku CLI
# 2. Se connecter
heroku login

# 3. Créer l application
heroku create mon-app-rails
# => https://mon-app-rails.herokuapp.com

# 4. Ajouter PostgreSQL
heroku addons:create heroku-postgresql:mini

# 5. Configurer les variables d'environnement
heroku config:set RAILS_MASTER_KEY=$(cat config/master.key)
heroku config:set RAILS_ENV=production

# 6. Déployer
git push heroku main

# 7. Exécuter les migrations
heroku run rails db:migrate

# 8. Seed si nécessaire
heroku run rails db:seed

Variables d'environnement

# Définir des variables
heroku config:set SECRET_KEY_BASE=abc123
heroku config:set SMTP_HOST=smtp.sendgrid.net
heroku config:set SMTP_USERNAME=apikey

# Voir les variables
heroku config

# Utiliser dans Rails
# config/environments/production.rb
config.action_mailer.smtp_settings = {
  address: ENV['SMTP_HOST'],
  user_name: ENV['SMTP_USERNAME'],
  password: ENV['SMTP_PASSWORD']
}

Configuration PostgreSQL en production

# config/database.yml
production:
  adapter: postgresql
  encoding: unicode
  url: <%= ENV['DATABASE_URL'] %>
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

Commandes Heroku utiles

# Logs en temps réel
heroku logs --tail

# Console Rails en production
heroku run rails console

# Bash dans le dyno
heroku run bash

# Redémarrer
heroku restart

# Échelle
heroku ps:scale web=2 worker=1

# Maintenance mode
heroku maintenance:on
heroku maintenance:off

# Backup de base de données
heroku pg:backups:capture
heroku pg:backups:download

Alternatives à Heroku

Plateformes de déploiement Rails

PlateformePrixDifficultéAvantages
HerokuÀ partir de 5$/moisFacileGit push deploy, addons
RenderGratuit (limité)FacileAlternative Heroku moderne
Fly.ioGratuit (limité)MoyenEdge computing, rapide
RailwayGratuit (limité)FacileInterface élégante
AWS EC2VariableDifficileContrôle total, scalable

← Scroll horizontally →

Quick Check

Pourquoi ne faut-il JAMAIS utiliser SQLite en production ?

Learn AI, from prompts to agents

15 free guidesQuizzes, diagrams and workshopsNo account needed

Your progress

Progress is self-reported and stored in your browser. The final completion record is not a qualification or an independent assessment.

View progress and completion record

Learn AI, from prompts to agents

15 free guidesQuizzes, diagrams and workshopsNo account needed

One practical AI workflow in your inbox

Methods, resources and new exercises. Free, unsubscribe at any time.