DEV Community

Cover image for Automated CI/CD with GitHub Actions: From Testing to Production Deployment
Daleska
Daleska

Posted on

Automated CI/CD with GitHub Actions: From Testing to Production Deployment

Introduction

In modern software engineering practices, the integration and deployment of code is as important as writing the code itself. Continuous Integration (CI) and Continuous Deployment (CD) are essential methodologies for ensuring that changes in code are tested, integrated, and deployed in an automated and efficient manner. GitHub Actions is a powerful tool for automating these workflows directly within GitHub repositories, facilitating both the execution of automated tests and the deployment of applications to production environments.

In this article, we will explore how to set up a comprehensive CI/CD pipeline using GitHub Actions. Specifically, we will demonstrate how to automate the process of running tests whenever changes are pushed to the repository, and how to automatically deploy the application to a production environment such as Heroku, upon successful completion of the tests.


Prerequisites

Before starting with the configuration, ensure you have the following:

  • A project with automated tests set up (we’ll be using Node.js and Jest in this case).

  • A repository on GitHub.

  • Access to a production server or cloud service, such as Heroku.


Setting Up the CI/CD Workflow with GitHub Actions

1. Create the GitHub Actions Configuration File

GitHub Actions uses YAML configuration files to define workflows. These workflows specify the various jobs and steps that GitHub will execute automatically upon certain triggers (e.g., pushing code to the repository).

  1. Create a directory .github/workflows in the root of your repository if it does not already exist.

  2. Inside this folder, create a file named ci-cd.yml. This file will define the CI/CD pipeline.

Here’s an example configuration for automating tests and deploying the application to Heroku:

name: CI/CD Pipeline

on:
  push:
    branches:
      - main  # Triggers the workflow when there is a push to the main branch

jobs:
  # Job to run the tests
  test:
    runs-on: ubuntu-latest  # Specifies the environment (Ubuntu latest version)

    steps:
      - name: Checkout code
        uses: actions/checkout@v2  # Checks out the repository code

      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '14'  # Specifies the Node.js version for the environment

      - name: Install dependencies
        run: npm install  # Installs the project dependencies

      - name: Run tests
        run: npm test  # Runs the tests using npm

  # Job to deploy the app to production (only if tests pass)
  deploy:
    needs: test  # Ensures that the deploy job runs only if the test job is successful
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up Heroku credentials
        uses: akshatshah/heroku-action@v1
        with:
          heroku_api_key: ${{ secrets.HEROKU_API_KEY }}  # Using GitHub Secrets for Heroku API key

      - name: Deploy to Heroku
        run: |
          git remote add heroku https://git.heroku.com/your-app.git
          git push heroku main  # Deploys the application to Heroku.
Enter fullscreen mode Exit fullscreen mode

Explanation of the Workflow:

  • on.push.branches: This section defines the event that triggers the workflow. Here, it is configured to run whenever code is pushed to the main branch.

  • jobs.test:

This job will run on Ubuntu.

It consists of several steps:

Checkout code: This step pulls the latest code from the repository.

Set up Node.js: Ensures that the correct version of Node.js is available for the application.

Install dependencies: Installs all the necessary dependencies listed in your package.json.

Run tests: Executes the tests using npm test.

  • jobs.deploy:

This job is set to run only after the test job is successful (needs: test).

It deploys the application to Heroku using a stored API key in GitHub Secrets. This makes the deployment secure.

2. Configuring Tests with Jest

To automate the testing process, we need to configure a testing framework. In this tutorial, we will use Jest, a popular testing framework for JavaScript applications.

Install Jest as a Development Dependency:

npm install --save-dev jest
Enter fullscreen mode Exit fullscreen mode
  • Create a simple test file in the tests folder:
// __tests__/app.test.js
test('sumar 2 + 2', () => {
  expect(2 + 2).toBe(4);
});
Enter fullscreen mode Exit fullscreen mode
  • Make sure your package.json has a test script:
`"scripts": {
  "test": "jest"
}
Enter fullscreen mode Exit fullscreen mode

3. Create GitHub Secrets

To connect GitHub Actions to your production server (for example, Heroku), you need to configure credentials in GitHub Secrets.

  1. Go to your GitHub repository.

  2. In the top menu, click Settings.

  3. From the left sidebar, select Secrets and variables > Actions.

  4. Add a new secret with the name HEROKU_API_KEY and paste your Heroku API key as the value.

4. Verify the Workflow

Once everything is configured, make a "push" to your repository and verify that the workflow runs automatically. You can do this in the Actions tab of your repository on GitHub. If everything is set up correctly, GitHub Actions will run the tests, and if they pass, it will deploy the application to production.

Conclusion

GitHub Actions is an incredibly powerful tool for automating your CI/CD workflows. In this article, we’ve configured a workflow that not only runs tests automatically but also deploys our application to a production environment using Heroku, all from within GitHub.

This example is just a starting point. GitHub Actions has many more features, such as integrating other CI/CD services, generating documentation, or even notifying your team automatically about the status of tests and deployments.

Example Repository

Here’s the link to my example repository on GitHub where you can see the full code and configuration of this workflow:

GitHub Repository - Example CI/CD with GitHub Actions

Top comments (0)