Continuous Integration (CI) and Continuous Deployment (CD) are crucial practices in modern software development. They ensure that code changes are automatically tested, integrated, and deployed to production, which reduces the risk of errors and enhances development efficiency. This chapter will guide you through setting up CI/CD pipelines for Go applications.
Choosing a CI/CD Tool: There are several CI/CD tools available, such as GitHub Actions, GitLab CI, Travis CI, CircleCI, and Jenkins. Each has its advantages, but for simplicity, we will use GitHub Actions as an example.
Creating a CI/CD Pipeline: Define a workflow that includes steps for testing, building, and deploying your Go application.
Ensure your Go project is set up correctly with the necessary configuration files:
go.mod
and go.sum
for dependency management.cmd/
, pkg/
, internal/
).Create a directory named .github/workflows/
in the root of your repository. Inside this directory, create a file named ci.yml
.
Here's a basic example of a GitHub Actions workflow for a Go application:
yamlname: Go CI/CD
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@v3
with:
go-version: 1.18
- name: Install dependencies
run: go mod tidy
- name: Run tests
run: go test ./...
- name: Build
run: go build -v ./...
deploy:
needs: build
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18
- name: Build
run: go build -v -o myapp ./...
- name: Deploy to Server
env:
SSH_PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
SERVER_USER: ${{ secrets.SERVER_USER }}
SERVER_IP: ${{ secrets.SERVER_IP }}
run: |
echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "mkdir -p ~/app"
scp -o StrictHostKeyChecking=no myapp $SERVER_USER@$SERVER_IP:~/app/myapp
ssh -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP "sudo systemctl restart myapp"
actions/checkout
action to check out the code from the repository.actions/setup-go
action to set up the Go environment with the specified version.go mod tidy
to ensure all dependencies are installed.go test ./...
to execute all tests in the repository.Caching Dependencies: Improve build times by caching dependencies.
yaml- name: Cache Go modules
uses: actions/cache@v2
with:
path: |
~/go/pkg/mod
~/.cache/go-build
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
Static Analysis: Integrate tools like golint
, go vet
, and staticcheck
to enforce code quality.
yaml- name: Run Golint
run: go install golang.org/x/lint/golint@latest && golint ./...
Containerization: Use Docker to containerize your application and ensure consistency across environments.
yaml- name: Build Docker Image
run: docker build -t myapp:latest .
- name: Push Docker Image
run: docker push myapp:latest
Setting up a CI/CD pipeline for Go applications ensures that your code is always tested, built, and deployed consistently. By leveraging tools like GitHub Actions, you can automate these processes, reducing manual effort and minimizing the risk of errors.