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.