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.
Before you start, ensure you have the following:
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.
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"
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
Go to your GitHub repository, navigate to the "Actions" tab, and monitor the workflow execution. You should see the build and deployment steps running.
GitLab CI is another powerful tool for automating your development workflows. It integrates seamlessly with GitLab repositories.
.gitlab-ci.yml FileCreate 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"
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
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.
Jenkins is a widely used open-source automation server that can be configured for various CI/CD workflows.
Install the following plugins in Jenkins:
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"'
}
}
}
}
}
}
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
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!