Sling Academy
Home/Golang/Creating CI/CD Pipelines for Go Projects

Creating CI/CD Pipelines for Go Projects

Last updated: November 27, 2024

Introduction

Continuous Integration (CI) and Continuous Deployment (CD) are integral parts of modern software development. They help automate testing and deployment, making releases faster and more reliable. In this article, we'll walk through setting up a CI/CD pipeline for a Go project using GitHub Actions.

Prerequisites

  • Basic understanding of Go programming language
  • A Go project hosted in a GitHub repository
  • Familiarity with GitHub Actions

Setting Up CI with GitHub Actions

GitHub Actions allow us to automate the testing phase of our CI pipeline. Let’s set it up for a Go project.

  1. Create a directory for your workflows in the root of your project:

    mkdir -p .github/workflows
  2. Create a file named ci.yml in the .github/workflows/ directory. This file will define our CI workflow.
  3. Edit the ci.yml file and add the following contents:
name: Go CI

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

jobs:
  build:
    runs-on: ubuntu-latest

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

    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: '^1.16'

    - name: Install dependencies
      run: go mod download

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

This YAML file configures GitHub Actions to build the Go project and run tests whenever code is pushed to or a pull request is made against the main branch.

Setting Up CD: Deploying Go Applications

Next, we need to automate deployments. For this, we'll use another GitHub workflow. The following is a simple example.

  1. In the same .github/workflows directory, create another file named cd.yml.
  2. Edit the cd.yml and add the following contents:
name: Deploy

on:
  push:
    tags:
    - 'v*.*.*'

jobs:
  deploy:
    runs-on: ubuntu-latest

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

    - name: Set up Go
      uses: actions/setup-go@v2
      with:
        go-version: '^1.16'

    - name: Build
      run: |
        go build -o myapp ./...

    - name: Deploy to server
      env:
        SERVER_IP: ${{ secrets.SERVER_IP }}
        USERNAME: ${{ secrets.USERNAME }}
        PASSWORD: ${{ secrets.PASSWORD }}
      run: |
        sshpass -p "$PASSWORD" scp myapp $USERNAME@$SERVER_IP:/path/to/app
        sshpass -p "$PASSWORD" ssh $USERNAME@$SERVER_IP 'systemctl restart myapp'

This CD pipeline gets triggered when a new tag matching the pattern v*.*.* is pushed. It automates the process of building the Go application and deploying it to a server.

Conclusion

Setting up CI/CD pipelines for Go projects using GitHub Actions can significantly enhance the efficiency and reliability of your software development process. It automates the critical steps of testing and deployment, allowing developers to focus more on coding and less on operations. By integrating these setups, teams can ensure that their applications remain robust, resilient, and quickly adaptable to changes.

Next Article: Monitoring Go Applications in Production (with Prometheus)

Previous Article: How to Build and Deploy a REST API in Go

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
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • 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