In this chapter, we'll explore the process of deploying Go applications and setting up Continuous Integration/Continuous Deployment (CI/CD) pipelines. From the basics of deployment to advanced CI/CD techniques, we'll cover everything you need to know to streamline your development workflow.
Deployment refers to the process of making your application available for use. In the context of Go, deployment involves packaging your application and its dependencies into a format that can be executed on a target environment.
Go allows you to compile your code into a single binary executable, which contains all the necessary dependencies. This makes deployment straightforward as you only need to distribute the binary file.
Docker containers provide a lightweight and consistent environment for running applications. You can package your Go application into a Docker image, which encapsulates the application along with its dependencies and runtime environment.
# Start from the official Go image
FROM golang:latest as builder
# Set the working directory
WORKDIR /app
# Copy the Go mod and sum files
COPY go.mod go.sum ./
# Download dependencies
RUN go mod download
# Copy the source code
COPY . .
# Build the Go app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
# Start a new stage from scratch
FROM alpine:latest
# Set the working directory
WORKDIR /root/
# Copy the pre-built binary from the previous stage
COPY --from=builder /app/app .
# Expose port 8080
EXPOSE 8080
# Command to run the executable
CMD ["./app"]
In this example, we create a multi-stage Dockerfile to build and deploy a Go application as a Docker container.
CI/CD is a set of practices aimed at automating the process of integrating code changes, running tests, and deploying applications. Continuous Integration (CI) focuses on automating the build and testing of code changes, while Continuous Deployment (CD) automates the deployment of successful builds to production.
name: CI/CD Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: '1.17'
- name: Build and Test
run: go build && go test ./...
- name: Code Quality Checks
run: golangci-lint run
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Deploy to Staging
run: |
# Add commands to deploy to staging environment
echo "Deploying to staging..."
- name: Integration Tests
run: |
# Add commands to run integration tests
- name: Manual Approval
run: |
# Add manual approval step
- name: Deploy to Production
run: |
# Add commands to deploy to production environment upon approval
This GitHub Actions workflow demonstrates a CI/CD pipeline for a Go application. It includes build and test steps, deployment to staging, integration tests, manual approval, and deployment to production.
In this chapter, we've explored the intricacies of deploying Go applications and setting up CI/CD pipelines. From understanding deployment strategies to implementing CI/CD workflows with practical examples, you now have a comprehensive understanding of how to automate the process of delivering Go applications with efficiency and reliability. By leveraging these techniques, you can streamline your development workflow and ensure the consistent delivery of high-quality software. Happy coding !❤️