Sling Academy
Home/Golang/Auto deploy Go apps with CI/ CD and GitHub Actions

Auto deploy Go apps with CI/ CD and GitHub Actions

Last updated: November 28, 2024

Continuous Integration and Continuous Deployment (CI/CD) are essential practices in modern software development. They enable developers to automate the process of integrating code, running tests, and, in this article, deploying Go applications via GitHub Actions.

Prerequisites

  • A GitHub account
  • An existing Go application repository on GitHub
  • Basic understanding of YAML syntax

Step 1: Setting Up a Go Project

Ensure you have a Go project on your local machine. If you don't, here is a quick step to create one:

$ mkdir my-go-app
$ cd my-go-app
$ go mod init my-go-app
$ touch main.go

Edit main.go to include a simple Go application:

package main

import "fmt"

func main() {
    fmt.Println("Hello, Go world!")
}

Commit and push this code to your GitHub repository:

$ git add .
$ git commit -m "Initial commit with Go application"
$ git push origin main

Step 2: Creating a GitHub Action Workflow

Create a directory in your project for GitHub Actions:

$ mkdir -p .github/workflows

Create a new file called ci-cd.yml in the .github/workflows directory:

name: Go CI/CD

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - name: Check out repository code
      uses: actions/checkout@v2

    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: 1.16
    
    - name: Cache Go modules
      uses: actions/cache@v2
      with:
        path: ~/go/pkg/mod
        key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
        restore-keys: |
          ${{ runner.os }}-go-
    
    - name: Build
      run: go build -v ./...

    - name: Run unit tests
      run: go test -v ./...

Step 3: Adding Deployment Steps

To auto deploy your Go app, you'll need additional steps tailored to your deployment environment. For illustration, let's assume we're deploying to a server via SSH:

    - name: Deploy to server
      uses: appleboy/[email protected]
      with:
        host: ${{ secrets.SERVER_HOST }}
        username: ${{ secrets.SERVER_USER }}
        key: ${{ secrets.SERVER_SSH_KEY }}
        script: |
          cd /path/to/deployed/app
          git pull origin main
          go build ./...
          ./my-go-app

This assumes that deployment credentials are stored as secrets in your GitHub repository. Navigate to the repository's settings and add these secrets under "Secrets and variables" > "Actions" for secure storage.

Conclusion

By following these steps, you've automated building, testing, and deploying a Go application via GitHub Actions. While this example uses an SSH-based deployment, the deployment step can be adjusted for other environments such as AWS, GCP, or DigitalOcean.

Previous Article: Caching data with Redis in Go (Golang)

Series: Development and Debugging in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant
  • Fixing Go error: syntax error: unexpected X, expecting Y