Deploying Go Application to DigitalOcean from MacOS: A Guide
Deploying a Go application to DigitalOcean from your local MacOS can be done using a combination of shell scripts and the go build command to compile your Go code. Here’s a step-by-step guide:
-
Prerequisites:
- A DigitalOcean account and an API token.
- DigitalOcean CLI (
doctl) installed on your MacOS: Install it using Homebrew by runningbrew install doctl. - SSH keys set up on your DigitalOcean account for secure access.
- Go installed on your MacOS.
-
Create a DigitalOcean Droplet:
First, you need to create a Droplet. You can do this through the DigitalOcean web interface, or using
doctl:doctl auth init # Authenticate doctl with your API token doctl compute droplet create my-go-app \ --region nyc3 \ --image ubuntu-20-04-x64 \ --size s-1vcpu-1gb \ --ssh-keys your_ssh_key_id \ --waitReplace
your_ssh_key_idwith your actual SSH key ID in DigitalOcean. -
Write a MacOS Shell Script for Deployment:
Create a shell script named
deploy.shto handle the deployment process.#!/bin/bash set -e # Variables DO_DROPLET_IP="your_droplet_ip" # Replace with the actual Droplet IP SSH_USER="root" GO_APP_NAME="my_go_app" LOCAL_DIR="/path/to/your/go/app" # Local directory containing your Go application REMOTE_DIR="/opt/$GO_APP_NAME" # Build the Go application echo "Building the Go application..." cd $LOCAL_DIR GOOS=linux GOARCH=amd64 go build -o $GO_APP_NAME # Copy files to the remote server echo "Copying files to the remote server..." scp $LOCAL_DIR/$GO_APP_NAME $SSH_USER@$DO_DROPLET_IP:/tmp/ # Remote commands to set up the application echo "Setting up the application on the remote server..." ssh $SSH_USER@$DO_DROPLET_IP << 'EOF' set -e GO_APP_NAME="my_go_app" REMOTE_DIR="/opt/$GO_APP_NAME" TMP_DIR="/tmp/$GO_APP_NAME" # Create the remote directory mkdir -p $REMOTE_DIR # Move the Go application to the remote directory mv /tmp/$GO_APP_NAME $REMOTE_DIR/ # Make the Go application executable chmod +x $REMOTE_DIR/$GO_APP_NAME # Set up systemd service (for existence of init.d) cat << SERVICE > /etc/systemd/system/$GO_APP_NAME.service [Unit] Description=My Go App After=network.target [Service] ExecStart=$REMOTE_DIR/$GO_APP_NAME Restart=always User=root [Install] WantedBy=multi-user.target SERVICE # Enable and start the service systemctl enable $GO_APP_NAME systemctl start $GO_APP_NAME echo "Deployment is complete!" EOF -
Make Your Script Executable:
chmod +x deploy.sh -
Run the Deployment Script:
Now, you can run your deployment script:
./deploy.sh
This script will:
- Compile the Go application for a Linux environment (
GOOS=linux GOARCH=amd64). - Copy the compiled binary to the DigitalOcean Droplet using
scp. - SSH into the Droplet and move the binary to the desired directory.
- Set up a systemd service to manage the Go application, enabling it to start on system boot and managing restart policies.
Make sure to replace placeholders (like your_droplet_ip, your_ssh_key_id, and /path/to/your/go/app) with your actual details for a smooth deployment process.