Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Comprehensive Tutorial on CI/CD Pipelines

Introduction to CI/CD

CI/CD stands for Continuous Integration and Continuous Deployment. It is a method to frequently deliver apps to customers by introducing automation into the stages of app development. The main concepts attributed to CI/CD are continuous integration, continuous deployment, and continuous delivery.

Setting Up a CI/CD Pipeline

To set up a CI/CD pipeline, various tools such as Jenkins, GitLab CI, CircleCI, and Travis CI can be used. In this tutorial, we'll use GitHub Actions to demonstrate a simple CI/CD pipeline for a Go application.

Prerequisites

Before setting up the CI/CD pipeline, ensure you have the following:

  • A GitHub account
  • A Go programming environment set up on your local machine
  • Basic knowledge of Git and GitHub

Step 1: Create a Go Project

First, create a simple Go project. Open your terminal and run the following commands:

mkdir my-go-app
cd my-go-app
go mod init my-go-app
touch main.go

Next, open main.go and add the following code:

package main

import "fmt"

func main() {
fmt.Println("Hello, CI/CD!")
}

Step 2: Push to GitHub

Initialize a Git repository, add your files, commit, and push to GitHub:

git init
git add .
git commit -m "Initial commit"
git remote add origin <your_github_repo_url>
git push -u origin master

Step 3: Configure GitHub Actions

GitHub Actions allows you to automate, customize, and execute software development workflows right in your repository. To configure GitHub Actions for your Go project, follow these steps:

Create a directory for your GitHub Actions workflows:

mkdir -p .github/workflows
cd .github/workflows

Create a workflow configuration file named ci.yml with the following content:

name: Go CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
- name: Build
run: go build -v .
- name: Test
run: go test -v .

Commit and push your workflow configuration:

git add .github/workflows/ci.yml
git commit -m "Add GitHub Actions configuration"
git push

Step 4: Verify the Pipeline

After pushing the workflow configuration, navigate to the "Actions" tab in your GitHub repository. You should see your workflow running. If everything is set up correctly, you will see a green checkmark indicating that the build and tests passed successfully.

Step 5: Continuous Deployment

To add continuous deployment to your pipeline, you can use GitHub Actions to deploy your application to a cloud provider such as AWS, Google Cloud, or Azure. Below is an example of how to deploy a Docker container to AWS Elastic Beanstalk:

First, create a Dockerfile in the root directory of your project:

FROM golang:1.15
WORKDIR /app
COPY . .
RUN go build -o main .
CMD ["./main"]

Next, update your ci.yml file to include deployment steps:

name: Go CI/CD

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.15
- name: Build
run: go build -v .
- name: Test
run: go test -v .
- name: Build Docker image
run: docker build -t my-go-app .
- name: Deploy to AWS Elastic Beanstalk
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: |
docker tag my-go-app:latest <your_aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-go-app:latest
docker push <your_aws_account_id>.dkr.ecr.<region>.amazonaws.com/my-go-app:latest
eb init -p docker my-go-app --region <region>
eb create my-go-app-env

Commit and push the changes:

git add Dockerfile .github/workflows/ci.yml
git commit -m "Add Docker and AWS deployment steps"
git push

Conclusion

Setting up a CI/CD pipeline is a crucial step in modern software development. It ensures that your code is continuously integrated, tested, and deployed, which helps in maintaining a high standard of code quality and faster release cycles. In this tutorial, we demonstrated how to set up a CI/CD pipeline for a Go application using GitHub Actions.