Author: Daniyal Javed


Automating infrastructure nowadays is becoming fairly common and one of the first things that Enterprises look towards when modernizing their technology stack.

Enterprises are looking to move fast, and in order to move quickly, you have to decrease the amount of time it takes to provision and configure new infrastructure. We have countless blogs covering this with various automation languages such as Ansible and Terraform.

Today, I will make automation a little more fun by provisioning instances on Google Cloud Platform using Google Assistant and Google Dialogflow. In the middle, I’m going to be using GitHub Actions to do the heavy lifting of completing this workflow.

What’s GitHub Actions?

GitHub Actions is an API for cause and effect on GitHub: orchestrate any workflow, based on any event, while GitHub manages the execution, provides rich feedback, and secures every step along the way. With GitHub Actions, workflows and steps are just code in a repository, so you can create, share, reuse, and fork your software development practices.

With the Alpha release of GitHub Actions last year, CI/CD was introduced. GitHub Actions now makes it easier to automate how you build, test, and deploy your projects on any platform, including Linux, macOS, and Windows. Run your workflows in a container or in a virtual machine. Actions also support more languages and frameworks than ever, including Node.js, Python, Java, PHP, Ruby, C/C++, .NET, Android, and iOS.

What’s Dialogflow?

Dialogflow is a natural language processing (NLP) platform that can be used to build conversational applications and experiences on multiple platforms. The Google-powered product enables developers to create text-based and voice conversation interfaces for responding to customer queries in different languages.

For example, Dialogflow can be used to create messaging bots that respond to queries in platforms like Alexa, Google Assistant, Slack and several other messaging integrations.

What’s Google Assistant?

You probably know:

Let’s get started!

Workflow:

By saying the number of instances to Google Assistant, an issue will be opened or closed. This issue will contain the number of instances I’m requesting for. GitHub Actions will parse the number from the issue and execute a Terraform plan to create GCP instances.

Configuring GitHub Actions workflow:

The main GitHub Actions file used is here:

name: 'Issue parser and provision with Terraform'
on:
issues:
    types: [opened]

jobs:
issue_parser:
    runs-on: ubuntu-latest
    name: Grab body of latest issue
    steps:
    - name: 'Checkout' 
    uses: actions/[email protected]
    - name: Parse latest GitHub issue
    env:
        GITHUB_TOKEN: $
    uses: daniyalj/[email protected]
    with:
        org_name: 'daniyalj'
        repo_name: 'actions-assistant-demo'
        global_email: '[email protected]'
    - name: 'Terraform Init'
    uses: hashicorp/[email protected]
    with:
        tf_actions_version: 0.12.16
        tf_actions_subcommand: 'init'
    env:
        GITHUB_TOKEN: $
        GOOGLE_CREDENTIALS: $
    - name: 'Terraform Plan'
    uses: hashicorp/[email protected]
    with:
        tf_actions_version: 0.12.16
        tf_actions_subcommand: 'plan'
    env:
        GITHUB_TOKEN: $
        GOOGLE_CREDENTIALS: $
    - name: 'Terraform Apply'
    uses: hashicorp/[email protected]
    with:
        tf_actions_version: 0.12.16
        tf_actions_subcommand: 'apply'
    env:
        GITHUB_TOKEN: $
        GOOGLE_CREDENTIALS: $

The repo for the above file is here. The above Actions workflow is also leveraging a custom Action which is available on the GitHub Marketplace

GitHub Actions has a secret store which is utilized for GOOGLE_CREDENTIALS while GITHUB_TOKEN is pre-configured in GitHub Actions. For configuring this follow here

Configuring Dialogflow:

I’m not going to get into how to get started with Dialogflow. Once the app has been created, create a Fulfillment with the following code which will call GitHub’s API to close or open an Issue:

const {dialogflow} = require('actions-on-google');
const functions = require('firebase-functions');

const app = dialogflow({debug: true});

app.intent('numberofvms', (conv, {number}) => {
    conv.data.number = parseInt(number);

if (number > 20 ) {
    conv.ask("C'mon! Are you serious? Your lab admin is going to get really really grumpy. Do you want to try again? Now, how many GCP instances would you like?");
}

if (number < 20 ) {
var github = require('request');

github.post({
headers: {'User-Agent' : 'daniyalj', 'Authorization': 'token <**YOUR-GITHUB-TOKEN**>'},
url:     'https://api.github.com/repos/daniyalj/actions-assistant-demo/issues',
json:    
{
"title": "GCP instance",
"body": number,
"assignees": [
    "daniyalj"
],
"labels": [
    "GCP"
]
}
}, function(error, response, body){
console.log(body);
}); 
}
});

app.intent('destroy', (conv) => {

var request = require('request');

request.get({
    headers: {'User-Agent' : 'daniyalj'},
    url:     'https://api.github.com/repos/daniyalj/actions-assistant-demo/issues',
}, function(error, response, body){
    //console.log(body)
    if(error){
    new Error('Check your API endpoint ....');
    }else{
    var parsed = JSON.parse(body);
    //console.log(parsed);
    var issuenum = parsed[0].number ? parsed[0].number : 'The object property does not exist';

    request.patch({
        headers: {'User-Agent' : 'daniyalj', 'Authorization': 'token <**YOUR-GITHUB-TOKEN**>'},
        url:     `https://api.github.com/repos/daniyalj/actions-assistant-demo/issues/${issuenum}`,
        json:
        {
        "state": "closed",
        "assignees": [
        "daniyalj"
        ],
        "labels": [
        "GCP"
        ]
    }
    },
function(error, response, body){
    console.log(body);
        }
    );
    }
});
});

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

Next, create the following Intents:

The above could be anything and could be customized to your liking. The important one is the numberofvm which looks like the following:

Additional components:

I’ve also configured a custom Action for Terraform Destroy, which is available here. This is referenced in the second GitHub Actions file on the repo

See it in action:

I presented this demo at a GitHub meetup last month in Toronto. The workflow starts at 01:01.

Hope this was a fun read! Interested in learning more?

//take the first step

Tagged:



//comments


//blog search


//other topics