Setting Up Continuous Integration with GitHub Actions: A Beginner’s Guide Tutorial
Continuous Integration (CI) is an essential practice in modern software development that helps automate the testing and deployment of code. GitHub Actions is a powerful tool integrated directly into GitHub, allowing you to automate workflows, including building, testing, and deploying code. In this guide, we’ll walk you through setting up Continuous Integration with GitHub Actions, helping you automatically run tests every time you push changes to your repository.
Step 1: What is Continuous Integration (CI)?
1.1 Understanding Continuous Integration
Continuous Integration (CI) is the practice of regularly merging all developers’ working copies to a shared repository. CI ensures that new code changes are automatically tested and verified, reducing the risk of integration bugs and speeding up the development process. It automates the build and testing phases of development, ensuring code quality and fast feedback.
Step 2: What is GitHub Actions?
2.1 Introduction to GitHub Actions
GitHub Actions is an automation tool built into GitHub that lets you define and create custom workflows for automating your software development lifecycle. Workflows are triggered by events (like a code push), and they can include tasks such as building, testing, or deploying code.
GitHub Actions uses YAML configuration files to define workflows, which are stored in your repository.
Step 3: Creating a GitHub Repository
To get started with GitHub Actions, you’ll need a GitHub repository. Follow these steps if you don’t have a repository yet:
- Go to GitHub: Visit github.com and log in.
- Create a New Repository:
- Click the New button on the repositories page.
- Name your repository (e.g.,
ci-github-actions-demo
). - Choose the visibility (public or private) and click Create repository.
- Clone the Repository: After creating the repository, clone it to your local machine by running:bashCopy code
git clone https://github.com/your-username/ci-github-actions-demo.git
Step 4: Setting Up Your Project
For the purpose of this tutorial, let’s assume we’re working with a simple Node.js project. However, the same process applies to other languages and frameworks.
4.1 Initialize a Node.js Project
- Open your terminal and navigate to your project directory:bashCopy code
cd ci-github-actions-demo
- Initialize a new Node.js project:bashCopy code
npm init -y
- Install some dependencies for testing. In this case, we’ll use Jest, a testing framework for JavaScript:bashCopy code
npm install --save-dev jest
- Add a simple test. Create a
sum.js
file that contains the following function:javascriptCopy codefunction sum(a, b) { return a + b; } module.exports = sum;
- Now, create a
sum.test.js
file for testing thesum
function:javascriptCopy codeconst sum = require('./sum'); test('adds 1 + 2 to equal 3', () => { expect(sum(1, 2)).toBe(3); });
- Add a test script in your
package.json
file:jsonCopy code"scripts": { "test": "jest" }
Now, you can run npm test
to manually test the code. But our goal is to automate this testing process using GitHub Actions!
Step 5: Setting Up GitHub Actions Workflow
Now, we will configure GitHub Actions to automatically run our tests whenever we push code to the repository.
5.1 Create the GitHub Actions Folder
In the root directory of your project, create a .github
folder with a subfolder called workflows
. This is where GitHub will look for your workflow definitions.
bashmkdir -p .github/workflows
5.2 Create the Workflow File
Inside the workflows
folder, create a new YAML file called ci.yml
(you can name it anything):
bashtouch .github/workflows/ci.yml
Open this file in your code editor and add the following content:
yamlname: CI for Node.js
# Trigger the workflow on push or pull request to the main branch
on:
push:
branches:
- main
pull_request:
branches:
- main
# Define the jobs that will run
jobs:
test:
# Use the latest version of Ubuntu
runs-on: ubuntu-latest
# Steps for the job
steps:
# Check out the code from the repository
- name: Checkout code
uses: actions/checkout@v3
# Set up Node.js environment
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
# Install dependencies
- name: Install dependencies
run: npm install
# Run tests
- name: Run tests
run: npm test
Explanation of the Workflow:
- name: The name of the workflow (
CI for Node.js
). - on: The workflow will trigger on
push
andpull_request
to themain
branch. - jobs: The
test
job runs on anubuntu-latest
environment. - steps: Each job consists of steps:
- Checkout code: This step checks out the code from the repository.
- Setup Node.js: This step installs Node.js version 16.
- Install dependencies: This step runs
npm install
to install the project’s dependencies. - Run tests: This step runs the
npm test
command to execute the tests.
Step 6: Committing and Pushing the Workflow
After setting up the GitHub Actions workflow, commit your changes and push them to GitHub:
bashgit add .
git commit -m "Set up CI with GitHub Actions"
git push origin main
Step 7: Viewing the GitHub Actions Workflow
After pushing your code to GitHub, GitHub Actions will automatically run the workflow defined in ci.yml
.
7.1 Viewing Workflow in GitHub
- Go to your repository on GitHub.
- Click on the Actions tab at the top of the page.
- You should see your workflow running. If everything is set up correctly, the tests should pass, and you’ll see a green checkmark indicating success.
Step 8: Automating Additional Tasks
In addition to running tests, GitHub Actions can automate many other tasks such as:
- Running Linting Tools: Automatically run linting tools like ESLint to ensure code quality.
- Deploying to Production: Automatically deploy your code to production servers, services like Heroku, or container platforms like Docker.
- Static Code Analysis: Tools like SonarQube can be integrated for code analysis.
Step 9: Best Practices for Using GitHub Actions
When setting up CI with GitHub Actions, consider the following best practices:
- Keep Your Workflows Simple: Start with a minimal setup and gradually add more steps as needed.
- Use Caching: GitHub Actions allows caching of dependencies (e.g., npm, pip) to speed up the workflow.
- Branch-Specific Workflows: Set up different workflows for different branches (e.g., development vs. production).
- Monitor Workflow Usage: GitHub Actions is free for public repositories but has usage limits for private ones. Monitor your usage to avoid exceeding the limits.
Step 10: Extending CI to Continuous Deployment (CD)
Once you have CI set up, the next step is often Continuous Deployment (CD), where the code is automatically deployed after passing the tests. GitHub Actions can be configured to deploy to a variety of platforms, including AWS, Heroku, and Docker.
For example, to deploy to Heroku after tests pass, you would add a deployment step in your workflow:
yaml# Deploy to Heroku
- name: Deploy to Heroku
uses: akhileshns/heroku-deploy@v3.12.12
with:
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
heroku_app_name: "your-heroku-app-name"
heroku_email: "your-email@example.com"
Conclusion
GitHub Actions makes it incredibly easy to set up Continuous Integration and other automation workflows directly within your GitHub repositories. In this guide, you’ve learned how to:
- Set up a basic Node.js project with automated tests.
- Create a GitHub Actions workflow that runs tests on every push or pull request.
- Extend your workflow to include additional steps, such as deployment.
By adopting CI with GitHub Actions, you’ll reduce manual effort, catch errors early, and streamline your development workflow.
Post Comment