
Complete CI/CD pipeline automation starting with GitHub Actions
- DevOps, Development
- 24 Jun, 2024
Escape the nightmare of manual deployment
“Okay, now the coding is done! Let’s connect to the server, get git pull, reinstall dependencies, build, kill the existing process, and launch a new process.”
In the early stages of a project, this manual deployment process may not be too much of a hassle. But what if code needs to be integrated and deployed dozens of times a day as your service grows and your team members grow? The process of a person manually testing every time, connecting to the server, and running a script inevitably causes human error (mistakes) and prevents developers from fully focusing on development.
The practical methodology that solves these problems and automates the development cycle to maximize productivity is CI/CD (Continuous Integration / Continuous Deployment). And one of the easiest and most powerful tools to build this CI/CD is GitHub Actions.
What is CI/CD?
CI (Continuous Integration)
It means regularly and frequently consolidating code written by multiple developers into a central repository (e.g. the main branch on GitHub). The key to successful CI is that automated builds and tests are run whenever code is merged to immediately verify that the new code does not break (regress) the existing system.
CD (Continuous Delivery/Deployment)
This is the process of automatically releasing (distributing) code that has passed CI, that is, verification, to the actual production environment (server) or staging environment. Users will be able to experience the latest features pushed by developers in near real-time without any long waiting times.
Why GitHub Actions?
In the past, to build CI/CD, you had to link separate external services such as Jenkins, Travis CI, and CircleCI and go through complicated server settings. However, the ecosystem has changed significantly with the advent of GitHub Actions.
- Perfect integration: It operates directly within the GitHub repository where the code is stored, so there is no need for a separate service subscription or integration process.
- Event-based operation: Workflows can be executed according to almost all event triggers in GitHub, such as
push,pull requestopening, merging a specific branch, and periodic execution (Cron). - Various open source actions: Even if you don't write your own script, numerous actions such as AWS connection, Docker build, Slack notification, etc. are created as 'action' units in the GitHub Marketplace, so you can just use them as if assembling blocks.
- Generous free quota: It is unlimited free in public repositories, and private repositories also provide a generous amount of execution time (2,000 minutes) for free each month.
GitHub Actions Capturing key concepts
These are the four main components you need to know before writing a configuration file.
- Workflow: refers to the entire automated process. It is written as a YAML file (
.yml) in the.github/workflows/directory. - Event: This is a trigger that executes the workflow. (e.g. when code is
pushedto themainbranch) - Job: An independent unit of work executed within a workflow. One workflow can have multiple Jobs, and by default, they are executed in parallel. (e.g.
test-job,deploy-job) - Step: A unit of individual commands executed sequentially within a Job. You can run a shell script (
run) or use an action created by someone else (uses).
Example: Node.js project test automation (CI) workflow
This is a ci.yml example that automatically installs npm packages and runs test code whenever code is pushed in a simple React or Node.js project.
# .github/workflows/ci.yml
name: Node.js CI
# When will it run? (Event)
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build-and-test:
# What environment (OS) will it run in?
runs-on: ubuntu-latest
steps:
# 1. Import the code from the current repository into the virtual environment. (Checkout)
- name: Checkout repository
uses: actions/checkout@v3
# 2. Set up the Node.js environment.
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18.x'
cache: 'npm' # Speed up the next build through npm caching.
# 3. Install dependency packages
- name: Install dependencies
run: npm ci
# 4. Run Lint check and test
- name: Run lint and tests
run: |
npm run lint
npm run test
finish
Save the above YAML code in .github/workflows/ in the top-level folder of the project, commit it, and push it. If you go to the 'Actions' tab of your GitHub repository, you'll see the magic of your workflow whirring away, furiously testing your code in an Ubuntu virtual environment.
Once your CI is fully established, the next step is to add a deployment (CD) pipeline to transfer this code to a server such as AWS EC2 or Vercel. With GitHub Actions, you can free yourself from tedious, repetitive manual tasks and spend more time writing creative code!









