|
| 1 | +# Deploying with Kamal |
| 2 | + |
| 3 | +ServiceStack templates now support deployment using Kamal, a CLI tool from the BaseCamp team that simplifies containerized application deployments. Kamal wraps SSH and Docker to streamline self-hosting while maintaining GitHub Actions as the CI runner. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Kamal enables simple commands to deploy containerized applications to Linux hosts, handling: |
| 8 | +- Bootstrap configuration |
| 9 | +- Reverse proxy setup |
| 10 | +- SSL certificate provisioning |
| 11 | +- Zero-downtime deployments |
| 12 | +- Rolling back changes |
| 13 | + |
| 14 | +::: info |
| 15 | +Kamal is built by the BaseCamp team, developers of Hey email service and BaseCamp project management tool. For comprehensive documentation on all Kamal features, visit [https://kamal-deploy.org/](https://kamal-deploy.org/) |
| 16 | +::: |
| 17 | + |
| 18 | +## Getting Started |
| 19 | + |
| 20 | +### Prerequisites |
| 21 | +- A domain name |
| 22 | +- A Linux host (e.g., VPS from providers like Hetzner) |
| 23 | +- GitHub account for CI/CD |
| 24 | + |
| 25 | +### Initial Setup |
| 26 | + |
| 27 | +1. Create a new ServiceStack application: |
| 28 | +```bash |
| 29 | +x new blazor-vue MyApp |
| 30 | +``` |
| 31 | + |
| 32 | +2. Generate deployment SSH key: |
| 33 | +```bash |
| 34 | +ssh-keygen -t ed25519 -C "deploy@myapp" -f ./deploy-key |
| 35 | +``` |
| 36 | + |
| 37 | +3. Configure server access: |
| 38 | +```bash |
| 39 | +cat ~/my-deploy-key.pub | ssh <user>@<your-ip-address> "cat >> ~/.ssh/authorized_keys" |
| 40 | +``` |
| 41 | + |
| 42 | +### GitHub Actions |
| 43 | + |
| 44 | +The template includes a GitHub Actions workflow that is broken up into 3 steps that trigger on push to the `main` branch, and then on successful build and test, it will deploy the application to your server. |
| 45 | + |
| 46 | +Once you create your GitHub repository, add the `SSH_PRIVATE_KEY` secret to your repository settings with the contents of your private key file. |
| 47 | + |
| 48 | +```bash |
| 49 | +gh secret set PRIVATE_SSH_KEY < ~/my-deploy-key |
| 50 | +``` |
| 51 | + |
| 52 | +### Configuration |
| 53 | + |
| 54 | +Update `config/deploy.yml` with your deployment settings: |
| 55 | + |
| 56 | +```yaml |
| 57 | +service: myapp |
| 58 | +image: your-github-username/myapp |
| 59 | + |
| 60 | +servers: |
| 61 | + web: |
| 62 | + - <your-server-ip-address> |
| 63 | + |
| 64 | +proxy: |
| 65 | + ssl: true |
| 66 | + host: myapp.example.com |
| 67 | +``` |
| 68 | +
|
| 69 | +::: info |
| 70 | +The `image` value should match your GitHub repository path on ghcr.io. For example: `ghcr.io/username/repository` |
| 71 | +::: |
| 72 | + |
| 73 | +Once you make these changes, commit and push to your repository to trigger the GitHub Actions workflow. |
| 74 | + |
| 75 | +Kamal will deploy the required services including Docker and Kamal Proxy if you server doesn't already have them installed. |
| 76 | + |
| 77 | +The authentication between GitHub Container Registry (ghcr.io) and your server is handled by the GitHub Actions workflow, the `deploy.yml` and [Kamal Secrets](https://kamal-deploy.org/docs/configuration/environment-variables/#secrets). |
| 78 | + |
| 79 | +:::info |
| 80 | +You can still use the Kamal CLI locally, but if you want to directly push deployments with `kamal deploy`, you will need to locally populate `KAMAL_REGISTRY_USER` and `KAMAL_REGISTRY_PASSWORD` with your GitHub username and a GitHub Personal Access Token with `read:packages` scope. |
| 81 | +::: |
| 82 | + |
| 83 | +## Common Kamal Commands |
| 84 | + |
| 85 | +Kamal provides several useful commands for managing your deployment: |
| 86 | + |
| 87 | +```bash |
| 88 | +# View deployment details |
| 89 | +kamal details |
| 90 | +
|
| 91 | +# Check application logs |
| 92 | +kamal app logs |
| 93 | +
|
| 94 | +# Deploy new version |
| 95 | +kamal deploy |
| 96 | +
|
| 97 | +# Restart application |
| 98 | +kamal app boot |
| 99 | +``` |
| 100 | + |
| 101 | +::: info |
| 102 | +Kamal commands are context-aware and will use the configuration from your current application directory. This makes managing multiple applications across different servers a lot easier as more applications are added. |
| 103 | +::: |
| 104 | + |
| 105 | +## Additional Containers |
| 106 | + |
| 107 | +Kamal supports extensive configuration options including "accessories" for additional features like databases, caches, and more. See the [Kamal documentation](https://kamal-deploy.org/docs/configuration/accessories/) for more information. |
| 108 | + |
| 109 | +## Benefits of Using Kamal |
| 110 | + |
| 111 | +- Simplified deployment process |
| 112 | +- Automatic SSL certificate management |
| 113 | +- Built on common technology (SSH and Docker) |
| 114 | +- Flexible self-hosting solution |
| 115 | +- Various secret management options |
| 116 | +- Built-in rollback capabilities |
| 117 | +- Simple command-line interface |
| 118 | + |
| 119 | +::: info |
| 120 | +Kamal is particularly useful for applications that don't require complex scaling beyond a single machine, offering a cost-effective alternative to cloud hosting. |
| 121 | +::: |
0 commit comments