Quick and Easy Integration of GitHub Action with Your Golang, PostgreSQL, and Redis Projects

A Simple Guide to Integrating GitHub Action with Golang, PostgreSQL, and Redis

ยท

3 min read

Quick and Easy Integration of GitHub Action with Your Golang, PostgreSQL, and Redis Projects

Prerequisites:

  1. Git & GitHub

  2. Basics of CI/CD (continuous integration and continuous delivery/deployment)

Let's first understand what GitHub Action is and the features of CI (Continuous Integration).

GitHub Actions is a continuous integration and continuous deployment (CI/CD) platform provided by GitHub. It allows you to automate workflows for your GitHub repositories. These workflows are defined in YAML files stored within your repository's .github/workflows directory.

Here's an example of a GitHub Actions workflow YAML file for Node.js:

name: CI

on: [push]

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Setup Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '14'

    - name: Install dependencies
      run: npm install

    - name: Build
      run: npm run build

    - name: Test
      run: npm test

Let's understand this example:

The name sets the workflow's name. The on field indicates the GitHub event that starts the workflow, here triggered by any push to the repository. jobs includes one or more jobs that run simultaneously. The build job, running on an ubuntu-latest virtual machine, consists of sequential steps. These steps include using actions/checkout@v2 to check out the repository, with each step named and potentially receiving inputs through with, and executing shell commands via run.

In this example, the 5th step will be the same for all GitHub Actions:

  1. Check out the repository.

  2. Sets up environment.

  3. Install dependencies.

  4. Build the project.

  5. Run tests.


Now you know how GitHub Actions function. To implement GitHub Actions with your Golang application, you can follow these steps:

  1. Create a GitHub repository for your Golang application if you haven't already.

  2. Set up your Golang application with the necessary configuration files like go.mod and main.go.

  3. Create a GitHub Actions workflow file. This file will define the steps that GitHub Actions will take when triggered. Create a .github/workflows directory in your repository and add a YAML file inside it. For example, you can name it go.yml.

  4. Define the workflow. Below is an example of a basic workflow file for building and testing a Golang application:

name: Go CI

on:
  push:
    branches: [main]

jobs:
  build:
    name: Build and Test
    runs-on: ubuntu-latest

    services:
      postgres:
        image: postgres:latest
        env:
          POSTGRES_DB: mydatabase
          POSTGRES_USER: myuser
          POSTGRES_PASSWORD: mypassword
        ports:
          - 5432:5432
        options: --health-cmd="pg_isready" --health-interval=10s --health-timeout=5s --health-retries=3

      redis:
        image: redis:latest
        ports:
          - 6379:6379
        options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Setup Go
      uses: actions/setup-go@v2
      with:
        go-version: '1.16' # Update with your Go version

    - name: Install dependencies
      run: go mod download

    - name: Build
      run: go build ./...

    - name: Test
      run: go test ./...

After the YAML file is ready, follow these steps:

  1. Commit and push the workflow file to your repository.

  2. Trigger the workflow by pushing a change to your repository or by creating a pull request, and GitHub Actions will automatically run the defined workflow.

  3. Monitor the workflow execution by checking the "Actions" tab of your GitHub repository to see the status.

Conclusion:

Understanding CI/CD is essential; only then will you discover the purpose of GitHub Actions. There are many cool features that I will cover in the next blog. Until then, keep practicing GitHub Actions - CI (Continuous Integration).

Thanks For Reading This Blog.

Did you find this article valuable?

Support Suraj Shetty by becoming a sponsor. Any amount is appreciated!

ย