codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐹

Go (Golang)

53 / 72 topics
51Docker for Go Applications52Kubernetes with Go53CI/CD Pipelines for Go Projects54Monitoring and Logging in Go Applications
Tutorials/Go (Golang)/CI/CD Pipelines for Go Projects
🐹Go (Golang)

CI/CD Pipelines for Go Projects

Updated 2026-04-20
3 min read

Introduction

Continuous Integration (CI) and Continuous Deployment (CD) are essential practices for modern software development. They help teams deliver code faster, with higher quality, and more reliably. In this tutorial, we will explore how to set up CI/CD pipelines for Go projects using popular tools like GitHub Actions, GitLab CI, and Jenkins.

Prerequisites

Before you start, ensure you have the following:

  • A Go project hosted on a version control system (e.g., GitHub, GitLab).
  • Basic knowledge of Go programming.
  • Access to a cloud provider or local server for deployment.

Setting Up CI/CD with GitHub Actions

GitHub Actions is a powerful automation tool that integrates seamlessly with GitHub repositories. It allows you to automate your build, test, and deployment workflows directly from your repository.

Step 1: Create a Workflow File

Create a new directory named .github/workflows in the root of your Go project if it doesn't already exist. Inside this directory, create a file named go.yml.

name: Go CI/CD Pipeline

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

jobs:
  build:
    runs-on: ubuntu-latest

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

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

    - name: Install dependencies
      run: |
        go get -t ./...

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

    - name: Build binary
      run: |
        CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o myapp .

  deploy:
    needs: build
    runs-on: ubuntu-latest

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

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

    - name: Deploy to server
      run: |
        # Add your deployment script here
        ssh user@your-server "docker stop myapp || true && docker rm myapp || true"
        scp myapp user@your-server:/path/to/deploy/
        ssh user@your-server "cd /path/to/deploy/ && docker build -t myapp . && docker run -d --name myapp -p 8080:8080 myapp"

Step 2: Commit and Push

Commit the go.yml file to your repository and push it to GitHub. This will trigger the CI/CD pipeline.

git add .github/workflows/go.yml
git commit -m "Add GitHub Actions CI/CD pipeline"
git push origin main

Step 3: Monitor Workflow

Go to your GitHub repository, navigate to the "Actions" tab, and monitor the workflow execution. You should see the build and deployment steps running.

Setting Up CI/CD with GitLab CI

GitLab CI is another powerful tool for automating your development workflows. It integrates seamlessly with GitLab repositories.

Step 1: Create a .gitlab-ci.yml File

Create a file named .gitlab-ci.yml in the root of your Go project.

image: golang:1.17

stages:
  - build
  - test
  - deploy

variables:
  GOOS: linux
  GOARCH: amd64

build_job:
  stage: build
  script:
    - go get -t ./...
    - CGO_ENABLED=0 GOOS=$GOOS GOARCH=$GOARCH go build -o myapp .

test_job:
  stage: test
  script:
    - go test ./...

deploy_job:
  stage: deploy
  only:
    - main
  script:
    - ssh user@your-server "docker stop myapp || true && docker rm myapp || true"
    - scp myapp user@your-server:/path/to/deploy/
    - ssh user@your-server "cd /path/to/deploy/ && docker build -t myapp . && docker run -d --name myapp -p 8080:8080 myapp"

Step 2: Commit and Push

Commit the .gitlab-ci.yml file to your repository and push it to GitLab. This will trigger the CI/CD pipeline.

git add .gitlab-ci.yml
git commit -m "Add GitLab CI/CD pipeline"
git push origin main

Step 3: Monitor Pipeline

Go to your GitLab project, navigate to the "CI/CD" section, and monitor the pipeline execution. You should see the build, test, and deployment stages running.

Setting Up CI/CD with Jenkins

Jenkins is a widely used open-source automation server that can be configured for various CI/CD workflows.

Step 1: Install Jenkins Plugins

Install the following plugins in Jenkins:

  • Go Plugin
  • SSH Pipeline Steps Plugin
  • Docker Pipeline Plugin

Step 2: Create a Jenkinsfile

Create a file named Jenkinsfile in the root of your Go project.

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                script {
                    sh 'go get -t ./...'
                    sh 'CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o myapp .'
                }
            }
        }

        stage('Test') {
            steps {
                script {
                    sh 'go test ./...'
                }
            }
        }

        stage('Deploy') {
            when {
                branch 'main'
            }
            steps {
                script {
                    sshagent(['your-ssh-key-id']) {
                        sh 'ssh user@your-server "docker stop myapp || true && docker rm myapp || true"'
                        sh 'scp myapp user@your-server:/path/to/deploy/'
                        sh 'ssh user@your-server "cd /path/to/deploy/ && docker build -t myapp . && docker run -d --name myapp -p 8080:8080 myapp"'
                    }
                }
            }
        }
    }
}

Step 3: Commit and Push

Commit the Jenkinsfile to your repository and push it to your version control system. This will trigger the CI/CD pipeline.

git add Jenkinsfile
git commit -m "Add Jenkins CI/CD pipeline"
git push origin main

Step 4: Configure Jenkins Job

  1. Go to your Jenkins dashboard.
  2. Click on "New Item" and create a new Pipeline job.
  3. In the "Pipeline" section, select "Pipeline script from SCM".
  4. Choose your version control system (e.g., Git) and provide the repository URL.
  5. Save the job and trigger it manually or configure it to run on specific events.

Best Practices

  • Use Environment Variables: Store sensitive information like SSH keys or API tokens in environment variables instead of hardcoding them.
  • Parallelize Steps: Use parallel stages to speed up your pipeline execution.
  • Cache Dependencies: Cache dependencies to reduce build times, especially for large projects.
  • Automate Testing: Ensure that all code changes are tested automatically before deployment.
  • Monitor and Optimize: Regularly monitor your CI/CD pipeline performance and optimize it as needed.

Conclusion

Setting up CI/CD pipelines for Go projects can significantly improve your development workflow. By automating build, test, and deployment processes, you can deliver code faster and with higher quality. Whether you choose GitHub Actions, GitLab CI, or Jenkins, the key is to find a tool that fits your team's needs and integrates well with your existing infrastructure.

Feel free to customize these examples to fit your specific project requirements and environment. Happy coding!


PreviousKubernetes with GoNext Monitoring and Logging in Go Applications

Recommended Gear

Kubernetes with GoMonitoring and Logging in Go Applications