This tutorial will illustrate how you can integrate GitHub Actions with Apache Kafka via Lenses.io CLI. GitHub Actions offers a powerful way of triggering workflows and allowing you to communicate with lots of systems using command line utilities.
Two reasons for sending events into Kafka through lenses.io using a CI/CD system would be to be consumed by a microservice or be processed by the powerful SQL Engine of lenses.io.
Pre-requisites
In order to follow this tutorial, you are going to need to have Lenses installed.
You can download the free Lenses Box from here, which comes with an instance of Lenses and a complete Kafka environment as a single Docker image.
The scenario also assumes the following:
You have Lenses up and running and accessible from the Internet
You already have created and cloned the desired GitHub repository on your local machine
You have the necessary privileges for making changes to your GitHub repository
The Scenario
You want to log to Kafka certain kinds of events that happen on a GitHub repository that you own using GitHub actions. These events can then be consumed by a microservice.
Implementing the scenario
This section will illustrate the steps needed for implementing the scenario, starting with the creation of a new service account in Lenses.
Creating a Lenses Group
Before creating a new service account, you should have at least one Group that this service account will belong to. For the purposes of this blog post, we are going to create a new group using Lenses UI. For creating a new group you should go the panel and click on the Admin
sign under the +
text. The GROUPS
group will be created as follows.github-actions

Creating a Lenses Service Account
After logging in to Lenses, you should go to the panel and select the Admin
option. After that click on the Service Accounts
button and add the necessary information.New Service Account
The creation of the new service account is illustrated in the following screenshot.

The name of the service account will be . After clicking on github
button, the service account will be created and a security token will be given to you, as illustrated by the next screenshot.Create New Service Account

This security token will be displayed only once and there is no other way to be displayed again in Lenses. This means that you will have to save it in order to be able to put it in a GitHub Secret variable or use it elsewhere.
Creating a Kafka Topic
We will create a new Kafka Topic () in Lenses to store the records coming from GitHub Actions. The name of the Kafka topic will be Dashboard > Topics
and is going to be created as follows:github-logs

After that go drill down to the created topic and change the serialisation type of the topic key and values as and the STRING
as value
followed by clicking JSON
Update.

Note that you will need to create the Kafka Topic only once, which is the main reason that we do not automate the creation of the Kafka topic. Additionally, you can also create a Kafka topic in Lenses using and the lenses-cli
command or with a SQL command in lenses-cli topic create
section.SQL Studio
Last, note that it is not necessary to define the structure of the JSON records that will be stored in .github-logs
Storing Secrets in GitHub
The value of the token that was generated by Lenses when creating the service account is what is going to be stored as a GitHub secret.
In your GitHub repository go to the tab and select Setting
from the left column. You will see existing secrets, if any, and a Secrets
link that you will need to click on.Add new secret
The next screenshot shows the creation of the new secret in GitHub UI.

The name of the secret is and its value is the token from the Lenses service account – the name of the secret can be anything you want.SERVICE_ACCOUNT
Creating GitHub Actions
You can create a new GitHub action in an existing GitHub repository by going to the tab and selecting the type of action you want to create. In our case, the type of action will be Actions
, which means that you should click on the Simple workflow
button under Set up this workflow
. This will allow you to create a new configuration file. As we will make the necessary changes afterwards, we will press on the Simple workflow
button on the top right corner and continue with the default configuration.Start commit

All GitHub actions are defined and implemented under the directory of your GitHub repository. In this case the name of the YAML file with the definition of the GitHub action will be .github/workflows
.lenses.yml
GitHub Environment Variables
When a GitHub Action is triggered and before it gets executed, GitHub automatically creates and sets a number of environment variables, including the following:
: This environment variable holds the name of the GitHub event that took place.GITHUB_EVENT_NAME
: This environment variable holds the name of the GitHub repository.GITHUB_REPOSITORY
: This environment variable holds the name of the GitHub branch.GITHUB_REF
: This environment variable holds the name of the GitHub user.GITHUB_ACTOR
Viewing the GitHub Actions configuration
The contents of the file are as follows:lenses.yml
```
name: Lenses CLI
on: [push, pull_request, create, issue_comment, delete]
jobs:
build:
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: Download lenses-cli
env:
SERVICE_ACCOUNT: ${{ secrets.SERVICE_ACCOUNT }}
run: |
wget https://github.com/lensesio/lenses-go/releases/download/3.0.10/lenses-cli-linux-386.tar.gz
tar xzvf lenses-cli-linux-386.tar.gz
mv lenses-cli-linux-386 lenses-cli
chmod 755 insert.sh
./insert.sh
```It is really important to understand how you can access the GitHub secret that we defined earlier because this has to be done through the use of an environment variable. So, you access the secret as in your YAML file and you assign its value to an environment variable named secrets.SERVICE_ACCOUNT
that will be accessible in the GitHub job.SERVICE_ACCOUNT
also shows how you can download and extract lenses.yml
in case you need it for querying Lenses or performing other administrative tasks.lenses-cli
The values of the key define when this job will be triggered. The last command is on
is a bash script that will be shown in the next subsection.lenses.yml
The insert.sh bash script
The last step in the job is the execution of a script named . Although it is not mandatory to use a script, a script is very convenient for grouping and executing a set of commands.insert.sh
The contents of are the following:insert.sh
```
{
"value": {
"VendorID": 2,
"tpep_pickup_datetime": "2016-01-01 00:00:00",
"tpep_dropoff_datetime": "2016-01-01 00:00:00",
"passenger_count": 1,
"trip_distance": 10.54,
"pickup_longitude": "-73.98455047607422",
"pickup_latitude": "40.6795654296875",
"RateCodeID": 1,
"store_and_fwd_flag": "N",
"dropoff_longitude": "-73.95027160644531",
"dropoff_latitude": "40.78892517089844",
"payment_type": 1,
"fare_amount": 33,
"extra": 0.5,
"mta_tax": 0.5,
"improvement_surcharge": 0.3,
"tip_amount": 0,
"tolls_amount": 0,
"total_amount": 34.3
},
"metadata": {
"offset": 0,
"partition": 0,
"timestamp": 1587447010296,
"__keysize": 0,
"__valsize": 147
}
},
```The script is using insert.sh
for inserting data into Lenses.curl(1)
The definition of the variable includes the name of the Kafka topic as well as the type of its key (endpoint
) and its value (kt=STRING
) and is really important for the successful execution of vt=JSON
.insert.sh
Additionally, the value of is the hostname or the IP address of the Lenses server and should be accessible from the Internet. If you do not have a Lenses server available on the Internet, you might find the server
utility really helpful.ngrok
Viewing the results
Once you start pushing, pulling, raising issues, branches and adding comments to your repository, the GitHub action we defined will start being executed and write data to Kafka topic through Lenses.
We can visit the Kafka topic and see its entries.github-actions

Additionally, you can check the results of the GitHub action in GitHub UI.

If there exist any errors in the definition of the GitHub Action, they will be shown in GitHub UI.
Next Steps
Now that you know how to use GitHub actions to communicate with Lenses, you should start automating as many things as possible. Learn how to apply GitOps to your real time pipelines in our blog.






