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.

Multi-Environment Deployment

Deploy to different environments (development, staging, production) with environment-specific configurations.

Container Integration

Seamless integration with Docker and Kubernetes for containerized deployments.

Deployment Hooks

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.

Basic Deployment Configuration
Essential deployment settings
# 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 configuration
  • environments - Environment-specific configurations
  • hooks - Commands to run before and after deployment

Deployment Providers

Gophel supports multiple deployment providers to fit your infrastructure needs.

Docker Deployment
Deploy your application as a Docker container
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

  1. Gophel builds your Go application
  2. Builds a Docker image using the specified Dockerfile
  3. Tags the image with the configured registry, repository, and tag
  4. Pushes the image to the Docker registry
  5. 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.

Environment Configuration
Customize deployments for different environments
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.

Database Migration Configuration
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 migrations
  • gophel db migrate down <steps> - Rollback a specific number of migrations
  • gophel db status - Show the current migration status
  • gophel 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: