Deployment
Streamline the process of deploying your Go applications to various environments.
Overview
Gophel's deployment features simplify the process of deploying your Go applications to different environments. With support for multiple deployment targets, environment-specific configurations, and deployment hooks, Gophel makes it easy to manage your application lifecycle from development to production.
Deploy to different environments (development, staging, production) with environment-specific configurations.
Seamless integration with Docker and Kubernetes for containerized deployments.
Run custom scripts before and after deployment to automate tasks like database migrations or notifications.
Deployment Configuration
Configure your deployment settings in the deploy
section of your gophel.yaml
file.
# Deployment configuration
deploy:
# Deployment provider
provider: "docker"
# Docker configuration
docker:
registry: "my-registry.com"
repository: "my-go-app"
tag: "latest"
dockerfile: "./Dockerfile"
build_args:
GO_VERSION: "1.18"
# Environment-specific configurations
environments:
production:
env:
APP_ENV: "production"
LOG_LEVEL: "warn"
port: 80
replicas: 3
staging:
env:
APP_ENV: "staging"
LOG_LEVEL: "info"
port: 8080
replicas: 1
# Hooks for deployment process
hooks:
pre_deploy:
- "go test ./..."
post_deploy:
- "curl -X POST https://hooks.slack.com/services/XXX/YYY/ZZZ -d '{"text":"Deployed successfully!"}'"
Key Fields
provider
- The deployment provider (docker, kubernetes, etc.)docker
- Docker-specific configurationenvironments
- Environment-specific configurationshooks
- Commands to run before and after deployment
Deployment Providers
Gophel supports multiple deployment providers to fit your infrastructure needs.
deploy:
provider: "docker"
docker:
# Docker registry
registry: "my-registry.com"
# Repository name
repository: "my-go-app"
# Image tag
tag: "latest"
# Path to Dockerfile
dockerfile: "./Dockerfile"
# Build arguments
build_args:
GO_VERSION: "1.18"
APP_ENV: "production"
# Container runtime options
run_options:
ports:
- "8080:8080"
volumes:
- "./data:/app/data"
env_file:
- ".env.production"
restart: "always"
network: "app-network"
Deployment Process
- Gophel builds your Go application
- Builds a Docker image using the specified Dockerfile
- Tags the image with the configured registry, repository, and tag
- Pushes the image to the Docker registry
- Runs the container with the specified options
Note: Make sure you have Docker installed and are logged in to your Docker registry before deploying.
Environment-Specific Deployments
Configure different settings for each deployment environment using the environments
section.
deploy:
# Common configuration
provider: "docker"
docker:
registry: "my-registry.com"
repository: "my-go-app"
# Environment-specific configurations
environments:
# Production environment
production:
# Environment variables
env:
APP_ENV: "production"
LOG_LEVEL: "warn"
DB_HOST: "prod-db.example.com"
# Docker-specific overrides
docker:
tag: "stable"
run_options:
ports:
- "80:8080"
restart: "always"
# Deployment hooks
hooks:
pre_deploy:
- "go test ./..."
post_deploy:
- "./scripts/notify-team.sh 'Production deployment successful'"
# Staging environment
staging:
env:
APP_ENV: "staging"
LOG_LEVEL: "info"
DB_HOST: "staging-db.example.com"
docker:
tag: "latest"
run_options:
ports:
- "8080:8080"
restart: "unless-stopped"
hooks:
post_deploy:
- "./scripts/notify-team.sh 'Staging deployment successful'"
# Development environment
development:
env:
APP_ENV: "development"
LOG_LEVEL: "debug"
DB_HOST: "localhost"
docker:
tag: "dev"
run_options:
ports:
- "8080:8080"
volumes:
- "./data:/app/data"
- "./logs:/app/logs"
Deploying to a Specific Environment
Use the --env
flag to specify the target environment:
gophel deploy --env production
Database Migrations
Gophel can handle database migrations as part of your deployment process.
deploy:
# Database migration configuration
database:
# Migration tool
tool: "migrate" # Options: migrate, goose, sql-migrate, etc.
# Migration directory
dir: "./migrations"
# Database connection
connection:
driver: "postgres"
dsn: "{{ DB_DSN }}" # Use environment variable
# Migration options
options:
# Run migrations before deployment
auto_migrate: true
# Verify migrations after running
verify: true
# Number of migrations to apply (0 for all)
steps: 0
# Rollback on deployment failure
rollback_on_failure: true
# Add migration steps to deployment hooks
hooks:
pre_deploy:
- "gophel db migrate up"
post_deploy:
- "gophel db status"
on_failure:
- "gophel db migrate down 1" # Rollback last migration on failure
Database Commands
gophel db migrate up
- Apply all pending migrationsgophel db migrate down <steps>
- Rollback a specific number of migrationsgophel db status
- Show the current migration statusgophel db create <name>
- Create a new migration file
Deployment Best Practices
Follow these guidelines for successful deployments:
- Use environment variables for sensitive information (API keys, passwords)
- Include comprehensive tests in your pre-deployment hooks
- Implement a rollback strategy for failed deployments
- Use semantic versioning for your application and Docker tags
- Set up monitoring and alerting for your deployed applications
- Document your deployment process and configuration
- Automate deployments with CI/CD pipelines
Next Steps
Now that you understand Gophel's deployment features, you might want to explore: