Back to Blog
Lesson 53 of the Git & GitHub: Git & GitHub from Zero course
GitSeptember 9, 20264 min read

Continuous Integration Pipeline: Automate Your Testing Workflow

Learn to build a Continuous Integration (CI) pipeline using GitHub Actions. Automate your testing workflow to ensure code quality on every pull request.

cigithub actionstestingautomationdevops

Previously in this course, we covered Final Project Integration. Now that your project is stable, we’ll move from manual verification to automation by building a Continuous Integration (CI) pipeline.

Why You Need Continuous Integration

In a professional environment, you never merge code that hasn't been tested. As you learned in our Collaborative Code Reviews lesson, humans are great at catching logic issues, but we are terrible at running repetitive verification steps manually.

Continuous Integration (CI) is the practice of automating the integration of code changes into a shared repository. By running tests on every push or pull request, you ensure that new changes don't break existing features. If you are interested in the broader ecosystem, you might also want to look at Setting Up a CI Pipeline: Automate Your Testing Workflow to see how this fits into larger engineering teams.

Defining a Workflow File

GitHub Actions uses "workflows" to automate tasks. A workflow is a YAML file stored in your repository under .github/workflows/. When you push this file, GitHub detects it and runs the defined steps automatically.

Every workflow requires three core components:

  1. Trigger: An event that starts the job (e.g., on: pull_request).
  2. Runner: A virtual machine (e.g., ubuntu-latest) provided by GitHub.
  3. Steps: The specific commands to run (e.g., installing dependencies and running tests).

Worked Example: Creating a Test Pipeline

Let’s create a workflow to run a test script whenever someone opens a pull request. Create a file at .github/workflows/ci.yml in your project:

YAML
name: Continuous Integration

# 1. Trigger the workflow on pull requests to the main branch
on:
  pull_request:
    branches: [ main ]

jobs:
  test:
    # 2. Use a standard Linux environment
    runs-on: ubuntu-latest
    
    steps:
    # 3. Check out your code from the repository
    - name: Checkout code
      uses: actions/checkout@v4

    # 4. Set up your language environment (e.g., Node.js, Python)
    - name: Set up Node.js
      uses: actions/setup-node@v4
      with:
        node-version: '20'

    # 5. Install dependencies and run your test script
    - name: Run tests
      run: |
        npm install
        npm test

Once you commit and push this file, GitHub will track the execution status. If your npm test command exits with an error, the build fails, and GitHub will prevent the merge until the issue is resolved. You can dive deeper into this process by checking out Running Tests in CI: Automating Your Validation Pipeline.

Hands-on Exercise

  1. Navigate to your project root.
  2. Create the directory structure: mkdir -p .github/workflows.
  3. Create the ci.yml file with the contents provided above.
  4. Commit the file and push it to your remote repository.
  5. Open a dummy pull request (you can just change a comment in your README.md) and watch the "Actions" tab in your GitHub repository.

Common Pitfalls

  • Hardcoding paths: Always use the actions/checkout step first. Your runner starts in a blank environment; it doesn't know about your files unless you check them out.
  • Ignoring output: If your tests fail, click on the "Details" link in the Pull Request UI. GitHub Actions logs every command, making it easy to find why a test failed.
  • Missing dependencies: If your code requires environment variables (like API keys), remember that you must configure them in the repository settings under "Secrets and variables" and map them into your workflow file.

FAQ

What happens if I push to main directly? The configuration above only triggers on pull_request. If you want to run tests on direct pushes, add push: branches: [ main ] to the on: section.

How much does this cost? GitHub Actions has a generous free tier for public repositories and a monthly allowance for private ones. For most beginner projects, it is completely free.

Can I run multiple jobs? Yes, you can define multiple jobs under the jobs: key. They run in parallel by default, which is great for speeding up your build.

Recap

We have moved from manual verification to automated quality control. By using GitHub Actions, you ensure that every line of code submitted to your project is validated against your test suite. This foundational practice keeps your main branch healthy and your deployments reliable.

Up next: Security Auditing — learning how to use GitHub's built-in tools to scan your dependencies for vulnerabilities.

Similar Posts