CI/CD

Continuous Integration

Continuous Integration (CI) is a development practice where developers frequently integrate their code changes into a shared repository, preferably several times a day. Each integration is automatically verified by

  1. Building the project and

  2. Running automated tests.

This process allows teams to detect problems early, improve software quality, and reduce the time it takes to validate and release new software updates.

Continuous Deployment

As the name suggests, deploying your code continuously to various environments (dev/stage/prod)

Continuous Deployment in Github

We’ll be deploying a next.js app to EC2 servers via Docker💡You don’t really need Docker here, since it’s deploying on a simple EC2 server. If you deploy to 1. GCP App runner 2. ECS 3. Kubernetes then it makes more sense to deploy a dockerised

Architecture diagram

notion image

How to create a CI/CD pipeline?

For Github, you can add all your pipelines to .github/workflows

notion image

CD pipelines look like this finally -

notion image

Hint - Use https://onlineyamltools.com/convert-yaml-to-json to see the pipeline in json

Step 1 - Create the CI pipeline

Make sure that whenever someone tries to create a PR, we build the project and make sure that it builds as expected

notion image

Lets add a build pipeline for our repo

Anytime a user creates a PR, we need to run npm run build and only if it succeeds should the workflow succeed

  • Fork the main repo

  • Add .github/workflows/build.yml in the root folder

  • Create the workflow

  • Push this to master branch

  • Create a new branch with some minimal changes and create a PR from it

  • You should see the workflow run

notion image

Let’s add a deploy step

  • Create dockerfiles for the apps you have

  • Create docker/Dockerfile.user

  • Add start-user-app script to the root package.json

💡You dont really need to build every app for every dockerfile. Can you change the build command so that only a single app is built for each dockerfile?

notion image
  • Create the CD pipeline that

  • Clones the repo

  • Builds the docker image

  • Pushes the docker image

  • Make sure to add the dockerhub secrets to github secrets of the repo (DOCKER_USERNAME, DOCKER_PASSWORD)

  • You should see a workflow running

notion image
notion image

Check dockerhub to ensure the image has indeed reached there

notion image

💡You might have to inject more environment variables (like DB URL) in there for the build to work as expected

Let’s pull the docker image

Ref - https://github.com/appleboy/ssh-action

  • Create an ec2 server

  • Download its keypair file

  • Allow http/https traffic

  • Ubuntu base image

  • Download docker on the machine

  • sudo docker run hello-world

  • Update workflow to pull the latest image on the ec2 machine

  • Point userapp.your_domain.com to the IP of the server

  • Add nginx reverse proxy to forward requests from userapp.your_domain.com to port on which the app is running

  • Install certbot and Refresh certificate

Last updated

Was this helpful?