4 Hours

8. GitHub Actions: CI/CD Fundamentals

CI/CD

GitHub Actions is a powerful CI/CD platform built into GitHub. It enables you to automate testing, building, and deployment directly from your repository. Understanding GitHub Actions is essential for modern software development. It's the standard for CI/CD in the GitHub ecosystem. With GitHub Actions, you can build, test, and deploy your code without leaving GitHub.

The key insight is that GitHub Actions is event-driven automation. Workflows are triggered by GitHub events (push, pull_request, schedule). Each workflow defines jobs that run on virtual machines. Jobs run steps that execute commands. This model enables complete automation of your development pipeline. You can run tests on every push, deploy to staging on every PR, and deploy to production on every merge to main.

This module covers the basics of GitHub Actions: workflows, jobs, steps, actions, and runners. You'll learn to write your first workflow, understand the YAML syntax, and see real-world examples. By the end, you'll be able to automate your testing and deployment pipeline.

Workflow Structure

A GitHub Actions workflow is a YAML file stored in .github/workflows/. It defines the automation pipeline. Each workflow has a name, trigger (on), and jobs. Jobs run in parallel by default. Each job has steps that run commands or use actions. The workflow structure is: name → on (trigger) → jobs (list) → each job has runs-on (runner) → steps (list). Steps can be run (shell commands) or uses (actions). Understanding this structure is essential for writing workflows.

Workflows are the foundation of GitHub Actions. You define what should happen, when, and how. The on trigger determines when the workflow runs — on push, on pull_request, on schedule, or on many other events. The jobs define what work to do. Each job runs on a runner (virtual machine). The steps are the actual commands or actions to execute. This structure gives you complete control over your automation.

Common Actions and Runners

GitHub Actions provides a marketplace of pre-built actions. Common actions include: actions/checkout (checkout code), actions/setup-node (setup Node.js), actions/setup-python (setup Python), and actions/upload-artifact (save files). Runners are the virtual machines that run jobs: ubuntu-latest, windows-latest, and macos-latest. Actions are reusable units of code. They save you from writing repetitive boilerplate. The runner determines the OS and environment. Choose the right runner for your use case. Most workflows use ubuntu-latest for compatibility.

Using pre-built actions saves time and reduces errors. Instead of writing shell commands to set up Node.js, you use the setup-node action. Instead of writing commands to upload artifacts, you use the upload-artifact action. This makes workflows more maintainable and reliable.

Step-by-Step: Your First GitHub Actions Workflow

Follow these steps to create your first GitHub Actions workflow. This will run tests automatically on every push.

Step 1: Create the workflow directory
In your repository, create the directory: mkdir -p .github/workflows

Step 2: Create the workflow file
Create a file called .github/workflows/ci.yml with this content:
name: CI<br><br>on:<br> push:<br> branches: [ main ]<br> pull_request:<br> branches: [ main ]<br><br>jobs:<br> test:<br> runs-on: ubuntu-latest<br> steps:<br> - uses: actions/checkout@v4<br> - uses: actions/setup-node@v4<br> with:<br> node-version: <strong><code>18
- run: npm install
- run: npm test

Step 3: Commit and push
git add .github/workflows/ci.yml
git commit -m <strong><code>Add CI workflow
git push

Step 4: View the workflow
Go to your repository on GitHub. Click the Actions tab. You'll see your workflow running. Click on it to see the details of each job and step.

Step 5: Make a change to trigger the workflow
Make a change to your code, commit, and push. The workflow will run automatically.

Try It Yourself: Add a linting step to the workflow. Add a step that runs npm run lint after the test step. Push and watch it run.

Knowledge Check

Ready to test your understanding of 8. GitHub Actions: CI/CD Fundamentals?

Back