Using GitHub Actions to automate development
Tags: productivity, devops, github, redpanda
Published: 2022-08-31 15:29:23

If you’re already using Redpanda, then you know one of its most alluring draws is its intention to make data streaming development work as simple as possible. (And, if you aren’t already using Redpanda, you can learn how we deliver that simplicity <a target="_blank" rel="noopener noreferrer" href="https://redpanda.com/blog/rpk-container/">in this blog post</a>.) 

Having scorned complexity, the logical next step was to create an easy and efficient way to automate and test builds that depend on Redpanda. In this post, we’ll show you how to do just that, using the Redpanda GitHub Action. 

## What are GitHub Actions?

Before digging into how to test our code, it’s worth understanding what a <a target="_blank" rel="noopener noreferrer" href="https://github.com/features/actions">GitHub Action</a> is. According to GitHub’s website, “GitHub Actions makes it easy to automate all your software workflows.”

In our case, we'll focus specifically on the continuous integration workflow so that we can run automated tests <a target="_blank" rel="noopener noreferrer" href="https://resources.github.com/ci-cd/">on GitHub CI</a>. After you get your code running and working with Redpanda on your local development environment, how do you ensure that your teammates won’t introduce breaking changes to code? Running the automated tests on the CI will ensure it's working as expected.

Sometimes it makes sense to isolate third-party dependencies in the software architecture, but what if we want to test using a _real_ Redpanda instance? That's the main reason why the Action was created: to bring all the Redpanda power to the GitHub CI environment. It makes the test suite faster and more reliable.

Even if you aren’t using Redpanda on <a target="_blank" rel="noopener noreferrer" href="https://docs.redpanda.com/docs/deployment/production-deployment/">your production environment</a>, you can benefit from its drop-in replacement for other Apache Kafka<sup>Ⓡ</sup> distributions and take advantage of faster boot times and less RAM usage. The Redpanda GitHub Action means saving build minutes (and CI costs) by using it.

So how do you use it? Let's configure it together.

## Local development

<p style="border-left: 4px solid hsla(0, 0%, 92%, 1.0); padding-left:10px">Disclaimer: The <a target="_blank" rel="noopener noreferrer" href="https://www.ruby-lang.org/en/">Ruby</a> language is an entirely arbitrary choice. You can write the code in any language you prefer.</p>

All the code discussed here is available on the <a target="_blank" rel="noopener noreferrer" href="https://github.com/fernandes/Redpanda-action-demo">Redpanda-action-demo</a> repository.

Here we have a test suite that performs two tests:

1. It publishes to a Redpanda topic
2. It fetches the message 

Both tests are supported by a setup that connects to Redpanda using the <a target="_blank" rel="noopener noreferrer" href="https://rubygems.org/gems/ruby-kafka">ruby-kafka gem</a>.

<p style="border-left: 4px solid hsla(0, 0%, 92%, 1.0); padding-left:10px">Note: Always use <code>localhost:9092</code> as your Redpanda address. It works locally, and GitHub CI will spin up the Docker image and bind its port to 9092.</p>

## Continuous Integration

To use the Redpanda GitHub Action, you’ll need to configure GitHub CI to run the test. This is configured in `.github/workflows/ci.yml`. Here are its contents, which we will talk more about below:

```
# the name of our job
name: CI

# yes, we want to run for all branches and pull requests
on:
  push:
	branches: "*"
  pull_request:
	branches: "*"

# we have just our job `test`
jobs:
  test:
	runs-on: ubuntu-latest
	# here is the main section for us, where we spin up the Redpanda instance
	# using the Redpanda GitHub action,pay attention on the `.with.version` key, we are using _latest_ but you can use any Redpanda version
	# tip: version is exactly the same as the Redpanda docker image
	steps:
  	- name: start Redpanda
    	uses: Redpanda-data/github-action@v0.1.3
    	with:
      	version: "latest"
  	- uses: actions/checkout@v2
  	# below is how we setup ruby and run the tests using `rake`
  	- name: Set up Ruby
    	uses: ruby/setup-ruby@359bebbc29cbe6c87da6bc9ea3bc930432750108
    	with:
      	ruby-version: '3.0'
      	bundler-cache: true
  	- name: Install dependencies
    	run: bundle install
  	- name: Run tests
    	run: bundle exec rake
```

After committing this file and pushing it to a branch, GitHub CI will run the test suite automatically and give you feedback about the changes on your pull request, showing you if the test failed or was successful.

Using the Redpanda GitHub Action is a straightforward way to get Kafka-based projects tested. It's simple, using just one Docker image with everything you need. There’s no JVM, no Zookeeper<sup>Ⓡ</sup>: it's an all-in-one solution. And it's super fast! Redpanda will boot up and be ready to work in a few seconds. It's a huge win in the developer experience.

When compared to <a target="_blank" rel="noopener noreferrer" href="https://rubygems.org/gems/ruby-kafka">a popular Kafka Docker image</a>, the Redpanda GitHub Action offers smaller RAM usage (47MB vs. 465MB) and smaller Docker image size (only 130MB vs. 465MB). Considering GitHub Actions is billed by minutes usage, the smaller the footprint, the cheaper it's to run the tests.

After getting set up and running the tests locally, we now know that everything is working properly and can now publish to our repo and collaborate.

## How will you use the Redpanda GitHub Action?

We want to hear how you use your newfound knowledge about the Redpanda Action on Github CI. Join <a target="_blank" rel="noopener noreferrer" href="https://rpnda.co/slack">the community Slack</a> to share your ideas and experience, and check out <a target="_blank" rel="noopener noreferrer" href="https://docs.redpanda.com/">Redpanda’s documentation</a> for information on the other things you can do with Redpanda and your applications. 

For more information on the Redpanda GitHub Action, or to report an issue, please see <a target="_blank" rel="noopener noreferrer" href="https://github.com/redpanda-data/github-action">the GitHub repository for the project</a>.

You can also visit <a target="_blank" rel="noopener noreferrer" href="https://github.com/fernandes/redpanda-action-demo">the repository for the demo in this article</a>, where you’ll find the code from this article and a sample test suite that uses the Github Action in a real-world scenario.

Happy testing!