Automated Golang Deployment to Server with GitHub Actions and Docker
Why Automate Deployment?
Manual deployment is error-prone and time-consuming. Every time you SSH into a server, pull code, rebuild, and restart services, you risk introducing human errors. Automated deployment pipelines eliminate this risk and let you ship faster with confidence.
The Pipeline
Our CI/CD pipeline consists of three stages:
- Build & Test — Run unit tests and build the Go binary
- Docker Image — Build and push a Docker image to a container registry
- Deploy — SSH into the production server and pull the latest image
GitHub Actions Workflow
The workflow triggers on every push to the main branch. It uses a multi-stage Docker build to keep the final image small (typically under 20MB for a Go binary). The deployment step uses SSH action to connect to the production server and run docker compose pull && docker compose up -d.
Docker Multi-Stage Build
We use a two-stage Dockerfile: the first stage compiles the Go binary with all dependencies, and the second stage copies only the binary into a minimal alpine image. This reduces the attack surface and keeps deployment fast.
Key Takeaways
- Always pin your GitHub Actions versions for reproducible builds
- Use Docker layer caching to speed up builds
- Store secrets (SSH keys, registry credentials) in GitHub Secrets
- Add health checks to your Docker containers for automatic recovery
Related Posts
Real-time Retail Data Synchronization
How I designed and built a real-time transaction and stock synchronization system across multiple retail outlets using Apache Kafka, Golang microservices, and Docker.
Building Email Verification for User Registration using Golang
Implementing a secure email verification flow for user registration in Go, covering token generation, SMTP integration, and database design.