GitHub Action for Using the ASK CLI and Bespoken Tools
In this article, we discuss how to implement a GitHub Action to run ASK CLI and Bespoken Commands to reuse it in your GitHub Actions Workflows.
Join the DZone community and get the full member experience.
Join For FreeIt is always good practice in the world of programming to try to develop things that are reusable. So anyone can integrate what has been developed and can quickly start using it.
This is the philosophy behind a GitHub Action. Small individual and reusable tasks that we can combine to create jobs and customize our GitHub Actions workflows.
Prerequisites
Here are the technologies used in this project:
- Amazon Developer Account - How to create an account
- AWS Account - Sign up here for free
- ASK CLI - Install and configure ASK CLI
- GitHub Account - Sign up here for free
- Visual Studio Code
The Alexa Skills Kit Command Line Interface (ASK CLI) is a tool for us to manage our Alexa Skills and its related resources, such as AWS Lambda functions. With the ASK CLI, we have access to the Skill Management API, which allows us to manage Alexa Skills through the command line.
GitHub Actions
GitHub Actions helps us to automate tasks within the software development lifecycle. GitHub Actions is event-driven, which means that we can run a series of commands after a specific event has occurred. For example, whenever someone creates a pull request for a repository, we can automatically run a pipeline on GitHub Actions.
An event automatically triggers the workflow
, which contains one or more jobs. Then the jobs
use steps
to control the order in which the actions are executed. These actions are the commands that automate certain processes.
GitHub Action to Run ASK CLI and Bespoken Commands
An Action is an individual task that we can combine to create jobs and customize our GitHub Actions workflows. We can create our own Actions or use and customize Actions shared by the GitHub community.
We can create an Action by writing custom code that interacts with the repositories in any way we want, including integrating with GitHub APIs and any publicly available third-party APIs. For example, an Action can publish npm modules, send SMS alerts when there is an urgent problem in the pipeline, or deploy our code if it is ready for production.
To share the Actions that we have created, the repository must be public.
Actions can be run directly on a machine or in a Docker container. We have to define the inputs, outputs and environment variables of an Action.
With eveyrthing explained, let’s look at the GitHub Action configuration file found in the action.yml
file.
x
# action.yml
name'Alexa ASK AWS CLI Action'
author'Xavier Portilla Edo'
description'GitHub Action using Docker image for ASK and AWS CLI '
branding
icon'activity'
color'blue'
inputs
command# id of input
description'Command to execute'
requiredtrue
default'ask --version'
outputs
result# id of output
description'The result of the command'
runs
using'docker'
image'./.github/action/Dockerfile'
args
$ inputs.command
As seen in the previous file, the GitHub Action has as input a parameter called command
which will be the ASK CLI or Bespoken command that we want to execute. This command will run on a specific docker container whose Dockerfile is specified in the run
section of the GitHub Action. This Docker executor will be passed the command entered through the command
input parameter for later execution and will return the result of the execution in the result
output parameter of the GitHub Action.
This GitHub Action is based on the Docker image that we have been using throughout the series of posts about Devops and you can find here. Note that this image already has the ASK CLI, the Bespoken Tools, Python, Git, the AWS CLI, among others installed.
However, this new Docker image that GitHub Action uses extends from the original Docker image previously commented.
This second Docker image is exactly the same as the one mentioned but an entrypoint
has been added to it.
This new Docker image can be found in .github/action/Dockerfile
:
x
# Original source from https://hub.docker.com/repository/docker/xavidop/alexa-ask-aws-cli
FROM xavidop/alexa-ask-aws-cli:latest
LABEL maintainer="Xavier Portilla Edo <xavierportillaedo@gmail.com>"
ADD --chown=node:node entrypoint.sh /home/node/entrypoint.sh
RUN chmod 777 /home/node/entrypoint.sh
ENTRYPOINT ["/home/node/entrypoint.sh"]
The new entrypoint
that we have added to this new Dockerfile, the only thing it will do is execute the command that is passed through the input parameter of the GitHub Action called command
:
x
#!/bin/bash
result=$($1)
echo "::set-output name=result::$result"
As output, the entrypoint
will return the result of the execution of the command received from the GitHub Action through the command
parameter in the format required by GitHub.
This format is as follows:::set-outpt name={ID_OUTPUT_PARAMETER}::{VALUE_OUTPUT_PARAMETER}
.
NOTE: It is important to add that the GitHub Action uses the latest version of ASK CLI.
Example of Use
We can use the following example:
x
name Alexa ASK AWS CLI Action
uses xavidop/alexa-ask-aws-cli-docker@v1.0.6
id command
with
command'ask --version'
env# Or as an environment variable
ASK_ACCESS_TOKEN $ secrets.ASK_ACCESS_TOKEN
ASK_REFRESH_TOKEN $ secrets.ASK_REFRESH_TOKEN
ASK_VENDOR_ID $ secrets.ASK_VENDOR_ID
AWS_ACCESS_KEY_ID $ secrets.AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY $ secrets.AWS_SECRET_ACCESS_KEY
# Use the output from the `hello` step
name Get the output
run echo "The result was ${{ steps.command.outputs.result }}"
NOTE: It is important to mention that to run the GitHub Action we must pass all the environment variables that the ASK CLI needs as seen in the previous example.
Using the Action in a GitHub Actions Workflow
This is an example of how GitHub Action can be integrated into a GitHub Actions workflow:
x
on push
jobs
test-action
runs-on ubuntu-latest
name Test Action
steps
# To use this repository's private action,
# you must check out the repository
name Checkout
uses actions/checkout@v2
name Test action step
uses xavidop/alexa-ask-aws-cli-docker@v1.0.6
id ask
with
command'ask --version'
env# Or as an environment variable
ASK_ACCESS_TOKEN $ secrets.ASK_ACCESS_TOKEN
ASK_REFRESH_TOKEN $ secrets.ASK_REFRESH_TOKEN
ASK_VENDOR_ID $ secrets.ASK_VENDOR_ID
AWS_ACCESS_KEY_ID $ secrets.AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY $ secrets.AWS_SECRET_ACCESS_KEY
# Use the output from the `hello` step
name Get the output
run echo "The result was ${{ steps.ask.outputs.result }}"
Test the GitHub Action On Every Commit
In turn, we have created a GitHub Actions workflow that its only purpose is to verify that the Action works in each new commit made to the repo. Thus we can detect that the new changes that are made will not break the current behaviour of the Action.
This workflow can be found in .github/workflows/main.yaml
:
x
on push
jobs
test-action
runs-on ubuntu-latest
name Test Action
steps
# To use this repository's private action,
# you must check out the repository
name Checkout
uses actions/checkout@v2
name Test action step
uses ./
id ask
with
command'ask --version'
env# Or as an environment variable
ASK_ACCESS_TOKEN $ secrets.ASK_ACCESS_TOKEN
ASK_REFRESH_TOKEN $ secrets.ASK_REFRESH_TOKEN
ASK_VENDOR_ID $ secrets.ASK_VENDOR_ID
AWS_ACCESS_KEY_ID $ secrets.AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY $ secrets.AWS_SECRET_ACCESS_KEY
SKILL_ID $ secrets.SKILL_ID
# Use the output from the `hello` step
name Get the output
run echo "The result was ${{ steps.ask.outputs.result }}"
As you can see, in the uses
property of the Test action step
of the workflow, it is forced to take the Action from the repository root directory (./
) Instead of taking it from the marketplace version (xavidop/alexa-ask-aws-cli-docker@v1.0.6
)
Resources
- DevOps Wikipedia - Wikipedia
- Node.js SDK Official Documentation - Node.js SDK Official Documentation
- Official documentation of the Alexa Skills Kit - Official documentation of the Alexa Skills Kit
- Official GitHub Actions documentation - Official GitHub Actions documentation
Conclusion
By creating this GitHub Action we enable any developer to execute commands from the ASK CLI and Bespoken and thus to automate their Alexa Skills processes in a very simple way by creating GitHub Actions workflows.
You can find the code in my GitHub and the GitHub Action on the official GitHub Actions marketplace.
That’s it!
I hope you find it useful! If you have any doubts or questions, feel free to contact me or post a comment below.
Happy coding!
Feel free to join to the Voice Devs Community by clicking here.
Opinions expressed by DZone contributors are their own.
Trending
-
Revolutionizing Algorithmic Trading: The Power of Reinforcement Learning
-
Reactive Programming
-
Building a Flask Web Application With Docker: A Step-by-Step Guide
-
Automating the Migration From JS to TS for the ZK Framework
Comments